Class: Ripple::Property
Overview
Encapsulates a single property on your Ripple::Document class.
Instance Attribute Summary collapse
-
#key ⇒ Symbol
readonly
The key of this property in the Document.
-
#options ⇒ Hash
readonly
Configuration options.
-
#type ⇒ Class
readonly
The Ruby type of property.
Instance Method Summary collapse
-
#default ⇒ Object
The default value for this property if defined, or nil.
-
#initialize(key, type, options = {}) ⇒ Property
constructor
Create a new document property.
-
#type_cast(value) ⇒ Object
Attempt to coerce the passed value into this property’s type.
-
#validation_options ⇒ Hash
Options appropriate for the validates class method.
Constructor Details
#initialize(key, type, options = {}) ⇒ Property
Create a new document property.
42 43 44 45 46 |
# File 'lib/ripple/properties.rb', line 42 def initialize(key, type, ={}) @options = . @key = key.to_sym @type = type end |
Instance Attribute Details
#key ⇒ Symbol (readonly)
Returns the key of this property in the Document.
30 31 32 |
# File 'lib/ripple/properties.rb', line 30 def key @key end |
#options ⇒ Hash (readonly)
Returns configuration options.
34 35 36 |
# File 'lib/ripple/properties.rb', line 34 def @options end |
#type ⇒ Class (readonly)
Returns the Ruby type of property.
32 33 34 |
# File 'lib/ripple/properties.rb', line 32 def type @type end |
Instance Method Details
#default ⇒ Object
Returns The default value for this property if defined, or nil.
49 50 51 52 53 54 55 |
# File 'lib/ripple/properties.rb', line 49 def default default = [:default] default = default.dup if default.duplicable? return nil if default.nil? type_cast(default.respond_to?(:call) ? default.call : default) end |
#type_cast(value) ⇒ Object
Attempt to coerce the passed value into this property’s type
66 67 68 69 70 71 72 |
# File 'lib/ripple/properties.rb', line 66 def type_cast(value) if @type.respond_to?(:ripple_cast) @type.ripple_cast(value) else value end end |
#validation_options ⇒ Hash
Returns options appropriate for the validates class method.
58 59 60 |
# File 'lib/ripple/properties.rb', line 58 def @options.dup.except(:default) end |