ismasan-hash_mapper 0.0.4 → 0.0.5

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/README.rdoc CHANGED
@@ -123,6 +123,15 @@ Just use the denormalize() method instead:
123
123
  This will work with your block filters and even nested mappers (see below).
124
124
 
125
125
  === Advanced usage
126
+ ==== Array access
127
+ You want:
128
+
129
+ {:names => ['Ismael', 'Celis']} converted to {:first_name => 'Ismael', :last_name => 'Celis'}
130
+
131
+ Do this:
132
+
133
+ map from('/names[0]'), to('/first_name')
134
+ map from('/names[1]'), to('/last_name')
126
135
  ==== Nested mappers
127
136
 
128
137
  You want to map nested structures delegating to different mappers:
@@ -183,18 +192,6 @@ But HashMapper's nested mappers will actually do that for you if a value is an a
183
192
  == REQUIREMENTS:
184
193
 
185
194
  == TODO:
186
- === Array access
187
-
188
- * See pending specs
189
-
190
- You want:
191
-
192
- {:names => ['Ismael', 'Celis']} converted to {:first_name => 'Ismael', :last_name => 'Celis'}
193
-
194
- Do this:
195
-
196
- map from('/names[0]'), to('/first_name')
197
- map from('/names[1]'), to('/last_name')
198
195
 
199
196
  == INSTALL:
200
197
 
data/lib/hash_mapper.rb CHANGED
@@ -11,7 +11,7 @@ unless Symbol.instance_methods.include?('to_proc')
11
11
  end
12
12
 
13
13
  module HashMapper
14
- VERSION = '0.0.4'
14
+ VERSION = '0.0.5'
15
15
 
16
16
  def maps
17
17
  @maps ||= []
@@ -85,8 +85,9 @@ module HashMapper
85
85
 
86
86
  def get_value_from_input(output, input, path, meth)
87
87
  value = path.inject(input) do |h,e|
88
- throw :no_value unless h.has_key?(e)
89
- h[e]
88
+ throw :no_value unless h.has_key?(e[0].to_sym)
89
+ e[1].nil? ? h[e[0].to_sym] : h[e[0].to_sym][e[1].to_i]
90
+ #h[e[0].to_sym]
90
91
  end
91
92
  value = delegate_to_nested_mapper(value, meth) if delegated_mapper
92
93
  value
@@ -103,14 +104,31 @@ module HashMapper
103
104
 
104
105
  def add_value_to_hash!(hash, path, value)
105
106
  path.inject(hash) do |h,e|
106
- if h[e]
107
- h[e]
107
+ if contained?(h,e)
108
+ if e[1].nil?
109
+ h[e[0].to_sym]
110
+ else
111
+ if e == path.last
112
+ h[e[0].to_sym][e[1].to_i] = value
113
+ end
114
+ h[e[0].to_sym][e[1].to_i]
115
+ end
108
116
  else
109
- h[e] = (e == path.last ? path.apply_filter(value) : {})
117
+ if e[1].nil?
118
+ h[e[0].to_sym] = (e == path.last ? path.apply_filter(value) : {})
119
+ else
120
+ h[e[0].to_sym] = []
121
+ h[e[0].to_sym][e[1].to_i] = (e == path.last ? path.apply_filter(value) : {})
122
+ end
110
123
  end
111
124
  end
112
125
  end
113
126
 
127
+ def contained?(h,e)
128
+ e[1].nil? ? h[e[0].to_sym] : h[e[0].to_sym][e[1].to_i].nil?
129
+ rescue
130
+ false
131
+ end
114
132
  end
115
133
 
116
134
  # contains array of path segments
@@ -142,7 +160,12 @@ module HashMapper
142
160
  private
143
161
 
144
162
  def parse(path)
145
- path.sub(/^\//,'').split('/').map(&:to_sym)
163
+ #path.sub(/^\//,'').split('/').map(&:to_sym)
164
+ path.sub(/^\//,'').split('/').map{ |p| key_index p }
165
+ end
166
+
167
+ def key_index(p)
168
+ p =~ /\[[0-9]+\]$/ ? p.sub(/\[([0-9]+)\]$/,' \1').split(' ') : [p,nil]
146
169
  end
147
170
 
148
171
  end
@@ -146,13 +146,11 @@ describe "array indexes" do
146
146
  end
147
147
 
148
148
  it "should extract defined array values" do
149
- pending "must reimplement for normalize and denormalize"
150
149
  WithArrays.normalize(@from).should == @to
151
150
  end
152
151
 
153
152
  it "should map the other way restoring arrays" do
154
- pending "must reimplement for normalize and denormalize"
155
- WithArrays.denormalize(@from).should == @to
153
+ WithArrays.denormalize(@to).should == @from
156
154
  end
157
155
  end
158
156
 
@@ -271,18 +269,22 @@ end
271
269
  class NoKeys
272
270
  extend HashMapper
273
271
 
274
- map from('/exists'), to('/exists_yahoo')
275
- map from('/foo'), to('/bar')
272
+ map from('/exists'), to('/exists_yahoo') #in
273
+ map from('/exists_as_nil'), to('/exists_nil') #in
274
+ map from('/foo'), to('/bar') # not in
275
+
276
276
  end
277
277
 
278
278
  describe "with non-matching maps" do
279
279
  before :all do
280
280
  @input = {
281
281
  :exists => 1,
282
+ :exists_as_nil => nil,
282
283
  :doesnt_exist => 2
283
284
  }
284
285
  @output = {
285
- :exists_yahoo => 1
286
+ :exists_yahoo => 1,
287
+ :exists_nil => nil
286
288
  }
287
289
  end
288
290
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ismasan-hash_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Celis
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-04 00:00:00 -08:00
12
+ date: 2009-02-08 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency