Class Wrest::Components::Mutators::Base

  1. lib/wrest/components/mutators/base.rb
Parent: Object

This is a base implementation of a hash mutator that ensures that the mutate method will chain to the next mutator by using a template method.

Methods

public class

  1. inherited
  2. new

public instance

  1. mutate

protected instance

  1. do_mutate

Attributes

next_mutator [R]

Public class methods

inherited (subklass)

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

See Mutators#chain for more information.

[show source]
    # File lib/wrest/components/mutators/base.rb, line 24
24:       def self.inherited(subklass)
25:         Wrest::Components::Mutators::REGISTRY[subklass.name.demodulize.underscore.to_sym] = subklass unless subklass.name.blank?
26:       end
new (next_mutator = nil)
[show source]
    # File lib/wrest/components/mutators/base.rb, line 28
28:       def initialize(next_mutator = nil)
29:         @next_mutator = next_mutator
30:       end

Public instance methods

mutate (tuple)

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.

[show source]
    # File lib/wrest/components/mutators/base.rb, line 45
45:       def mutate(tuple)
46:         out_tuple = do_mutate(tuple)
47:         next_mutator ? next_mutator.mutate(out_tuple) : out_tuple
48:       end

Protected instance methods

do_mutate (tuple)
[show source]
    # File lib/wrest/components/mutators/base.rb, line 51
51:       def do_mutate(tuple)
52:         raise Wrest::Exceptions::MethodNotOverridden
53:       end