mbleigh-mash 0.0.5 → 0.0.6

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.
Files changed (4) hide show
  1. data/lib/mash.rb +12 -12
  2. data/mash.gemspec +2 -2
  3. data/spec/mash_spec.rb +19 -1
  4. metadata +3 -3
@@ -40,15 +40,13 @@
40
40
  # mash.author # => <Mash name="Michael Bleigh">
41
41
  #
42
42
  class Mash < Hash
43
- VERSION = '0.0.3'
44
-
45
43
  # If you pass in an existing hash, it will
46
44
  # convert it to a Mash including recursively
47
45
  # descending into arrays and hashes, converting
48
46
  # them as well.
49
- def initialize(source_hash = nil)
47
+ def initialize(source_hash = nil, &blk)
50
48
  deep_update(source_hash) if source_hash
51
- super(nil)
49
+ super(&blk)
52
50
  end
53
51
 
54
52
  def id #:nodoc:
@@ -67,7 +65,7 @@ class Mash < Hash
67
65
  if key.is_a?(Symbol) && key?(key)
68
66
  self[key]
69
67
  else
70
- super
68
+ key ? super : super()
71
69
  end
72
70
  end
73
71
 
@@ -95,18 +93,18 @@ class Mash < Hash
95
93
  self[key] = Mash.new
96
94
  end
97
95
 
96
+ alias_method :regular_dup, :dup
98
97
  # Duplicates the current mash as a new mash.
99
98
  def dup
100
99
  Mash.new(self)
101
100
  end
102
101
 
103
- alias_method :regular_inspect, :inspect
104
-
105
102
  alias_method :picky_key?, :key?
106
103
  def key?(key)
107
104
  picky_key?(convert_key(key))
108
105
  end
109
106
 
107
+ alias_method :regular_inspect, :inspect
110
108
  # Prints out a pretty object-like string of the
111
109
  # defined attributes.
112
110
  def inspect
@@ -128,14 +126,15 @@ class Mash < Hash
128
126
  # Recursively merges this mash with the passed
129
127
  # in hash, merging each hash in the hierarchy.
130
128
  def deep_update(other_hash)
131
- other_hash = other_hash.stringify_keys unless other_hash.is_a?(Mash)
129
+ other_hash = other_hash.to_hash if other_hash.is_a?(Mash)
130
+ other_hash = other_hash.stringify_keys
132
131
  other_hash.each_pair do |k,v|
133
132
  k = convert_key(k)
134
133
  self[k] = self[k].to_mash if self[k].is_a?(Hash) unless self[k].is_a?(Mash)
135
134
  if self[k].is_a?(Hash) && other_hash[k].is_a?(Hash)
136
- self[k].deep_merge!(other_hash[k])
135
+ self[k] = self[k].deep_merge(other_hash[k]).dup
137
136
  else
138
- self.send(k + "=", convert_value(other_hash[k]))
137
+ self.send(k + "=", convert_value(other_hash[k],true))
139
138
  end
140
139
  end
141
140
  end
@@ -175,7 +174,7 @@ class Mash < Hash
175
174
  elsif key?(method_name)
176
175
  self[method_name]
177
176
  elsif match = method_name.to_s.match(/^([a-z][a-z0-9A-Z_]+)$/)
178
- default
177
+ default(method_name)
179
178
  else
180
179
  super
181
180
  end
@@ -187,9 +186,10 @@ class Mash < Hash
187
186
  key.to_s
188
187
  end
189
188
 
190
- def convert_value(value) #:nodoc:
189
+ def convert_value(value, dup=false) #:nodoc:
191
190
  case value
192
191
  when Hash
192
+ value = value.dup if value.is_a?(Mash) && dup
193
193
  value.is_a?(Mash) ? value : value.to_mash
194
194
  when Array
195
195
  value.collect{ |e| convert_value(e) }
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "mash"
3
- s.version = "0.0.5"
4
- s.date = "2008-04-26"
3
+ s.version = "0.0.6"
4
+ s.date = "2008-07-22"
5
5
  s.summary = "An extended Hash that gives simple pseudo-object functionality that can be built from hashes and easily extended"
6
6
  s.email = "michael@intridea.com"
7
7
  s.homepage = "http://github.com/mbleigh/mash"
@@ -69,6 +69,12 @@ describe Mash do
69
69
  @mash.details.email.should == "michael@intridea.com"
70
70
  end
71
71
 
72
+ it "should convert hash assignments into mashes" do
73
+ @mash.details = {:email => 'randy@asf.com', :address => {:state => 'TX'} }
74
+ @mash.details.email.should == 'randy@asf.com'
75
+ @mash.details.address.state.should == 'TX'
76
+ end
77
+
72
78
  context "#initialize" do
73
79
  it "should convert an existing hash to a Mash" do
74
80
  converted = Mash.new({:abc => 123, :name => "Bob"})
@@ -90,11 +96,23 @@ describe Mash do
90
96
  end
91
97
 
92
98
  it "should convert an existing Mash into a Mash" do
93
- initial = Mash.new(:name => 'randy')
99
+ initial = Mash.new(:name => 'randy', :address => {:state => 'TX'})
94
100
  copy = Mash.new(initial)
95
101
  initial.name.should == copy.name
102
+ initial.object_id.should_not == copy.object_id
103
+ copy.address.state.should == 'TX'
104
+ copy.address.state = 'MI'
105
+ initial.address.state.should == 'TX'
106
+ copy.address.object_id.should_not == initial.address.object_id
96
107
  end
97
108
 
109
+ it "should accept a default block" do
110
+ initial = Mash.new { |h,i| h[i] = []}
111
+ initial.default_proc.should_not be_nil
112
+ initial.default.should be_nil
113
+ initial.test.should == []
114
+ initial.test?.should be_true
115
+ end
98
116
  end
99
117
  end
100
118
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mbleigh-mash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-04-26 00:00:00 -07:00
12
+ date: 2008-07-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -55,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
55
  requirements: []
56
56
 
57
57
  rubyforge_project:
58
- rubygems_version: 1.0.1
58
+ rubygems_version: 1.2.0
59
59
  signing_key:
60
60
  specification_version: 2
61
61
  summary: Mash is an extended Hash that gives simple pseudo-object functionality that can be built from hashes and easily extended