mostash 0.0.2 → 0.0.3
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/.gitignore +2 -0
- data/VERSION +1 -1
- data/lib/mostash.rb +1 -0
- data/lib/mostash/mostash.rb +10 -8
- data/spec/as_hash_spec.rb +101 -42
- data/spec/mostash_spec.rb +2 -2
- metadata +51 -43
data/.gitignore
ADDED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/lib/mostash.rb
CHANGED
data/lib/mostash/mostash.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
class
|
1
|
+
class Mostash < OpenStruct
|
2
2
|
def initialize(init={})
|
3
3
|
super({})
|
4
4
|
__init__ init
|
5
5
|
end
|
6
6
|
|
7
|
-
def method_missing(method_name, *args)
|
8
|
-
#dbg "#{method_name} was sent #{args.inspect}"
|
9
|
-
if
|
7
|
+
def method_missing(method_name, *args, &block)
|
8
|
+
#dbg "#{method_name} was sent #{args.inspect}, and block #{block.inspect}"
|
9
|
+
if @table.respond_to? method_name
|
10
|
+
@table.send method_name, *args, &block
|
11
|
+
elsif __is_setter__( method_name )
|
10
12
|
super method_name, __adjusted_value__( args.first )
|
11
13
|
else
|
12
14
|
super
|
@@ -23,13 +25,13 @@ class MoStash < OpenStruct
|
|
23
25
|
|
24
26
|
def merge(new_hash)
|
25
27
|
new_mo = @table.merge( new_hash ) do |key, oldval, newval|
|
26
|
-
if oldval.class ==
|
28
|
+
if oldval.class == Mostash
|
27
29
|
oldval.merge newval
|
28
30
|
else
|
29
31
|
newval
|
30
32
|
end
|
31
33
|
end
|
32
|
-
|
34
|
+
Mostash.new( new_mo )
|
33
35
|
end
|
34
36
|
|
35
37
|
#TODO: HACK!!!!!
|
@@ -41,7 +43,7 @@ class MoStash < OpenStruct
|
|
41
43
|
def to_hash
|
42
44
|
hash = {}
|
43
45
|
@table.each_pair do |key, value|
|
44
|
-
hash[key] = if value.class ==
|
46
|
+
hash[key] = if value.class == Mostash
|
45
47
|
value.to_hash
|
46
48
|
else
|
47
49
|
value
|
@@ -60,7 +62,7 @@ class MoStash < OpenStruct
|
|
60
62
|
|
61
63
|
def __adjusted_value__(value)
|
62
64
|
case value
|
63
|
-
when Hash then
|
65
|
+
when Hash then Mostash.new( value )
|
64
66
|
when Array then value.map{ |v| __adjusted_value__( v ) }
|
65
67
|
else value
|
66
68
|
end
|
data/spec/as_hash_spec.rb
CHANGED
@@ -1,61 +1,120 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe 'MoStash as Hash' do
|
4
|
-
it "should support merge when single level" do
|
5
|
-
mo = MoStash.new :foo => 'bar', :hello => 'world'
|
6
|
-
|
7
|
-
new_mo = mo.merge( :hello => 'tester' )
|
8
4
|
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
context 'to_hash' do
|
6
|
+
it "should be able to return a hash for single level" do
|
7
|
+
mo = MoStash.new( :foo => 'bar' )
|
8
|
+
|
9
|
+
mo.to_hash.should == { :foo => 'bar' }
|
10
|
+
end
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
new_mo = mo.merge( :foo => 'baz' )
|
12
|
+
it "should be able to return a hash for multi level" do
|
13
|
+
mo = MoStash.new( :foo => 'bar', :nested => { :hello => 'world' } )
|
17
14
|
|
18
|
-
|
19
|
-
|
15
|
+
mo.to_hash.should == { :foo => 'bar', :nested => { :hello => 'world' } }
|
16
|
+
end
|
20
17
|
end
|
21
18
|
|
22
|
-
|
23
|
-
mo = MoStash.new( { :foo => 'bar', :nested => { :hello => 'world' }} )
|
24
|
-
|
25
|
-
new_mo = mo.merge( :nested => { :hello => 'tester' } )
|
19
|
+
context 'hash functions' do
|
26
20
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
21
|
+
it "should respond to empty?" do
|
22
|
+
mo = Mostash.new
|
23
|
+
mo.empty?.should == true
|
24
|
+
end
|
31
25
|
|
32
|
-
|
33
|
-
|
26
|
+
it "should be able to delete a key/value" do
|
27
|
+
mo = Mostash.new :a => 100, :b => 200
|
34
28
|
|
35
|
-
|
36
|
-
|
37
|
-
|
29
|
+
mo.delete :a
|
30
|
+
mo.size.should == 1
|
31
|
+
mo[:a].should be_nil
|
32
|
+
end
|
38
33
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
mo.merge!( :nested => { :hello => 'tester' } )
|
34
|
+
it "should be clearable" do
|
35
|
+
mo = Mostash.new :a => 100, :b => 200
|
43
36
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
37
|
+
mo.clear
|
38
|
+
mo.size.should == 0
|
39
|
+
end
|
48
40
|
|
49
|
-
|
50
|
-
|
41
|
+
it "should be able to iterable via each_pair" do
|
42
|
+
mo = Mostash.new :a => 100, :b => 200
|
43
|
+
vals = []
|
44
|
+
mo.each_pair { |key, value| vals << "#{key} = #{value}" }
|
51
45
|
|
52
|
-
|
53
|
-
|
46
|
+
vals.size.should == 2
|
47
|
+
vals[0].should == "a = 100"
|
48
|
+
vals[1].should == "b = 200"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should be able to iterable via each_value" do
|
52
|
+
mo = Mostash.new :a => 100, :b => 200
|
53
|
+
vals = []
|
54
|
+
mo.each_value { |value| vals << value }
|
55
|
+
|
56
|
+
vals.size.should == 2
|
57
|
+
vals[0].should == 100
|
58
|
+
vals[1].should == 200
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should be iterable via each_key" do
|
62
|
+
mo = Mostash.new :a => 100, :b => 200
|
63
|
+
vals = []
|
64
|
+
mo.each_key { |key| vals << key }
|
65
|
+
|
66
|
+
vals.size.should == 2
|
67
|
+
vals[0].should == :a
|
68
|
+
vals[1].should == :b
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'merge' do
|
72
|
+
it "should support merge when single level" do
|
73
|
+
mo = MoStash.new :foo => 'bar', :hello => 'world'
|
74
|
+
|
75
|
+
new_mo = mo.merge( :hello => 'tester' )
|
76
|
+
|
77
|
+
new_mo[:foo].should == 'bar'
|
78
|
+
new_mo[:hello].should == 'tester'
|
79
|
+
end
|
54
80
|
|
55
|
-
|
56
|
-
|
81
|
+
it "should support merge of single level when multi leveled" do
|
82
|
+
mo = MoStash.new( { :foo => 'bar', :nested => { :hello => 'world' }} )
|
83
|
+
|
84
|
+
new_mo = mo.merge( :foo => 'baz' )
|
85
|
+
|
86
|
+
new_mo[:foo].should == 'baz'
|
87
|
+
new_mo[:nested][:hello].should == 'world'
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should support merge of multi levels when is multi leveled" do
|
91
|
+
mo = MoStash.new( { :foo => 'bar', :nested => { :hello => 'world' }} )
|
92
|
+
|
93
|
+
new_mo = mo.merge( :nested => { :hello => 'tester' } )
|
94
|
+
|
95
|
+
new_mo[:foo].should == 'bar'
|
96
|
+
new_mo[:nested][:hello].should == 'tester'
|
97
|
+
new_mo[:nested].class.should == MoStash
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should support merge!" do
|
101
|
+
mo = MoStash.new( :foo => 'bar' )
|
102
|
+
|
103
|
+
mo.merge!( :foo => 'baz' )
|
104
|
+
mo.foo.should == 'baz'
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should support multi level merge!" do
|
108
|
+
mo = MoStash.new( { :foo => 'bar', :nested => { :hello => 'world' }} )
|
109
|
+
|
110
|
+
mo.merge!( :nested => { :hello => 'tester' } )
|
111
|
+
|
112
|
+
mo[:foo].should == 'bar'
|
113
|
+
mo[:nested][:hello].should == 'tester'
|
114
|
+
mo[:nested].class.should == MoStash
|
115
|
+
end
|
116
|
+
end
|
57
117
|
|
58
|
-
mo.to_hash.should == { :foo => 'bar', :nested => { :hello => 'world' } }
|
59
118
|
end
|
60
|
-
end
|
61
119
|
|
120
|
+
end
|
data/spec/mostash_spec.rb
CHANGED
metadata
CHANGED
@@ -1,34 +1,37 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mostash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
version: 0.0.
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
|
-
|
13
|
+
- Joel Friedman
|
13
14
|
autorequire:
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-07-08 00:00:00 -05:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
32
35
|
description: You can treat an object as either a hash or as an OpenStruct. In additon to this you can create them nested, unlike OpenStruct
|
33
36
|
email: asher.friedman@gmail.com
|
34
37
|
executables: []
|
@@ -36,47 +39,52 @@ executables: []
|
|
36
39
|
extensions: []
|
37
40
|
|
38
41
|
extra_rdoc_files:
|
39
|
-
|
42
|
+
- README.markdown
|
40
43
|
files:
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
44
|
+
- .gitignore
|
45
|
+
- README.markdown
|
46
|
+
- Rakefile
|
47
|
+
- VERSION
|
48
|
+
- lib/mostash.rb
|
49
|
+
- lib/mostash/mostash.rb
|
50
|
+
- spec/as_hash_spec.rb
|
51
|
+
- spec/mostash_spec.rb
|
52
|
+
- spec/spec_helper.rb
|
49
53
|
has_rdoc: true
|
50
54
|
homepage:
|
51
55
|
licenses: []
|
52
56
|
|
53
57
|
post_install_message:
|
54
58
|
rdoc_options:
|
55
|
-
|
59
|
+
- --charset=UTF-8
|
56
60
|
require_paths:
|
57
|
-
|
61
|
+
- lib
|
58
62
|
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
59
64
|
requirements:
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
65
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
66
73
|
requirements:
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
72
80
|
requirements: []
|
73
81
|
|
74
82
|
rubyforge_project:
|
75
|
-
rubygems_version: 1.3.
|
83
|
+
rubygems_version: 1.3.7
|
76
84
|
signing_key:
|
77
85
|
specification_version: 3
|
78
86
|
summary: A combo of OpenStruct and a ruby hash
|
79
87
|
test_files:
|
80
|
-
|
81
|
-
|
82
|
-
|
88
|
+
- spec/as_hash_spec.rb
|
89
|
+
- spec/mostash_spec.rb
|
90
|
+
- spec/spec_helper.rb
|