Class: Separatum::Processors::FieldChanger
- Inherits:
-
Object
- Object
- Separatum::Processors::FieldChanger
- Defined in:
- lib/separatum/processors/field_changer.rb
Constant Summary collapse
- CLASS_METHOD =
Allows:
SomeClass#method SomeNamespace::SomeClass#method SuperNamespace::SomeNamespace::SomeClass#method
/^([A-Za-z_][0-9A-Za-z_]*(?:::[A-Za-z_][0-9A-Za-z_]*)*)#([A-Za-z_][0-9A-Za-z_]*)$/
Instance Method Summary collapse
- #call(*hashes) ⇒ Object
-
#initialize(*options, &block) ⇒ FieldChanger
constructor
A new instance of FieldChanger.
- #parse_options(*options) ⇒ Object
Constructor Details
#initialize(*options, &block) ⇒ FieldChanger
Returns a new instance of FieldChanger.
10 11 12 |
# File 'lib/separatum/processors/field_changer.rb', line 10 def initialize(*, &block) (*, &block) end |
Instance Method Details
#call(*hashes) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/separatum/processors/field_changer.rb', line 14 def call(*hashes) hashes.flatten.map do |hash| new_hash = hash.symbolize_keys if hash[:_klass] == @klass && hash.key?(@method) new_hash[@method] = @transformer.call(hash[@method]) end new_hash end end |
#parse_options(*options) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/separatum/processors/field_changer.rb', line 24 def (*) p1 = .shift if p1.is_a?(String) && (res = p1.match(CLASS_METHOD)) @klass = res[1] @method = res[2].to_sym elsif p1.is_a?(Class) @klass = p1.to_s p2 = .shift if p2.is_a?(Symbol) @method = p2 else fail end else fail end if block_given? @transformer = Proc.new pp @transformer else p3 = .shift if p3.is_a?(Proc) @transformer = p3 elsif p3.is_a?(String) && (res = p3.match(CLASS_METHOD)) @transformer = Proc.new { |value| Object.const_get(res[1]).new.send(res[2].to_sym, value) } elsif p3.is_a?(Class) p4 = .shift if p4.is_a?(Symbol) @transformer = Proc.new { |value| p3.new.send(p4, value) } else fail end else fail end end end |