Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Got rid of coerce method, because it can be handled by the filter (an…

…d can use e.g. &:to_i)
  • Loading branch information...
commit 2fb09daa0705786ae4b179e81707fd005a3bf07d 1 parent 4552ce9
@markevans markevans authored
Showing with 18 additions and 14 deletions.
  1. +16 −12 lib/hash_mapper.rb
  2. +2 −2 spec/hash_mapper_spec.rb
View
28 lib/hash_mapper.rb
@@ -1,6 +1,13 @@
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
+# This allows us to call blah(&:some_method) instead of blah{|i| i.some_method }
+class Symbol
+ def to_proc
+ Proc.new {|obj| obj.send(self) }
+ end
+end
+
module HashMapper
VERSION = '0.0.2'
@@ -8,14 +15,14 @@ def maps
@maps ||= []
end
- def map(from, to, &blk)
+ def map(from, to, &filter)
self.maps << [from, to]
- to.filter = blk if block_given? # Useful if just one block given
+ to.filter = filter if block_given? # Useful if just one block given
end
- def from(path, coerce_method = nil, &blk)
- path_map = PathMap.new(path, coerce_method)
- path_map.filter = blk if block_given? # Useful if two blocks given
+ def from(path, &filter)
+ path_map = PathMap.new(path)
+ path_map.filter = filter if block_given? # Useful if two blocks given
path_map
end
@@ -73,22 +80,19 @@ class PathMap
attr_writer :filter
- def initialize(path, coerce_method = nil)
+ def initialize(path)
@path = path.dup
- @coerce_method = coerce_method
@index = extract_array_index!(path)
@segments = parse(path)
@filter = lambda{|value| value}# default filter does nothing
end
def resolve_value(another_path, incoming_hash)
- coerce another_path.extract_value_from(incoming_hash)
+ apply_filter( another_path.extract_value_from(incoming_hash) )
end
- def coerce(value)
- value = @filter.call(value)
- return value unless @coerce_method
- value.send(@coerce_method) rescue value
+ def apply_filter(value)
+ @filter.call(value)
end
def extract_value_from(incoming_hash)
View
4 spec/hash_mapper_spec.rb
@@ -68,8 +68,8 @@ class ManyLevels
class DifferentTypes
extend HashMapper
- map from('/strings/a',:to_s), to('/integers/a',:to_i)
- map from('/integers/b',:to_i), to('/strings/b',:to_s)
+ map from('/strings/a', &:to_s), to('/integers/a', &:to_i)
+ map from('/integers/b', &:to_i), to('/strings/b', &:to_s)
end
describe 'coercing types' do
Please sign in to comment.
Something went wrong with that request. Please try again.