Class: KDecor::Helper

Inherits:
Object
  • Object
show all
Includes:
ResolveInstance
Defined in:
lib/k_decor/helper.rb

Overview

KDoc.settings :x, decorators: [Class, Class.instance, :class] do

#

end

Instance Method Summary collapse

Methods included from ResolveInstance

#resolve_decorator_instance

Instance Method Details

#decorate(data, *decorators, behaviour: :default, behaviours: []) ⇒ Object

Run a list of decorators against the data



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/k_decor/helper.rb', line 35

def decorate(data, *decorators, behaviour: :default, behaviours: [])
  if behaviours.length.zero?
    decorators.map do |decorator|
      decorator.decorate(data, behaviour: behaviour)
    end
  else
    behaviours.each do |behave|
      decorators.map do |decorator|
        decorator.decorate(data, behaviour: behave)
      end
    end
  end
  data
end

#resolve_decorators(decorators) ⇒ Object

Resolve decorator instances by evaluating a list of decorators in the format :symbol, class or :class_instance

:symbol will be looked up from pre-configured decorators :class will be instantiated :class_instance will be returned as is

decorators as either Class or Instances or pre-configured symbols

Examples:

Sample of how decorators can be passed


decorators(:uppercase, PeopleModelDecorator, PluralizeModelDecorator.new('Person' => 'People') })

Parameters:



24
25
26
27
28
29
30
31
32
# File 'lib/k_decor/helper.rb', line 24

def resolve_decorators(decorators)
  decorators.map do |decorator|
    if decorator.is_a?(Symbol)
      KDecor.configuration.get_decorator(decorator)
    else
      resolve_decorator_instance(decorator)
    end
  end.compact
end