Module: Representable::DSLAdditions

Defined in:
lib/representable.rb

Overview

Internal module for DSL sugar that should not go into the core library.

Instance Method Summary collapse

Instance Method Details

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

DISCUSS: separate module?



181
182
183
184
185
186
# File 'lib/representable.rb', line 181

def inline_representer(base_module, name, options, &block) # DISCUSS: separate module?
  Module.new do
    include *base_module # Representable::JSON or similar.
    instance_exec &block
  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.



156
157
158
159
160
161
162
163
164
165
# File 'lib/representable.rb', line 156

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

  property(name, options, &block)
end

#property(name, options = {}, &block) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/representable.rb', line 167

def property(name, options={}, &block)
  parent = representable_attrs[name]

  if block_given?
    options[:extend] = inline_representer_for(parent, name, options, &block) # FIXME: passing parent sucks since we don't use it all the time.
  end

  if options[:inherit]
    parent.options.merge!(options) and return parent
  end # FIXME: can we handle this in super/Definition.new ?

  super
end