Permalink
Browse files

Adds a fix for json generation

  • Loading branch information...
Daniel Neighman Michael Bleigh
Daniel Neighman authored and Michael Bleigh committed Jun 15, 2010
1 parent a026498 commit 0c85e9482f0ca0232d7cf7cf6577a359ea07ffed
Showing with 59 additions and 87 deletions.
  1. +1 −0 .gitignore
  2. +0 −19 Rakefile
  3. +5 −38 hashie.gemspec
  4. +15 −1 lib/hashie/hash.rb
  5. +2 −6 lib/hashie/mash.rb
  6. +32 −22 spec/hashie/mash_spec.rb
  7. +4 −1 spec/spec_helper.rb
View
@@ -4,3 +4,4 @@ coverage
rdoc
pkg
*.gem
+.bundle
View
@@ -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'
View
@@ -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
View
@@ -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
+end
View
@@ -111,12 +111,8 @@ def deep_update(other_hash)
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 @@ def method_missing(method_name, *args)
when "!"
initializing_reader(match[1])
else
- default(method_name)
+ default(method_name, *args, &blk)
end
end
View
@@ -4,122 +4,132 @@
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"
@mash.details = Hashie::Hash[:email => "michael@asf.com"].to_mash
@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
initial.default.should be_nil
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
+end
View
@@ -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/autorun'
Spec::Runner.configure do |config|
-
+
end

0 comments on commit 0c85e94

Please sign in to comment.