mash 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +6 -3
- data/README.txt +8 -1
- data/lib/mash.rb +28 -4
- data/spec/mash_spec.rb +20 -0
- metadata +1 -1
data/History.txt
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
-
===
|
1
|
+
=== 0.0.2 / 2008-04-12
|
2
2
|
|
3
|
-
*
|
3
|
+
* Added bang(!) method support
|
4
|
+
* No longer automatically multi-level assigning
|
5
|
+
* Hash conversion now calls methods instead of []= to allow for overrides
|
4
6
|
|
5
|
-
|
7
|
+
=== 0.0.1 / 2008-04-12
|
6
8
|
|
9
|
+
* Initial release
|
data/README.txt
CHANGED
@@ -10,13 +10,19 @@ be used in RESTful API libraries to provide easy object-like access
|
|
10
10
|
to JSON and XML parsed hashes.
|
11
11
|
|
12
12
|
== SYNOPSIS:
|
13
|
-
|
13
|
+
|
14
14
|
mash = Mash.new
|
15
15
|
mash.name? # => false
|
16
|
+
mash.name # => nil
|
16
17
|
mash.name = "My Mash"
|
17
18
|
mash.name # => "My Mash"
|
18
19
|
mash.name? # => true
|
19
20
|
mash.inspect # => <Mash name="My Mash">
|
21
|
+
|
22
|
+
mash = Mash.new
|
23
|
+
# use bang methods for multi-level assignment
|
24
|
+
mash.author!.name = "Michael Bleigh"
|
25
|
+
mash.author # => <Mash name="Michael Bleigh">
|
20
26
|
|
21
27
|
== INSTALL:
|
22
28
|
|
@@ -25,6 +31,7 @@ RubyGem:
|
|
25
31
|
sudo gem install mash
|
26
32
|
|
27
33
|
Git:
|
34
|
+
|
28
35
|
git clone git://github.com/mbleigh/mash
|
29
36
|
|
30
37
|
== LICENSE:
|
data/lib/mash.rb
CHANGED
@@ -4,6 +4,14 @@
|
|
4
4
|
# without the overhead of actually doing so. Think of it as OpenStruct
|
5
5
|
# with some additional goodies.
|
6
6
|
#
|
7
|
+
# A Mash will look at the methods you pass it and perform operations
|
8
|
+
# based on the following rules:
|
9
|
+
#
|
10
|
+
# * No punctuation: Returns the value of the hash for that key, or nil if none exists.
|
11
|
+
# * Assignment (<tt>=</tt>): Sets the attribute of the given method name.
|
12
|
+
# * Existence (<tt>?</tt>): Returns true or false depending on whether that key has been set.
|
13
|
+
# * Bang (<tt>!</tt>): Forces the existence of this key, used for deep Mashes. Think of it as "touch" for mashes.
|
14
|
+
#
|
7
15
|
# == Basic Example
|
8
16
|
#
|
9
17
|
# mash = Mash.new
|
@@ -21,8 +29,18 @@
|
|
21
29
|
# mash.f.first.g # => 44
|
22
30
|
# mash.f.last # => 12
|
23
31
|
#
|
32
|
+
# == Bang Example
|
33
|
+
#
|
34
|
+
# mash = Mash.new
|
35
|
+
# mash.author # => nil
|
36
|
+
# mash.author! # => <Mash>
|
37
|
+
#
|
38
|
+
# mash = Mash.new
|
39
|
+
# mash.author!.name = "Michael Bleigh"
|
40
|
+
# mash.author # => <Mash name="Michael Bleigh">
|
41
|
+
#
|
24
42
|
class Mash < Hash
|
25
|
-
VERSION = '0.0.
|
43
|
+
VERSION = '0.0.2'
|
26
44
|
|
27
45
|
# If you pass in an existing hash, it will
|
28
46
|
# convert it to a Mash including recursively
|
@@ -66,16 +84,19 @@ class Mash < Hash
|
|
66
84
|
self[match[1]] = args.first
|
67
85
|
elsif (match = method_name.to_s.match(/(.*)\?$/)) && args.size == 0
|
68
86
|
key?(match[1])
|
87
|
+
elsif (match = method_name.to_s.match(/(.*)!$/)) && args.size == 0
|
88
|
+
return self[match[1]] if key?(match[1])
|
89
|
+
self[match[1]] = Mash.new
|
69
90
|
elsif keys.include?(method_name.to_s)
|
70
91
|
self[method_name]
|
71
92
|
elsif match = method_name.to_s.match(/^([a-z][a-z0-9A-Z_]+)$/)
|
72
|
-
|
93
|
+
nil
|
73
94
|
else
|
74
95
|
super
|
75
96
|
end
|
76
97
|
end
|
77
98
|
|
78
|
-
|
99
|
+
protected
|
79
100
|
|
80
101
|
def mash_a_hash(hash) #:nodoc:
|
81
102
|
hash.each do |k,v|
|
@@ -86,7 +107,10 @@ class Mash < Hash
|
|
86
107
|
v = collect_mashed_hashes_in(v) if v.is_a?(Array)
|
87
108
|
end
|
88
109
|
|
89
|
-
|
110
|
+
# we use the method call instead of []= here so that
|
111
|
+
# it can be easily overridden for custom behavior in
|
112
|
+
# inheriting objects
|
113
|
+
self.send "#{k.to_s}=", v
|
90
114
|
end
|
91
115
|
end
|
92
116
|
|
data/spec/mash_spec.rb
CHANGED
@@ -37,6 +37,26 @@ describe Mash do
|
|
37
37
|
@mash.to_s.should == @mash.inspect
|
38
38
|
end
|
39
39
|
|
40
|
+
it "should return nil instead of raising an error for attribute-esque method calls" do
|
41
|
+
@mash.abc.should be_nil
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return a Mash when passed a bang method to a non-existenct key" do
|
45
|
+
@mash.abc!.is_a?(Mash).should be_true
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return the existing value when passed a bang method for an existing key" do
|
49
|
+
@mash.name = "Bob"
|
50
|
+
@mash.name!.should == "Bob"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should allow for multi-level assignment through bang methods" do
|
54
|
+
@mash.author!.name = "Michael Bleigh"
|
55
|
+
@mash.author.should == Mash.new(:name => "Michael Bleigh")
|
56
|
+
@mash.author!.website!.url = "http://www.mbleigh.com/"
|
57
|
+
@mash.author.website.should == Mash.new(:url => "http://www.mbleigh.com/")
|
58
|
+
end
|
59
|
+
|
40
60
|
context "#initialize" do
|
41
61
|
it "should convert an existing hash to a Mash" do
|
42
62
|
converted = Mash.new({:abc => 123, :name => "Bob"})
|