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
- #clone ⇒ 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
- #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 = [: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
55 56 57 |
# File 'lib/representable/definition.rb', line 55 def attribute [:attribute] 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 |
#default ⇒ Object
63 64 65 |
# File 'lib/representable/definition.rb', line 63 def default [:default] end |
#default_for(value) ⇒ Object
42 43 44 45 |
# File 'lib/representable/definition.rb', line 42 def default_for(value) return default if skipable_nil_value?(value) value end |
#from ⇒ Object
38 39 40 |
# File 'lib/representable/definition.rb', line 38 def from ([:from] || name).to_s end |
#has_default? ⇒ Boolean
47 48 49 |
# File 'lib/representable/definition.rb', line 47 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
51 52 53 |
# File 'lib/representable/definition.rb', line 51 def representer_module [:extend] end |
#setter ⇒ Object
18 19 20 |
# File 'lib/representable/definition.rb', line 18 def setter :"#{name}=" end |
#skipable_nil_value?(value) ⇒ Boolean
59 60 61 |
# File 'lib/representable/definition.rb', line 59 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 |
#typed? ⇒ Boolean
22 23 24 |
# File 'lib/representable/definition.rb', line 22 def typed? sought_type.is_a?(Class) or representer_module # also true if only :extend is set, for people who want solely rendering. end |