mostash 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mostash.rb +3 -2
- data/lib/mostash/mostash.rb +29 -0
- data/spec/as_hash_spec.rb +61 -0
- metadata +42 -40
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/mostash.rb
CHANGED
@@ -3,6 +3,7 @@ require 'ostruct'
|
|
3
3
|
require File.join(File.dirname(__FILE__), "mostash", "mostash")
|
4
4
|
|
5
5
|
def dbg( msg )
|
6
|
-
|
7
|
-
|
6
|
+
file, line, method_raw = caller[0].split('/').last.split(':')
|
7
|
+
method = method_raw.match(/^in `(.+)'/)[1]
|
8
|
+
puts "#{method} (#{file}##{line}): #{msg}"
|
8
9
|
end
|
data/lib/mostash/mostash.rb
CHANGED
@@ -21,6 +21,35 @@ class MoStash < OpenStruct
|
|
21
21
|
self.send "#{key.to_s}"
|
22
22
|
end
|
23
23
|
|
24
|
+
def merge(new_hash)
|
25
|
+
new_mo = @table.merge( new_hash ) do |key, oldval, newval|
|
26
|
+
if oldval.class == MoStash
|
27
|
+
oldval.merge newval
|
28
|
+
else
|
29
|
+
newval
|
30
|
+
end
|
31
|
+
end
|
32
|
+
MoStash.new( new_mo )
|
33
|
+
end
|
34
|
+
|
35
|
+
#TODO: HACK!!!!!
|
36
|
+
def merge!(new_hash)
|
37
|
+
@table = self.merge( new_hash ).instance_variable_get( '@table' )
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_hash
|
42
|
+
hash = {}
|
43
|
+
@table.each_pair do |key, value|
|
44
|
+
hash[key] = if value.class == MoStash
|
45
|
+
value.to_hash
|
46
|
+
else
|
47
|
+
value
|
48
|
+
end
|
49
|
+
end
|
50
|
+
hash
|
51
|
+
end
|
52
|
+
|
24
53
|
private
|
25
54
|
|
26
55
|
def __init__(hash)
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
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
|
+
|
9
|
+
new_mo[:foo].should == 'bar'
|
10
|
+
new_mo[:hello].should == 'tester'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should support merge of single level when multi leveled" do
|
14
|
+
mo = MoStash.new( { :foo => 'bar', :nested => { :hello => 'world' }} )
|
15
|
+
|
16
|
+
new_mo = mo.merge( :foo => 'baz' )
|
17
|
+
|
18
|
+
new_mo[:foo].should == 'baz'
|
19
|
+
new_mo[:nested][:hello].should == 'world'
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should support merge of multi levels when is multi leveled" do
|
23
|
+
mo = MoStash.new( { :foo => 'bar', :nested => { :hello => 'world' }} )
|
24
|
+
|
25
|
+
new_mo = mo.merge( :nested => { :hello => 'tester' } )
|
26
|
+
|
27
|
+
new_mo[:foo].should == 'bar'
|
28
|
+
new_mo[:nested][:hello].should == 'tester'
|
29
|
+
new_mo[:nested].class.should == MoStash
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should support merge!" do
|
33
|
+
mo = MoStash.new( :foo => 'bar' )
|
34
|
+
|
35
|
+
mo.merge!( :foo => 'baz' )
|
36
|
+
mo.foo.should == 'baz'
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should support multi level merge!" do
|
40
|
+
mo = MoStash.new( { :foo => 'bar', :nested => { :hello => 'world' }} )
|
41
|
+
|
42
|
+
mo.merge!( :nested => { :hello => 'tester' } )
|
43
|
+
|
44
|
+
mo[:foo].should == 'bar'
|
45
|
+
mo[:nested][:hello].should == 'tester'
|
46
|
+
mo[:nested].class.should == MoStash
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should be able to return a hash for single level" do
|
50
|
+
mo = MoStash.new( :foo => 'bar' )
|
51
|
+
|
52
|
+
mo.to_hash.should == { :foo => 'bar' }
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should be able to return a hash for multi level" do
|
56
|
+
mo = MoStash.new( :foo => 'bar', :nested => { :hello => 'world' } )
|
57
|
+
|
58
|
+
mo.to_hash.should == { :foo => 'bar', :nested => { :hello => 'world' } }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
metadata
CHANGED
@@ -3,32 +3,32 @@ name: mostash
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
version: 0.0.
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
|
-
- Joel Friedman
|
12
|
+
- Joel Friedman
|
13
13
|
autorequire:
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-05 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
32
|
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
33
|
email: asher.friedman@gmail.com
|
34
34
|
executables: []
|
@@ -36,38 +36,39 @@ executables: []
|
|
36
36
|
extensions: []
|
37
37
|
|
38
38
|
extra_rdoc_files:
|
39
|
-
- README.markdown
|
39
|
+
- README.markdown
|
40
40
|
files:
|
41
|
-
- README.markdown
|
42
|
-
- Rakefile
|
43
|
-
- VERSION
|
44
|
-
- lib/mostash.rb
|
45
|
-
- lib/mostash/mostash.rb
|
46
|
-
- spec/
|
47
|
-
- spec/
|
41
|
+
- README.markdown
|
42
|
+
- Rakefile
|
43
|
+
- VERSION
|
44
|
+
- lib/mostash.rb
|
45
|
+
- lib/mostash/mostash.rb
|
46
|
+
- spec/as_hash_spec.rb
|
47
|
+
- spec/mostash_spec.rb
|
48
|
+
- spec/spec_helper.rb
|
48
49
|
has_rdoc: true
|
49
50
|
homepage:
|
50
51
|
licenses: []
|
51
52
|
|
52
53
|
post_install_message:
|
53
54
|
rdoc_options:
|
54
|
-
- --charset=UTF-8
|
55
|
+
- --charset=UTF-8
|
55
56
|
require_paths:
|
56
|
-
- lib
|
57
|
+
- lib
|
57
58
|
required_ruby_version: !ruby/object:Gem::Requirement
|
58
59
|
requirements:
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
64
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
66
|
requirements:
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
71
72
|
requirements: []
|
72
73
|
|
73
74
|
rubyforge_project:
|
@@ -76,5 +77,6 @@ signing_key:
|
|
76
77
|
specification_version: 3
|
77
78
|
summary: A combo of OpenStruct and a ruby hash
|
78
79
|
test_files:
|
79
|
-
- spec/
|
80
|
-
- spec/
|
80
|
+
- spec/as_hash_spec.rb
|
81
|
+
- spec/mostash_spec.rb
|
82
|
+
- spec/spec_helper.rb
|