Class: Representable::Definition
- Inherits:
-
Object
- Object
- Representable::Definition
- Defined in:
- lib/representable/definition.rb
Overview
Created at class compile time. Keeps configuration options for one property.
Instance Attribute Summary collapse
-
#name ⇒ Object
(also: #getter)
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #array? ⇒ Boolean
- #attribute ⇒ Object
- #binding ⇒ Object
- #clone ⇒ Object
- #content ⇒ Object
- #create_binding(*args) ⇒ Object
- #default ⇒ Object
- #default_for(value) ⇒ Object
- #from ⇒ Object
- #has_default? ⇒ Boolean
- #hash? ⇒ Boolean
-
#initialize(sym, options = {}) ⇒ Definition
constructor
A new instance of Definition.
- #representer_module ⇒ Object
- #setter ⇒ Object
- #skipable_nil_value?(value) ⇒ Boolean
- #sought_type ⇒ Object
- #sync? ⇒ Boolean
- #typed? ⇒ Boolean
Constructor Details
#initialize(sym, options = {}) ⇒ Definition
Returns a new instance of Definition.
7 8 9 10 11 12 |
# File 'lib/representable/definition.rb', line 7 def initialize(sym, ={}) @name = sym.to_s @options = @options[:default] ||= [] if array? # FIXME: move to CollectionBinding! end |
Instance Attribute Details
#name ⇒ Object (readonly) Also known as: getter
Returns the value of attribute name.
4 5 6 |
# File 'lib/representable/definition.rb', line 4 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/representable/definition.rb', line 4 def @options end |
Instance Method Details
#array? ⇒ Boolean
26 27 28 |
# File 'lib/representable/definition.rb', line 26 def array? [:collection] end |
#attribute ⇒ Object
56 57 58 |
# File 'lib/representable/definition.rb', line 56 def attribute [:attribute] end |
#binding ⇒ Object
72 73 74 |
# File 'lib/representable/definition.rb', line 72 def binding [:binding] end |
#clone ⇒ Object
14 15 16 |
# File 'lib/representable/definition.rb', line 14 def clone self.class.new(name, .clone) # DISCUSS: make generic Definition.cloned_attribute that passes list to constructor. end |
#content ⇒ Object
60 61 62 |
# File 'lib/representable/definition.rb', line 60 def content [:content] end |
#create_binding(*args) ⇒ Object
76 77 78 |
# File 'lib/representable/definition.rb', line 76 def create_binding(*args) binding.call(self, *args) end |
#default ⇒ Object
68 69 70 |
# File 'lib/representable/definition.rb', line 68 def default [:default] end |
#default_for(value) ⇒ Object
43 44 45 46 |
# File 'lib/representable/definition.rb', line 43 def default_for(value) return default if skipable_nil_value?(value) value end |
#from ⇒ Object
38 39 40 41 |
# File 'lib/representable/definition.rb', line 38 def from # TODO: deprecate :from. ([:from] || [:as] || name).to_s end |
#has_default? ⇒ Boolean
48 49 50 |
# File 'lib/representable/definition.rb', line 48 def has_default? .has_key?(:default) end |
#hash? ⇒ Boolean
30 31 32 |
# File 'lib/representable/definition.rb', line 30 def hash? [:hash] end |
#representer_module ⇒ Object
52 53 54 |
# File 'lib/representable/definition.rb', line 52 def representer_module [:extend] or [:decorator] end |
#setter ⇒ Object
18 19 20 |
# File 'lib/representable/definition.rb', line 18 def setter :"#{name}=" end |
#skipable_nil_value?(value) ⇒ Boolean
64 65 66 |
# File 'lib/representable/definition.rb', line 64 def skipable_nil_value?(value) value.nil? and not [:render_nil] end |
#sought_type ⇒ Object
34 35 36 |
# File 'lib/representable/definition.rb', line 34 def sought_type [:class] end |
#sync? ⇒ Boolean
80 81 82 |
# File 'lib/representable/definition.rb', line 80 def sync? [:parse_strategy] == :sync end |
#typed? ⇒ Boolean
22 23 24 |
# File 'lib/representable/definition.rb', line 22 def typed? sought_type.is_a?(Class) or representer_module or [:instance] # also true if only :extend is set, for people who want solely rendering. end |