hashie 2.0.4 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 2.0.5
4
+
5
+ * make coercion work better with non-symbol keys in mash wapcaplet #96
6
+
3
7
  ## 2.0.4
4
8
 
5
9
  * make #fetch method consistent with normal Hash markiz #94
@@ -21,6 +21,10 @@ module Hashie
21
21
  super(key, value)
22
22
  end
23
23
 
24
+ def custom_writer(key, value)
25
+ self[key] = value
26
+ end
27
+
24
28
  def replace(other_hash)
25
29
  (keys - other_hash.keys).each { |key| delete(key) }
26
30
  other_hash.each { |key, value| self[key] = value }
@@ -31,7 +35,7 @@ module Hashie
31
35
  module ClassMethods
32
36
  # Set up a coercion rule such that any time the specified
33
37
  # key is set it will be coerced into the specified class.
34
- # Coercion will occur by first attempting to call Class.coerce
38
+ # Coercion will occur by first attempting to call Class.coerce
35
39
  # and then by calling Class.new with the value as an argument
36
40
  # in either case.
37
41
  #
@@ -59,7 +63,7 @@ module Hashie
59
63
  # Returns the specific key coercion for the specified key,
60
64
  # if one exists.
61
65
  def key_coercion(key)
62
- key_coercions[key]
66
+ key_coercions[key.to_sym]
63
67
  end
64
68
 
65
69
  # Set up a coercion rule such that any time a value of the
@@ -94,7 +98,7 @@ module Hashie
94
98
  end
95
99
  end
96
100
  end
97
-
101
+
98
102
  # Return all value coercions that have the :strict rule as true.
99
103
  def strict_value_coercions; @strict_value_coercions || {} end
100
104
  # Return all value coercions that have the :strict rule as false.
@@ -103,7 +107,7 @@ module Hashie
103
107
  # Fetch the value coercion, if any, for the specified object.
104
108
  def value_coercion(value)
105
109
  from = value.class
106
- strict_value_coercions[from] || lenient_value_coercions[from]
110
+ strict_value_coercions[from] || lenient_value_coercions[from]
107
111
  end
108
112
  end
109
113
  end
@@ -82,7 +82,7 @@ module Hashie
82
82
 
83
83
  # Retrieves an attribute set in the Mash. Will convert
84
84
  # any key passed in to a string before retrieving.
85
- def [](key)
85
+ def custom_reader(key)
86
86
  value = regular_reader(convert_key(key))
87
87
  yield value if block_given?
88
88
  value
@@ -91,10 +91,13 @@ module Hashie
91
91
  # Sets an attribute in the Mash. Key will be converted to
92
92
  # a string before it is set, and Hashes will be converted
93
93
  # into Mashes for nesting purposes.
94
- def []=(key,value) #:nodoc:
94
+ def custom_writer(key,value) #:nodoc:
95
95
  regular_writer(convert_key(key), convert_value(value))
96
96
  end
97
97
 
98
+ alias_method :[], :custom_reader
99
+ alias_method :[]=, :custom_writer
100
+
98
101
  # This is the bang method reader, it will return a new Mash
99
102
  # if there isn't a value already assigned to the key requested.
100
103
  def initializing_reader(key)
@@ -148,11 +151,11 @@ module Hashie
148
151
  other_hash.each_pair do |k,v|
149
152
  key = convert_key(k)
150
153
  if regular_reader(key).is_a?(Mash) and v.is_a?(::Hash)
151
- regular_reader(key).deep_update(v, &blk)
154
+ custom_reader(key).deep_update(v, &blk)
152
155
  else
153
156
  value = convert_value(v, true)
154
157
  value = blk.call(key, self[k], value) if blk
155
- regular_writer(key, value)
158
+ custom_writer(key, value)
156
159
  end
157
160
  end
158
161
  self
@@ -1,3 +1,3 @@
1
1
  module Hashie
2
- VERSION = '2.0.4'
2
+ VERSION = '2.0.5'
3
3
  end
@@ -77,6 +77,38 @@ describe Hashie::Extensions::Coercion do
77
77
  instance[:hi].should == "bye"
78
78
  end
79
79
  end
80
+
81
+ context "when used with a Mash" do
82
+ class UserMash < Hashie::Mash
83
+ end
84
+ class TweetMash < Hashie::Mash
85
+ include Hashie::Extensions::Coercion
86
+ coerce_key :user, UserMash
87
+ end
88
+
89
+ it "should coerce with instance initialization" do
90
+ tweet = TweetMash.new(:user => {:email => 'foo@bar.com'})
91
+ tweet[:user].should be_a(UserMash)
92
+ end
93
+
94
+ it "should coerce when setting with attribute style" do
95
+ tweet = TweetMash.new
96
+ tweet.user = {:email => 'foo@bar.com'}
97
+ tweet[:user].should be_a(UserMash)
98
+ end
99
+
100
+ it "should coerce when setting with string index" do
101
+ tweet = TweetMash.new
102
+ tweet['user'] = {:email => 'foo@bar.com'}
103
+ tweet[:user].should be_a(UserMash)
104
+ end
105
+
106
+ it "should coerce when setting with symbol index" do
107
+ tweet = TweetMash.new
108
+ tweet[:user] = {:email => 'foo@bar.com'}
109
+ tweet[:user].should be_a(UserMash)
110
+ end
111
+ end
80
112
  end
81
113
 
82
114
  describe '.coerce_value' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashie
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 2.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-04-24 00:00:00.000000000 Z
13
+ date: 2013-05-10 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -157,7 +157,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
157
157
  version: '0'
158
158
  segments:
159
159
  - 0
160
- hash: 2159890714195675822
160
+ hash: 214342929644806912
161
161
  required_rubygems_version: !ruby/object:Gem::Requirement
162
162
  none: false
163
163
  requirements:
@@ -166,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
166
  version: '0'
167
167
  segments:
168
168
  - 0
169
- hash: 2159890714195675822
169
+ hash: 214342929644806912
170
170
  requirements: []
171
171
  rubyforge_project:
172
172
  rubygems_version: 1.8.23