Class: Wrest::Components::Mutators::Base
- Inherits:
-
Object
- Object
- Wrest::Components::Mutators::Base
- Defined in:
- lib/wrest/components/mutators/base.rb
Overview
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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#next_mutator ⇒ Object
readonly
Returns the value of attribute next_mutator.
Instance Method Summary collapse
-
#initialize(next_mutator = nil) ⇒ Base
constructor
A new instance of Base.
-
#mutate(tuple) ⇒ Object
This is a template method which operates on a tuple (well, pair) from a hash map and guarantees mutator chaining.
Constructor Details
#initialize(next_mutator = nil) ⇒ Base
Returns a new instance of Base.
17 18 19 |
# File 'lib/wrest/components/mutators/base.rb', line 17 def initialize(next_mutator = nil) @next_mutator = next_mutator end |
Instance Attribute Details
#next_mutator ⇒ Object (readonly)
Returns the value of attribute next_mutator.
15 16 17 |
# File 'lib/wrest/components/mutators/base.rb', line 15 def next_mutator @next_mutator 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.
34 35 36 37 |
# File 'lib/wrest/components/mutators/base.rb', line 34 def mutate(tuple) out_tuple = do_mutate(tuple) next_mutator ? next_mutator.mutate(out_tuple) : out_tuple end |