hashie 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +25 -0
- data/Rakefile +0 -2
- data/VERSION +1 -1
- data/hashie.gemspec +4 -2
- data/lib/hashie/hash.rb +15 -1
- data/lib/hashie/mash.rb +2 -6
- data/spec/hashie/mash_spec.rb +32 -22
- data/spec/spec_helper.rb +4 -1
- metadata +5 -3
data/.gitignore
CHANGED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
---
|
2
|
+
dependencies:
|
3
|
+
rake:
|
4
|
+
group:
|
5
|
+
- :development
|
6
|
+
version: ">= 0"
|
7
|
+
rspec:
|
8
|
+
group:
|
9
|
+
- :test
|
10
|
+
version: ">= 0"
|
11
|
+
json:
|
12
|
+
group:
|
13
|
+
- :development
|
14
|
+
version: ">= 0"
|
15
|
+
specs:
|
16
|
+
- rake:
|
17
|
+
version: 0.8.7
|
18
|
+
- json:
|
19
|
+
version: 1.4.3
|
20
|
+
- rspec:
|
21
|
+
version: 1.3.0
|
22
|
+
hash: d9c314ac1790a9ac25dfdaf7574b86d62b88f1d5
|
23
|
+
sources:
|
24
|
+
- Rubygems:
|
25
|
+
uri: http://gemcutter.org
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/hashie.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{hashie}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Michael Bleigh"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-06-22}
|
13
13
|
s.description = %q{Hashie is a small collection of tools that make hashes more powerful. Currently includes Mash (Mocking Hash) and Dash (Discrete Hash).}
|
14
14
|
s.email = %q{michael@intridea.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -19,6 +19,8 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".gitignore",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
22
24
|
"LICENSE",
|
23
25
|
"README.rdoc",
|
24
26
|
"Rakefile",
|
data/lib/hashie/hash.rb
CHANGED
@@ -4,5 +4,19 @@ module Hashie
|
|
4
4
|
# not be available in all libraries.
|
5
5
|
class Hash < Hash
|
6
6
|
include Hashie::HashExtensions
|
7
|
+
|
8
|
+
# Converts a mash back to a hash (with stringified keys)
|
9
|
+
def to_hash
|
10
|
+
out = {}
|
11
|
+
keys.each do |k|
|
12
|
+
out[k] = Hashie::Hash === self[k] ? self[k].to_hash : self[k]
|
13
|
+
end
|
14
|
+
out
|
15
|
+
end
|
16
|
+
|
17
|
+
# The C geneartor for the json gem doesn't like mashies
|
18
|
+
def to_json(*args)
|
19
|
+
to_hash.to_json(*args)
|
20
|
+
end
|
7
21
|
end
|
8
|
-
end
|
22
|
+
end
|
data/lib/hashie/mash.rb
CHANGED
@@ -111,12 +111,8 @@ module Hashie
|
|
111
111
|
alias_method :update, :deep_update
|
112
112
|
alias_method :merge!, :update
|
113
113
|
|
114
|
-
# Converts a mash back to a hash (with stringified keys)
|
115
|
-
def to_hash
|
116
|
-
Hash.new(default).merge(self)
|
117
|
-
end
|
118
114
|
|
119
|
-
def method_missing(method_name, *args)
|
115
|
+
def method_missing(method_name, *args, &blk)
|
120
116
|
return self[method_name] if key?(method_name)
|
121
117
|
match = method_name.to_s.match(/(.*?)([?=!]?)$/)
|
122
118
|
case match[2]
|
@@ -127,7 +123,7 @@ module Hashie
|
|
127
123
|
when "!"
|
128
124
|
initializing_reader(match[1])
|
129
125
|
else
|
130
|
-
default(method_name)
|
126
|
+
default(method_name, *args, &blk)
|
131
127
|
end
|
132
128
|
end
|
133
129
|
|
data/spec/hashie/mash_spec.rb
CHANGED
@@ -4,62 +4,62 @@ describe Hashie::Mash do
|
|
4
4
|
before(:each) do
|
5
5
|
@mash = Hashie::Mash.new
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
it "should inherit from hash" do
|
9
9
|
@mash.is_a?(Hash).should be_true
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
it "should be able to set hash values through method= calls" do
|
13
13
|
@mash.test = "abc"
|
14
14
|
@mash["test"].should == "abc"
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
it "should be able to retrieve set values through method calls" do
|
18
18
|
@mash["test"] = "abc"
|
19
19
|
@mash.test.should == "abc"
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
it "should test for already set values when passed a ? method" do
|
23
23
|
@mash.test?.should be_false
|
24
24
|
@mash.test = "abc"
|
25
25
|
@mash.test?.should be_true
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
it "should make all [] and []= into strings for consistency" do
|
29
29
|
@mash["abc"] = 123
|
30
30
|
@mash.key?('abc').should be_true
|
31
31
|
@mash["abc"].should == 123
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
it "should have a to_s that is identical to its inspect" do
|
35
35
|
@mash.abc = 123
|
36
36
|
@mash.to_s.should == @mash.inspect
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
it "should return nil instead of raising an error for attribute-esque method calls" do
|
40
40
|
@mash.abc.should be_nil
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
it "should return a Hashie::Mash when passed a bang method to a non-existenct key" do
|
44
44
|
@mash.abc!.is_a?(Hashie::Mash).should be_true
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
it "should return the existing value when passed a bang method for an existing key" do
|
48
48
|
@mash.name = "Bob"
|
49
49
|
@mash.name!.should == "Bob"
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
it "#initializing_reader should return a Hashie::Mash when passed a non-existent key" do
|
53
53
|
@mash.initializing_reader(:abc).is_a?(Hashie::Mash).should be_true
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
it "should allow for multi-level assignment through bang methods" do
|
57
57
|
@mash.author!.name = "Michael Bleigh"
|
58
58
|
@mash.author.should == Hashie::Mash.new(:name => "Michael Bleigh")
|
59
59
|
@mash.author!.website!.url = "http://www.mbleigh.com/"
|
60
60
|
@mash.author.website.should == Hashie::Mash.new(:url => "http://www.mbleigh.com/")
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
it "#deep_update should recursively Hashie::Mash Hashie::Mashes and hashes together" do
|
64
64
|
@mash.first_name = "Michael"
|
65
65
|
@mash.last_name = "Bleigh"
|
@@ -67,53 +67,53 @@ describe Hashie::Mash do
|
|
67
67
|
@mash.deep_update({:details => {:email => "michael@intridea.com"}})
|
68
68
|
@mash.details.email.should == "michael@intridea.com"
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
it "should convert hash assignments into Hashie::Mashes" do
|
72
72
|
@mash.details = {:email => 'randy@asf.com', :address => {:state => 'TX'} }
|
73
73
|
@mash.details.email.should == 'randy@asf.com'
|
74
74
|
@mash.details.address.state.should == 'TX'
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
it "should not convert the type of Hashie::Mashes childs to Hashie::Mash" do
|
78
78
|
class MyMash < Hashie::Mash
|
79
79
|
end
|
80
|
-
|
80
|
+
|
81
81
|
record = MyMash.new
|
82
82
|
record.son = MyMash.new
|
83
83
|
record.son.class.should == MyMash
|
84
84
|
end
|
85
|
-
|
85
|
+
|
86
86
|
context "#initialize" do
|
87
87
|
it "should convert an existing hash to a Hashie::Mash" do
|
88
88
|
converted = Hashie::Mash.new({:abc => 123, :name => "Bob"})
|
89
89
|
converted.abc.should == 123
|
90
90
|
converted.name.should == "Bob"
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
it "should convert hashes recursively into Hashie::Mashes" do
|
94
94
|
converted = Hashie::Mash.new({:a => {:b => 1, :c => {:d => 23}}})
|
95
95
|
converted.a.is_a?(Hashie::Mash).should be_true
|
96
96
|
converted.a.b.should == 1
|
97
97
|
converted.a.c.d.should == 23
|
98
98
|
end
|
99
|
-
|
99
|
+
|
100
100
|
it "should convert hashes in arrays into Hashie::Mashes" do
|
101
101
|
converted = Hashie::Mash.new({:a => [{:b => 12}, 23]})
|
102
102
|
converted.a.first.b.should == 12
|
103
103
|
converted.a.last.should == 23
|
104
104
|
end
|
105
|
-
|
105
|
+
|
106
106
|
it "should convert an existing Hashie::Mash into a Hashie::Mash" do
|
107
107
|
initial = Hashie::Mash.new(:name => 'randy', :address => {:state => 'TX'})
|
108
108
|
copy = Hashie::Mash.new(initial)
|
109
|
-
initial.name.should == copy.name
|
109
|
+
initial.name.should == copy.name
|
110
110
|
initial.object_id.should_not == copy.object_id
|
111
111
|
copy.address.state.should == 'TX'
|
112
112
|
copy.address.state = 'MI'
|
113
113
|
initial.address.state.should == 'TX'
|
114
114
|
copy.address.object_id.should_not == initial.address.object_id
|
115
115
|
end
|
116
|
-
|
116
|
+
|
117
117
|
it "should accept a default block" do
|
118
118
|
initial = Hashie::Mash.new { |h,i| h[i] = []}
|
119
119
|
initial.default_proc.should_not be_nil
|
@@ -121,5 +121,15 @@ describe Hashie::Mash do
|
|
121
121
|
initial.test.should == []
|
122
122
|
initial.test?.should be_true
|
123
123
|
end
|
124
|
+
|
125
|
+
describe "to_json" do
|
126
|
+
|
127
|
+
it "should render to_json" do
|
128
|
+
@mash.foo = :bar
|
129
|
+
@mash.bar = {"homer" => "simpson"}
|
130
|
+
expected = {"foo" => "bar", "bar" => {"homer" => "simpson"}}
|
131
|
+
JSON.parse(@mash.to_json).should == expected
|
132
|
+
end
|
133
|
+
end
|
124
134
|
end
|
125
|
-
end
|
135
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'json'
|
3
|
+
|
1
4
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
5
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
6
|
|
@@ -6,5 +9,5 @@ require 'spec'
|
|
6
9
|
require 'spec/autorun'
|
7
10
|
|
8
11
|
Spec::Runner.configure do |config|
|
9
|
-
|
12
|
+
|
10
13
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Michael Bleigh
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-06-22 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -41,6 +41,8 @@ extra_rdoc_files:
|
|
41
41
|
files:
|
42
42
|
- .document
|
43
43
|
- .gitignore
|
44
|
+
- Gemfile
|
45
|
+
- Gemfile.lock
|
44
46
|
- LICENSE
|
45
47
|
- README.rdoc
|
46
48
|
- Rakefile
|