class Decorum::Decorator

Attributes

decorator_handle[R]
object[R]
root[R]

Public Class Methods

accumulator(*args)

hint as to how one might use these attributes

Alias for: share
default_attributes(attrs) click to toggle source

allow Decorator classes to provide attribute defaults (not shared)

# File lib/decorum/decorator.rb, line 79
def default_attributes(attrs)
  @default_attributes = attrs
  attrs.keys.each do |attr|
    attr_accessor attr.to_sym
  end
end
get_default_attributes() click to toggle source
# File lib/decorum/decorator.rb, line 86
def get_default_attributes
  @default_attributes || {}
end
immediate(*method_names) click to toggle source

allow Decorator classes to override the decorated object's public methods; use with no args to declare the entire interface

# File lib/decorum/decorator.rb, line 92
def immediate(*method_names)
  if method_names.empty?
    @all_immediate = true
  else
    @immediate_methods ||= []
    @immediate_methods += method_names
  end
end
immediate_methods() click to toggle source
# File lib/decorum/decorator.rb, line 101
def immediate_methods
  @all_immediate ? instance_methods(false) : (@immediate_methods || [])
end
new(next_link, root, options) click to toggle source
# File lib/decorum/decorator.rb, line 7
def initialize(next_link, root, options)
  @passed_options   = options
  @next_link        = next_link
  @root             = root
  @decorator_handle = options[:decorator_handle]

  defaults = self.class.get_default_attributes
  attribute_options = defaults ? defaults.merge(options) : options

  attribute_options.each do |key, value|
    setter = :"#{key}="
    if respond_to?(setter)
      send setter, value
    end
  end
end
share(*args) click to toggle source

allow Decorator classes to share state among their instances

# File lib/decorum/decorator.rb, line 59
def share(*args)
  args.each do |getter|
    getter = getter.to_sym
    define_method(getter) { self.decorated_state.send(getter) }

    boolean = :"#{getter}?"
    define_method(boolean) { self.decorated_state.send(getter) ? true : false }

    setter = :"#{getter}="
    define_method(setter) { |value| self.decorated_state.send(setter, value) }
    
    resetter = :"reset_#{getter}"
    define_method(resetter) { self.decorated_state.send(setter, nil) }
  end
end
Also aliased as: accumulator

Public Instance Methods

decorated_state() click to toggle source

a superhash of shared state between Decorators of the same class

# File lib/decorum/decorator.rb, line 31
def decorated_state
  root._decorum_decorated_state(self.class)
end
decorated_tail(current_value=nil) { || ... } click to toggle source

Decorators that want stackable or cumulative behavior can do so with tail recursion. Wrap the tail call in decorated_tail to catch the end of the chain and return the accumulator

# File lib/decorum/decorator.rb, line 39
def decorated_tail(current_value=nil, &block)
  response = catch :chain_stop do
    yield
  end
  response.is_a?(Decorum::ChainStop) ? current_value : response
end
Also aliased as: defer
defer(current_value=nil, &block)
Alias for: decorated_tail
method_missing(*args, &block) click to toggle source

delegate to next_link note that we are not faking respond_to? because we want that to reflect what the decorator can respond to locally

# File lib/decorum/decorator.rb, line 51
def method_missing(*args, &block)
  @next_link.send(*args, &block)
end
post_decorate() click to toggle source

override if you want

# File lib/decorum/decorator.rb, line 25
def post_decorate
  nil
end