mostash 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ pkg/*
2
+ *.gemspec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -1,6 +1,7 @@
1
1
  require 'ostruct'
2
2
 
3
3
  require File.join(File.dirname(__FILE__), "mostash", "mostash")
4
+ MoStash = Mostash
4
5
 
5
6
  def dbg( msg )
6
7
  file, line, method_raw = caller[0].split('/').last.split(':')
@@ -1,12 +1,14 @@
1
- class MoStash < OpenStruct
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 __is_setter__( method_name )
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 == MoStash
28
+ if oldval.class == Mostash
27
29
  oldval.merge newval
28
30
  else
29
31
  newval
30
32
  end
31
33
  end
32
- MoStash.new( new_mo )
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 == MoStash
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 MoStash.new( value )
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
@@ -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
- new_mo[:foo].should == 'bar'
10
- new_mo[:hello].should == 'tester'
11
- end
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
- 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' )
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
- new_mo[:foo].should == 'baz'
19
- new_mo[:nested][:hello].should == 'world'
15
+ mo.to_hash.should == { :foo => 'bar', :nested => { :hello => 'world' } }
16
+ end
20
17
  end
21
18
 
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' } )
19
+ context 'hash functions' do
26
20
 
27
- new_mo[:foo].should == 'bar'
28
- new_mo[:nested][:hello].should == 'tester'
29
- new_mo[:nested].class.should == MoStash
30
- end
21
+ it "should respond to empty?" do
22
+ mo = Mostash.new
23
+ mo.empty?.should == true
24
+ end
31
25
 
32
- it "should support merge!" do
33
- mo = MoStash.new( :foo => 'bar' )
26
+ it "should be able to delete a key/value" do
27
+ mo = Mostash.new :a => 100, :b => 200
34
28
 
35
- mo.merge!( :foo => 'baz' )
36
- mo.foo.should == 'baz'
37
- end
29
+ mo.delete :a
30
+ mo.size.should == 1
31
+ mo[:a].should be_nil
32
+ end
38
33
 
39
- it "should support multi level merge!" do
40
- mo = MoStash.new( { :foo => 'bar', :nested => { :hello => 'world' }} )
41
-
42
- mo.merge!( :nested => { :hello => 'tester' } )
34
+ it "should be clearable" do
35
+ mo = Mostash.new :a => 100, :b => 200
43
36
 
44
- mo[:foo].should == 'bar'
45
- mo[:nested][:hello].should == 'tester'
46
- mo[:nested].class.should == MoStash
47
- end
37
+ mo.clear
38
+ mo.size.should == 0
39
+ end
48
40
 
49
- it "should be able to return a hash for single level" do
50
- mo = MoStash.new( :foo => 'bar' )
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
- mo.to_hash.should == { :foo => 'bar' }
53
- end
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
- it "should be able to return a hash for multi level" do
56
- mo = MoStash.new( :foo => 'bar', :nested => { :hello => 'world' } )
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
@@ -54,10 +54,10 @@ describe MoStash do
54
54
  end
55
55
 
56
56
  it "should create method when new method called" do
57
- mo = MoStash.new
57
+ mo = Mostash.new
58
58
  mo.foo = "bar"
59
59
 
60
- mo.methods.should include("foo")
60
+ mo.methods.should include(:foo)
61
61
  end
62
62
 
63
63
  end
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
- - 0
7
- - 0
8
- - 2
9
- version: 0.0.2
7
+ - 0
8
+ - 0
9
+ - 3
10
+ version: 0.0.3
10
11
  platform: ruby
11
12
  authors:
12
- - Joel Friedman
13
+ - Joel Friedman
13
14
  autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-05-05 00:00:00 -05:00
18
+ date: 2010-07-08 00:00:00 -05:00
18
19
  default_executable:
19
20
  dependencies:
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
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
- - README.markdown
42
+ - README.markdown
40
43
  files:
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
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
- - --charset=UTF-8
59
+ - --charset=UTF-8
56
60
  require_paths:
57
- - lib
61
+ - lib
58
62
  required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
59
64
  requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- segments:
63
- - 0
64
- version: "0"
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
- - !ruby/object:Gem::Version
69
- segments:
70
- - 0
71
- version: "0"
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.6
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
- - spec/as_hash_spec.rb
81
- - spec/mostash_spec.rb
82
- - spec/spec_helper.rb
88
+ - spec/as_hash_spec.rb
89
+ - spec/mostash_spec.rb
90
+ - spec/spec_helper.rb