Skip to content

Commit

Permalink
refactored crack for upcoming improvements
Browse files Browse the repository at this point in the history
* renamed Crack to Nori
* split classes into separate files
* introduced a parser class
* changed tests to specs
  • Loading branch information
rubiii committed Feb 27, 2011
1 parent 8db0ba5 commit 13a785a
Show file tree
Hide file tree
Showing 29 changed files with 407 additions and 522 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*.sw?
.DS_Store
doc
coverage
rdoc
pkg
*~
*.gem
.bundle
Gemfile.lock
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--colour
13 changes: 2 additions & 11 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
source :rubygems
gemspec

group :development do
gem "jeweler"
gem "rcov", "~> 0.9.8"
end

group :test do
gem "test-unit", "~> 2.1.2"
gem "shoulda", "~> 2.11.0"
gem "jnunemaker-matchy", ">= 0.4.0", :require => "matchy"
end
# Specify your gem's dependencies in nori.gemspec
gemspec
2 changes: 1 addition & 1 deletion History
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
* Parsing empty or blank xml now returns empty hash instead of raising error.

== 0.1.0 2009-03-28
* Initial release.
* Initial release.
50 changes: 2 additions & 48 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,48 +1,2 @@
require 'bundler'
Bundler.require :development

$:.unshift File.expand_path('../lib', __FILE__)
require 'crack'

Jeweler::Tasks.new do |gem|
gem.name = "crack"
gem.summary = %Q{Really simple JSON and XML parsing, ripped from Merb and Rails.}
gem.email = "[email protected]"
gem.homepage = "http://github.com/jnunemaker/crack"
gem.authors = ["John Nunemaker", "Wynn Netherland"]
gem.rubyforge_project = 'crack'
gem.version = Crack::VERSION
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new

require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'crack'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end

require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/*_test.rb'
test.verbose = false
end

begin
require 'rcov/rcovtask'
Rcov::RcovTask.new do |test|
test.libs << 'test'
test.pattern = 'test/**/*_test.rb'
test.verbose = true
end
rescue LoadError
task :rcov do
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
end
end

task :default => :test
require "bundler"
Bundler::GemHelper.install_tasks
63 changes: 0 additions & 63 deletions crack.gemspec

This file was deleted.

8 changes: 0 additions & 8 deletions lib/crack.rb

This file was deleted.

68 changes: 0 additions & 68 deletions lib/crack/json.rb

This file was deleted.

12 changes: 12 additions & 0 deletions lib/nori.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "nori/version"
require "nori/core_ext"
require "nori/parser"

module Nori

def self.parse(xml)
adapter = Parser.find Parser.use
adapter.new.parse xml
end

end
3 changes: 3 additions & 0 deletions lib/nori/core_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require "nori/core_ext/object"
require "nori/core_ext/string"
require "nori/core_ext/hash"
39 changes: 9 additions & 30 deletions lib/crack/core_extensions.rb → lib/nori/core_ext/hash.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
require 'uri'
require "uri"

module Crack
module CoreExtensions
module Object #:nodoc:
# @return <TrueClass, FalseClass>
#
# @example [].blank? #=> true
# @example [1].blank? #=> false
# @example [nil].blank? #=> false
#
# Returns true if the object is empty or !self (if applicable)
def blank?
respond_to?(:empty?) ? empty? : !self
end unless method_defined?(:blank?)
end
module Nori
module CoreExt
module Hash

module String #:nodoc:
def snake_case
return self.downcase if self =~ /^[A-Z]+$/
self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
$+.downcase
end unless method_defined?(:snake_case)
end

module Hash #:nodoc:
# @return <String> This hash as a query string
#
# @example
Expand All @@ -36,7 +16,7 @@ module Hash #:nodoc:
# }.to_params
# #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
def to_params
params = self.map { |k,v| normalize_param(k,v) }.join
params = self.map { |k, v| normalize_param(k,v) }.join
params.chop! # trailing &
params
end
Expand Down Expand Up @@ -71,21 +51,20 @@ def normalize_param(key, value)

param
end

# @return <String> The hash as attributes for an XML tag.
#
# @example
# { :one => 1, "two"=>"TWO" }.to_xml_attributes
# #=> 'one="1" two="TWO"'
def to_xml_attributes
map do |k,v|
map do |k, v|
%{#{k.to_s.snake_case.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"}
end.join(' ')
end

end
end
end

Object.send :include, Crack::CoreExtensions::Object
String.send :include, Crack::CoreExtensions::String
Hash.send :include, Crack::CoreExtensions::Hash
Hash.send :include, Nori::CoreExt::Hash
13 changes: 13 additions & 0 deletions lib/nori/core_ext/object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Nori
module CoreExt
module Object

def blank?
respond_to?(:empty?) ? empty? : !self
end unless method_defined?(:blank?)

end
end
end

Object.send :include, Nori::CoreExt::Object
15 changes: 15 additions & 0 deletions lib/nori/core_ext/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Nori
module CoreExt
module String

def snake_case
return self.downcase if self =~ /^[A-Z]+$/
self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
$+.downcase
end unless method_defined?(:snake_case)

end
end
end

String.send :include, Nori::CoreExt::String
Loading

0 comments on commit 13a785a

Please sign in to comment.