Class: Wrest::Components::Mutators::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/wrest/components/mutators/base.rb

Direct Known Subclasses

CamelToSnakeCase, XmlTypeCaster

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(next_mutator = nil) ⇒ Base

Returns a new instance of Base.



35
36
37
# File 'lib/wrest/components/mutators/base.rb', line 35

def initialize(next_mutator = nil)
  @next_mutator = next_mutator
end

Instance Attribute Details

#next_mutatorObject (readonly)

Returns the value of attribute next_mutator.



20
21
22
# File 'lib/wrest/components/mutators/base.rb', line 20

def next_mutator
  @next_mutator
end

Class Method Details

.inherited(subklass) ⇒ Object

Registers all subclasses of Mutators::Base in Mutators::REGISTRY making it easy to reference and chain them later.

See Mutators#chain for more information.



27
28
29
30
31
32
33
# File 'lib/wrest/components/mutators/base.rb', line 27

def self.inherited(subklass)
  super
  return if Utils.string_blank?(subklass.name)

  key = Utils.string_underscore(Utils.string_demodulize(subklass.name)).to_sym
  Wrest::Components::Mutators.registry[key] = subklass
end

Instance Method Details

#mutate(tuple) ⇒ Object

This is a template method which operates on a tuple (well, pair) from a hash map and guarantees mutator chaining.

Iterating over any hash using each injects each key/value pair from the hash in the form of an array. This method expects of this form as an argument, i.e. an array with the structure [:key, :value]

The implementation of the mutation is achieved by overriding the do_mutate method in a subclass. Note that failing to do so will result in an exception at runtime.



52
53
54
55
# File 'lib/wrest/components/mutators/base.rb', line 52

def mutate(tuple)
  out_tuple = do_mutate(tuple)
  next_mutator ? next_mutator.mutate(out_tuple) : out_tuple
end