Class: KDecor::BaseDecorator

Inherits:
Object
  • Object
show all
Includes:
KLog::Logging
Defined in:
lib/k_decor/base_decorator.rb

Overview

Base decorator

Instance Attribute Summary collapse

Instance Method Summary collapse

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_typeObject

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_behavioursObject

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)

Returns:

  • (Boolean)


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

Returns:

  • (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

Raises:

  • (KType::Error)


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