Class: EpiDecorator::Base

Inherits:
Delegator
  • Object
show all
Defined in:
lib/epi_decorator/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Base

Returns a new instance of Base.



9
10
11
# File 'lib/epi_decorator/base.rb', line 9

def initialize(object)
  self.object = object
end

Instance Attribute Details

#objectObject Also known as: source, model

Returns the value of attribute object.



4
5
6
# File 'lib/epi_decorator/base.rb', line 4

def object
  @object
end

Class Method Details

.collection_decorator_classObject



75
76
77
# File 'lib/epi_decorator/base.rb', line 75

def self.collection_decorator_class
  ::EpiDecorator::CollectionDecorator
end

.decorate(object) ⇒ Object



46
47
48
# File 'lib/epi_decorator/base.rb', line 46

def self.decorate(object)
  new(object)
end

.decorate_collection(collection, with: self) ⇒ Object



50
51
52
# File 'lib/epi_decorator/base.rb', line 50

def self.decorate_collection(collection, with: self)
  collection_decorator_class.new(collection, with: with)
end

.decorates(object_class) ⇒ Object



79
80
81
# File 'lib/epi_decorator/base.rb', line 79

def self.decorates(object_class)
  @object_class = object_class.to_s.camelize.constantize
end

.decorates_association(association, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/epi_decorator/base.rb', line 54

def self.decorates_association(association, options = {})
  define_method association do
    association_data = object.send(association)
    return if association_data.nil?
    decorator_class = options[:with] || association_data.decorator_class
    
    if association_data.respond_to?(:map)
      decorator_class.decorate_collection association_data
    else
      decorator_class.decorate association_data
    end
  end
end

.decorates_associations(*associations) ⇒ Object



68
69
70
71
72
73
# File 'lib/epi_decorator/base.rb', line 68

def self.decorates_associations(*associations)
  options = associations.extract_options!
  associations.each do |association|
    decorates_association association, options
  end
end

.method_missing(method, *args, &block) ⇒ Object



83
84
85
86
87
# File 'lib/epi_decorator/base.rb', line 83

def self.method_missing(method, *args, &block)
  return super unless delegatable?(method)

  object_class.send(method, *args, &block)
end

.respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/epi_decorator/base.rb', line 89

def self.respond_to_missing?(method, include_private = false)
  super || delegatable?(method)
end

Instance Method Details

#!=(other) ⇒ Object



25
26
27
# File 'lib/epi_decorator/base.rb', line 25

def !=(other)
  !(self == other)
end

#==(other) ⇒ Object



21
22
23
# File 'lib/epi_decorator/base.rb', line 21

def ==(other)
  super || other == self.object
end

#__getobj__Object



13
14
15
# File 'lib/epi_decorator/base.rb', line 13

def __getobj__
  self.object
end

#__setobj__(obj) ⇒ Object



17
18
19
# File 'lib/epi_decorator/base.rb', line 17

def __setobj__(obj)
  self.object = obj
end

#decorateObject



38
39
40
# File 'lib/epi_decorator/base.rb', line 38

def decorate
  self
end

#decorated?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/epi_decorator/base.rb', line 42

def decorated?
  true
end

#instance_of?(klass) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/epi_decorator/base.rb', line 34

def instance_of?(klass)
  super || object.instance_of?(klass)
end

#kind_of?(klass) ⇒ Boolean Also known as: is_a?

Returns:

  • (Boolean)


29
30
31
# File 'lib/epi_decorator/base.rb', line 29

def kind_of?(klass)
  super || object.kind_of?(klass)
end