Module: Representable::Declarative

Includes:
Declarative::Heritage::DSL, Declarative::Schema::DSL, Declarative::Schema::Feature
Defined in:
lib/representable/declarative.rb

Constant Summary collapse

NestedBuilder =
->(options) do
  Module.new do
    include Representable # FIXME: do we really need this?
    feature(*options[:_features])
    include(*options[:_base]) # base when :inherit, or in decorator.

    module_eval(&options[:_block])
  end
end

Instance Method Summary collapse

Instance Method Details

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



9
10
11
# File 'lib/representable/declarative.rb', line 9

def collection(name, options={}, &block)
  property(name, options.merge(collection: true), &block)
end

#default_nested_classObject



42
43
44
# File 'lib/representable/declarative.rb', line 42

def default_nested_class
  Module.new # FIXME: make that unnecessary in Declarative
end

#definitionsObject Also known as: representable_attrs



61
62
63
# File 'lib/representable/declarative.rb', line 61

def definitions
  @definitions ||= Config.new(Representable::Definition)
end

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



13
14
15
16
17
18
# File 'lib/representable/declarative.rb', line 13

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 original object.



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

def nested(name, options={}, &block)
  options = options.merge(
    getter:   ->(opts) { self },
    setter:   ->(opts) { },
    instance: ->(opts) { self },
  )

  if block
    options[:_nested_builder] = Decorator.nested_builder
    options[:_base]           = Decorator.default_nested_class
  end

  property(name, options, &block)
end

#nested_builderObject



57
58
59
# File 'lib/representable/declarative.rb', line 57

def nested_builder
  NestedBuilder
end

#representation_wrap=(name) ⇒ Object



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

def representation_wrap=(name)
  heritage.record(:representation_wrap=, name)

  definitions.wrap = name
end