Class: KDecor::BaseDecorator
- Inherits:
-
Object
- Object
- KDecor::BaseDecorator
- Includes:
- KLog::Logging
- Defined in:
- lib/k_decor/base_decorator.rb
Overview
Base decorator
Instance Attribute Summary collapse
-
#compatible_type ⇒ Object
Compatible type will store the Object Type that this decorator is suitable for processing.
-
#implemented_behaviours ⇒ Object
What are the specific behaviours available on this decorator.
Instance Method Summary collapse
-
#behaviour?(behaviour) ⇒ Boolean
Does the decorator implement the behaviour , any = false).
- #compatible?(target) ⇒ Boolean
- #decorate(target, **opts) ⇒ Object
-
#initialize(compatible_type) ⇒ BaseDecorator
constructor
A new instance of BaseDecorator.
Constructor Details
#initialize(compatible_type) ⇒ BaseDecorator
Returns a new instance of BaseDecorator.
22 23 24 25 |
# File 'lib/k_decor/base_decorator.rb', line 22 def initialize(compatible_type) @compatible_type = compatible_type @implemented_behaviours = [] end |
Instance Attribute Details
#compatible_type ⇒ Object
Compatible type will store the Object Type that this decorator is suitable for processing
10 11 12 |
# File 'lib/k_decor/base_decorator.rb', line 10 def compatible_type @compatible_type end |
#implemented_behaviours ⇒ Object
What are the specific behaviours available on this decorator
If you wish to use a decorator to run against a compatible data type you do not need individual behaviours then set implemented_behaviours to [:default]
But if this decorator type only targets certain behaviours then give it a list of specific :behaviour to perform. e.g. [:update_fields, :update_rows]
20 21 22 |
# File 'lib/k_decor/base_decorator.rb', line 20 def implemented_behaviours @implemented_behaviours end |
Instance Method Details
#behaviour?(behaviour) ⇒ Boolean
Does the decorator implement the behaviour , any = false)
29 30 31 |
# File 'lib/k_decor/base_decorator.rb', line 29 def behaviour?(behaviour) behaviour == :all || implemented_behaviours.include?(behaviour) end |
#compatible?(target) ⇒ Boolean
33 34 35 |
# File 'lib/k_decor/base_decorator.rb', line 33 def compatible?(target) target.is_a?(compatible_type) end |
#decorate(target, **opts) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/k_decor/base_decorator.rb', line 37 def decorate(target, **opts) raise KType::Error, "#{self.class} is incompatible with data object" unless compatible?(target) raise KType::Error, "#{self.class} has not implemented an update method" unless respond_to?(:update) update(target, **opts) end |