Module: Representable::Declarative

Defined in:
lib/representable/declarative.rb

Instance Method Summary collapse

Instance Method Details

#build_inline(base, features, name, options, &block) ⇒ Object

DISCUSS: separate module?


41
42
43
44
45
46
47
48
49
# File 'lib/representable/declarative.rb', line 41

def build_inline(base, features, name, options, &block) # DISCUSS: separate module?
  Module.new do
    include Representable
    feature *features # Representable::JSON or similar.
    include base if base # base when :inherit, or in decorator.

    module_eval &block
  end
end

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


11
12
13
14
# File 'lib/representable/declarative.rb', line 11

def collection(name, options={}, &block)
  options[:collection] = true # FIXME: don't override original.
  property(name, options, &block)
end

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


16
17
18
19
20
21
# File 'lib/representable/declarative.rb', line 16

def hash(name=nil, options={}, &block)
  return super() unless name  # allow Object.hash.

  options[:hash] = true
  property(name, options, &block)
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.


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

def nested(name, options={}, &block)
  options = options.merge(
    :use_decorator => true,
    :getter        => lambda { |*| self },
    :setter        => lambda { |*| },
    :instance      => lambda { |*| self }
  ) # DISCUSS: should this be a macro just as :parse_strategy?

  property(name, options, &block)
end

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


35
36
37
38
39
# File 'lib/representable/declarative.rb', line 35

def property(name, options={}, &block)
  representable_attrs.add(name, options) do |default_options| # handles :inherit.
    build_definition(name, default_options, &block)
  end
end

#representable_attrsObject


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

def representable_attrs
  @representable_attrs ||= build_config
end

#representation_wrap=(name) ⇒ Object


7
8
9
# File 'lib/representable/declarative.rb', line 7

def representation_wrap=(name)
  representable_attrs.wrap = name
end