class Light::Decorator::Base

Attributes

o[R]

@return original object

object[R]

@return original object

Public Class Methods

decorate(object, options = {})
Alias for: new
new(object, options = {}) click to toggle source

Decorate the object

@param [Object] object for decoration @param [Hash] options

@return [Object] decorated object

# File lib/light/decorator/decorate.rb, line 13
def initialize(object, options = {})
  @object = object
  @options = options

  delegate_methods
  decorate_associations
end
Also aliased as: decorate

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/light/decorator/decorate.rb, line 36
def ==(other)
  super || object == other
end
decorated?() click to toggle source

Check current ActiveRecord::Model is decorated or not

@return [Bool]

# File lib/light/decorator/decorate.rb, line 24
def decorated?
  true
end
eql?(other) click to toggle source
Calls superclass method
# File lib/light/decorator/decorate.rb, line 40
def eql?(other)
  super || object.eql?(other)
end
h()
Alias for: helpers
helpers() click to toggle source

Current view scope

@return [ActionView::Base]

# File lib/light/decorator/decorate.rb, line 31
def helpers
  return @helpers if defined?(@helpers)
  @helpers = Light::Decorator::ViewContext.current
end
Also aliased as: h
is_a?(klass) click to toggle source
Calls superclass method
# File lib/light/decorator/decorate.rb, line 44
def is_a?(klass)
  super || object.is_a?(klass)
end
kind_of?(klass) click to toggle source
Calls superclass method
# File lib/light/decorator/decorate.rb, line 48
def kind_of?(klass)
  super || object.kind_of?(klass)
end

Private Instance Methods

decorate_associations() click to toggle source
# File lib/light/decorator/decorate.rb, line 70
def decorate_associations
  object.class.reflect_on_all_associations.map(&:name).each do |reflection_name|
    define_singleton_method reflection_name do
      reflection = object.public_send(reflection_name)
      reflection.decorate(@options.reverse_merge(soft: true)) if reflection
    end
  end
end
delegate_methods() click to toggle source
# File lib/light/decorator/decorate.rb, line 61
def delegate_methods
  # It's more faster than using Forwardable
  (object.public_methods - methods + Light::Decorator::FORCE_DELEGATE).each do |method|
    define_singleton_method method do |*args, &block|
      object.__send__(method, *args, &block)
    end
  end
end