Module: Representable::Declarative
- Defined in:
- lib/representable/declarative.rb
Instance Method Summary collapse
-
#build_inline(base, features, name, options, &block) ⇒ Object
DISCUSS: separate module?.
- #collection(name, options = {}, &block) ⇒ Object
- #hash(name = nil, options = {}, &block) ⇒ Object
-
#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.
- #property(name, options = {}, &block) ⇒ Object
- #representable_attrs ⇒ Object
- #representation_wrap=(name) ⇒ Object
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, , &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, ={}, &block) [:collection] = true # FIXME: don't override original. property(name, , &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, ={}, &block) return super() unless name # allow Object.hash. [:hash] = true property(name, , &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, ={}, &block) = .merge( :use_decorator => true, :getter => lambda { |*| self }, :setter => lambda { |*| }, :instance => lambda { |*| self } ) # DISCUSS: should this be a macro just as :parse_strategy? property(name, , &block) end |
#property(name, options = {}, &block) ⇒ Object
35 36 37 38 39 |
# File 'lib/representable/declarative.rb', line 35 def property(name, ={}, &block) representable_attrs.add(name, ) do || # handles :inherit. build_definition(name, , &block) end end |
#representable_attrs ⇒ Object
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 |