Class: Representable::Decorator

Inherits:
Object
  • Object
show all
Includes:
Representable
Defined in:
lib/representable/decorator.rb

Constant Summary collapse

Coercion =

TODO: deprecate for 1.7.

Representable::Coercion

Constants included from Representable

VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Representable

included

Constructor Details

#initialize(represented) ⇒ Decorator

Returns a new instance of Decorator.



38
39
40
# File 'lib/representable/decorator.rb', line 38

def initialize(represented)
  @represented = represented
end

Instance Attribute Details

#representedObject (readonly) Also known as: decorated

Returns the value of attribute represented.



3
4
5
# File 'lib/representable/decorator.rb', line 3

def represented
  @represented
end

Class Method Details

.inline_representer(base_module, name, options, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/representable/decorator.rb', line 10

def self.inline_representer(base_module, name, options, &block)
  # FIXME: it is wrong to inherit from self here as we just want to "inherit" the included modules but nothing else.
  Class.new(self).tap do |decorator|
    decorator.class_eval do # Ruby 1.8.7 wouldn't properly execute the block passed to Class.new!
      # Remove parent's property definitions before defining the inline ones. #FIXME: don't inherit from self, remove those 2 lines.
      representable_attrs.clear
      representable_attrs.inheritable_arrays.clear

      include base_module
      instance_exec &block
    end
  end
end

.nested(name, options = {}, &block) ⇒ Object

Allows you to nest a block of properties in a separate section while still mapping them to the outer object.



25
26
27
28
29
30
31
32
33
34
# File 'lib/representable/decorator.rb', line 25

def self.nested(name, options={}, &block)
  options = options.merge(
    :nested   => true,
    :getter   => lambda { |*| self },
    :setter   => lambda { |*| },
    :instance => lambda { |*| self }
  )

  property(name, options, &block)
end

.prepare(represented) ⇒ Object



6
7
8
# File 'lib/representable/decorator.rb', line 6

def self.prepare(represented)
  new(represented)
end