summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Neighman <has.sox@gmail.com>2010-06-15 14:16:25 +0800
committerMichael Bleigh <michael@intridea.com>2010-06-22 22:00:39 +0800
commit0c85e9482f0ca0232d7cf7cf6577a359ea07ffed (patch)
tree34a2dd820c3e78721b3931a87b8a91b28f1091eb
parenta026498fb732eeb8ad9bdb11bf28d21d8c0e3467 (diff)
downloadhashie-0c85e9482f0ca0232d7cf7cf6577a359ea07ffed.tar.gz
Adds a fix for json generation
-rw-r--r--.gitignore1
-rw-r--r--Rakefile19
-rw-r--r--hashie.gemspec43
-rw-r--r--lib/hashie/hash.rb16
-rw-r--r--lib/hashie/mash.rb8
-rw-r--r--spec/hashie/mash_spec.rb54
-rw-r--r--spec/spec_helper.rb5
7 files changed, 59 insertions, 87 deletions
diff --git a/.gitignore b/.gitignore
index 76f8e2a..fb21928 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@ coverage
rdoc
pkg
*.gem
+.bundle
diff --git a/Rakefile b/Rakefile
index 662f76f..8853ee8 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,23 +1,6 @@
require 'rubygems'
require 'rake'
-begin
- require 'jeweler'
- Jeweler::Tasks.new do |gem|
- gem.name = "hashie"
- gem.summary = %Q{Your friendly neighborhood hash toolkit.}
- gem.description = %Q{Hashie is a small collection of tools that make hashes more powerful. Currently includes Mash (Mocking Hash) and Dash (Discrete Hash).}
- gem.email = "michael@intridea.com"
- gem.homepage = "http://github.com/intridea/hashie"
- gem.authors = ["Michael Bleigh"]
- gem.add_development_dependency "rspec"
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
- end
- Jeweler::GemcutterTasks.new
-rescue LoadError
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
-end
-
require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
@@ -30,8 +13,6 @@ Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.rcov = true
end
-task :spec => :check_dependencies
-
task :default => :spec
require 'rake/rdoctask'
diff --git a/hashie.gemspec b/hashie.gemspec
index 5ac9c63..846bca4 100644
--- a/hashie.gemspec
+++ b/hashie.gemspec
@@ -1,11 +1,10 @@
-# Generated by jeweler
-# DO NOT EDIT THIS FILE DIRECTLY
-# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-
+require 'bundler'
+
Gem::Specification.new do |s|
s.name = %q{hashie}
- s.version = "0.2.0"
+ s.version = File.read("VERSION")
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Michael Bleigh"]
@@ -16,27 +15,8 @@ Gem::Specification.new do |s|
"LICENSE",
"README.rdoc"
]
- s.files = [
- ".document",
- ".gitignore",
- "LICENSE",
- "README.rdoc",
- "Rakefile",
- "VERSION",
- "hashie.gemspec",
- "lib/hashie.rb",
- "lib/hashie/clash.rb",
- "lib/hashie/dash.rb",
- "lib/hashie/hash.rb",
- "lib/hashie/hash_extensions.rb",
- "lib/hashie/mash.rb",
- "spec/hashie/clash_spec.rb",
- "spec/hashie/dash_spec.rb",
- "spec/hashie/hash_spec.rb",
- "spec/hashie/mash_spec.rb",
- "spec/spec.opts",
- "spec/spec_helper.rb"
- ]
+ s.files = Dir["**/*"]
+ s.add_bundler_dependencies
s.homepage = %q{http://github.com/intridea/hashie}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
@@ -49,18 +29,5 @@ Gem::Specification.new do |s|
"spec/hashie/mash_spec.rb",
"spec/spec_helper.rb"
]
-
- if s.respond_to? :specification_version then
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
- s.specification_version = 3
-
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
- s.add_development_dependency(%q<rspec>, [">= 0"])
- else
- s.add_dependency(%q<rspec>, [">= 0"])
- end
- else
- s.add_dependency(%q<rspec>, [">= 0"])
- end
end
diff --git a/lib/hashie/hash.rb b/lib/hashie/hash.rb
index d13a58b..906563c 100644
--- a/lib/hashie/hash.rb
+++ b/lib/hashie/hash.rb
@@ -4,5 +4,19 @@ module Hashie
# not be available in all libraries.
class Hash < Hash
include Hashie::HashExtensions
+
+ # Converts a mash back to a hash (with stringified keys)
+ def to_hash
+ out = {}
+ keys.each do |k|
+ out[k] = Hashie::Hash === self[k] ? self[k].to_hash : self[k]
+ end
+ out
+ end
+
+ # The C geneartor for the json gem doesn't like mashies
+ def to_json
+ to_hash.to_json
+ end
end
-end \ No newline at end of file
+end
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
index 69df491..4a196d6 100644
--- a/lib/hashie/mash.rb
+++ b/lib/hashie/mash.rb
@@ -111,12 +111,8 @@ module Hashie
alias_method :update, :deep_update
alias_method :merge!, :update
- # Converts a mash back to a hash (with stringified keys)
- def to_hash
- Hash.new(default).merge(self)
- end
- def method_missing(method_name, *args)
+ def method_missing(method_name, *args, &blk)
return self[method_name] if key?(method_name)
match = method_name.to_s.match(/(.*?)([?=!]?)$/)
case match[2]
@@ -127,7 +123,7 @@ module Hashie
when "!"
initializing_reader(match[1])
else
- default(method_name)
+ default(method_name, *args, &blk)
end
end
diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb
index 7078353..70d1f6f 100644
--- a/spec/hashie/mash_spec.rb
+++ b/spec/hashie/mash_spec.rb
@@ -4,62 +4,62 @@ describe Hashie::Mash do
before(:each) do
@mash = Hashie::Mash.new
end
-
+
it "should inherit from hash" do
@mash.is_a?(Hash).should be_true
end
-
+
it "should be able to set hash values through method= calls" do
@mash.test = "abc"
@mash["test"].should == "abc"
end
-
+
it "should be able to retrieve set values through method calls" do
@mash["test"] = "abc"
@mash.test.should == "abc"
end
-
+
it "should test for already set values when passed a ? method" do
@mash.test?.should be_false
@mash.test = "abc"
@mash.test?.should be_true
end
-
+
it "should make all [] and []= into strings for consistency" do
@mash["abc"] = 123
@mash.key?('abc').should be_true
@mash["abc"].should == 123
end
-
+
it "should have a to_s that is identical to its inspect" do
@mash.abc = 123
@mash.to_s.should == @mash.inspect
end
-
+
it "should return nil instead of raising an error for attribute-esque method calls" do
@mash.abc.should be_nil
end
-
+
it "should return a Hashie::Mash when passed a bang method to a non-existenct key" do
@mash.abc!.is_a?(Hashie::Mash).should be_true
end
-
+
it "should return the existing value when passed a bang method for an existing key" do
@mash.name = "Bob"
@mash.name!.should == "Bob"
end
-
+
it "#initializing_reader should return a Hashie::Mash when passed a non-existent key" do
@mash.initializing_reader(:abc).is_a?(Hashie::Mash).should be_true
end
-
+
it "should allow for multi-level assignment through bang methods" do
@mash.author!.name = "Michael Bleigh"
@mash.author.should == Hashie::Mash.new(:name => "Michael Bleigh")
@mash.author!.website!.url = "http://www.mbleigh.com/"
@mash.author.website.should == Hashie::Mash.new(:url => "http://www.mbleigh.com/")
end
-
+
it "#deep_update should recursively Hashie::Mash Hashie::Mashes and hashes together" do
@mash.first_name = "Michael"
@mash.last_name = "Bleigh"
@@ -67,53 +67,53 @@ describe Hashie::Mash do
@mash.deep_update({:details => {:email => "michael@intridea.com"}})
@mash.details.email.should == "michael@intridea.com"
end
-
+
it "should convert hash assignments into Hashie::Mashes" do
@mash.details = {:email => 'randy@asf.com', :address => {:state => 'TX'} }
@mash.details.email.should == 'randy@asf.com'
@mash.details.address.state.should == 'TX'
end
-
+
it "should not convert the type of Hashie::Mashes childs to Hashie::Mash" do
class MyMash < Hashie::Mash
end
-
+
record = MyMash.new
record.son = MyMash.new
record.son.class.should == MyMash
end
-
+
context "#initialize" do
it "should convert an existing hash to a Hashie::Mash" do
converted = Hashie::Mash.new({:abc => 123, :name => "Bob"})
converted.abc.should == 123
converted.name.should == "Bob"
end
-
+
it "should convert hashes recursively into Hashie::Mashes" do
converted = Hashie::Mash.new({:a => {:b => 1, :c => {:d => 23}}})
converted.a.is_a?(Hashie::Mash).should be_true
converted.a.b.should == 1
converted.a.c.d.should == 23
end
-
+
it "should convert hashes in arrays into Hashie::Mashes" do
converted = Hashie::Mash.new({:a => [{:b => 12}, 23]})
converted.a.first.b.should == 12
converted.a.last.should == 23
end
-
+
it "should convert an existing Hashie::Mash into a Hashie::Mash" do
initial = Hashie::Mash.new(:name => 'randy', :address => {:state => 'TX'})
copy = Hashie::Mash.new(initial)
- initial.name.should == copy.name
+ initial.name.should == copy.name
initial.object_id.should_not == copy.object_id
copy.address.state.should == 'TX'
copy.address.state = 'MI'
initial.address.state.should == 'TX'
copy.address.object_id.should_not == initial.address.object_id
end
-
+
it "should accept a default block" do
initial = Hashie::Mash.new { |h,i| h[i] = []}
initial.default_proc.should_not be_nil
@@ -121,5 +121,15 @@ describe Hashie::Mash do
initial.test.should == []
initial.test?.should be_true
end
+
+ describe "to_json" do
+
+ it "should render to_json" do
+ @mash.foo = :bar
+ @mash.bar = {"homer" => "simpson"}
+ expected = {"foo" => "bar", "bar" => {"homer" => "simpson"}}
+ JSON.parse(@mash.to_json).should == expected
+ end
+ end
end
-end \ No newline at end of file
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 5ba09a8..d20e990 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,3 +1,6 @@
+require 'rubygems'
+require 'json'
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -6,5 +9,5 @@ require 'spec'
require 'spec/autorun'
Spec::Runner.configure do |config|
-
+
end