Class: Representable::Definition
- Inherits:
-
Object
- Object
- Representable::Definition
- Extended by:
- Forwardable
- Includes:
- Cloneable
- 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.
Instance Method Summary collapse
- #array? ⇒ Boolean
- #clone ⇒ Object
- #create_binding(*args) ⇒ Object
- #default_for(value) ⇒ Object
- #has_default? ⇒ Boolean
- #hash? ⇒ Boolean
-
#initialize(sym, options = {}, &block) ⇒ Definition
constructor
A new instance of Definition.
- #merge!(options, &block) ⇒ Object
- #representable? ⇒ Boolean
- #representer_module ⇒ Object
- #setter ⇒ Object
- #skipable_empty_value?(value) ⇒ Boolean
- #typed? ⇒ Boolean
Constructor Details
#initialize(sym, options = {}, &block) ⇒ Definition
Returns a new instance of Definition.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/representable/definition.rb', line 12 def initialize(sym, ={}, &block) @options = {} # @options = Inheritable::Hash.new # allows deep cloning. we then had to set Pipeline cloneable. @name = sym.to_s = .clone # defaults: [:parse_filter] = Pipeline[*[:parse_filter]] [:render_filter] = Pipeline[*[:render_filter]] [:as] ||= @name setup!(, &block) end |
Instance Attribute Details
#name ⇒ Object (readonly) Also known as: getter
Returns the value of attribute name.
9 10 11 |
# File 'lib/representable/definition.rb', line 9 def name @name end |
Instance Method Details
#array? ⇒ Boolean
56 57 58 |
# File 'lib/representable/definition.rb', line 56 def array? self[:collection] end |
#clone ⇒ Object
39 40 41 |
# File 'lib/representable/definition.rb', line 39 def clone self.class.new(name, @options.clone) end |
#create_binding(*args) ⇒ Object
82 83 84 |
# File 'lib/representable/definition.rb', line 82 def create_binding(*args) self[:binding].call(self, *args) end |
#default_for(value) ⇒ Object
64 65 66 67 |
# File 'lib/representable/definition.rb', line 64 def default_for(value) return self[:default] if skipable_empty_value?(value) value end |
#has_default? ⇒ Boolean
69 70 71 |
# File 'lib/representable/definition.rb', line 69 def has_default? @options.has_key?(:default) end |
#hash? ⇒ Boolean
60 61 62 |
# File 'lib/representable/definition.rb', line 60 def hash? self[:hash] end |
#merge!(options, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/representable/definition.rb', line 26 def merge!(, &block) = .clone [:parse_filter] = @options[:parse_filter].push(*[:parse_filter]) [:render_filter] = @options[:render_filter].push(*[:render_filter]) setup!(, &block) # FIXME: this doesn't yield :as etc. self end |
#representable? ⇒ Boolean
51 52 53 54 |
# File 'lib/representable/definition.rb', line 51 def representable? return if self[:representable] === false self[:representable] or typed? end |
#representer_module ⇒ Object
73 74 75 |
# File 'lib/representable/definition.rb', line 73 def representer_module @options[:extend] end |
#setter ⇒ Object
43 44 45 |
# File 'lib/representable/definition.rb', line 43 def setter :"#{name}=" end |
#skipable_empty_value?(value) ⇒ Boolean
77 78 79 80 |
# File 'lib/representable/definition.rb', line 77 def skipable_empty_value?(value) return true if array? and self[:render_empty] == false and value and value.size == 0 # TODO: change in 2.0, don't render emtpy. value.nil? and not self[:render_nil] end |
#typed? ⇒ Boolean
47 48 49 |
# File 'lib/representable/definition.rb', line 47 def typed? self[:class] or self[:extend] or self[:instance] end |