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



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

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

#default_nested_classObject



47
48
49
# File 'lib/representable/declarative.rb', line 47

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

#definitionsObject Also known as: representable_attrs



66
67
68
# File 'lib/representable/declarative.rb', line 66

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

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



18
19
20
21
22
23
# File 'lib/representable/declarative.rb', line 18

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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/representable/declarative.rb', line 27

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



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

def nested_builder
  NestedBuilder
end

#representation_wrap=(name) ⇒ Object



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

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

  definitions.wrap = name
end