Class: Id::Field::Definition
- Inherits:
-
Object
- Object
- Id::Field::Definition
- Defined in:
- lib/id/field/definition.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #default ⇒ Object
- #default! ⇒ Object
-
#initialize(name, options) ⇒ Definition
constructor
A new instance of Definition.
- #key ⇒ Object
- #optional? ⇒ Boolean
- #to_s ⇒ Object
- #type ⇒ Object
- #value(data) ⇒ Object
Constructor Details
#initialize(name, options) ⇒ Definition
Returns a new instance of Definition.
3 4 5 6 |
# File 'lib/id/field/definition.rb', line 3 def initialize(name, ) @name = name @options = end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
42 43 44 |
# File 'lib/id/field/definition.rb', line 42 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
42 43 44 |
# File 'lib/id/field/definition.rb', line 42 def @options end |
Instance Method Details
#default ⇒ Object
8 9 10 |
# File 'lib/id/field/definition.rb', line 8 def default .fetch(:default, nil) end |
#default! ⇒ Object
12 13 14 |
# File 'lib/id/field/definition.rb', line 12 def default! default.is_a?(Proc) ? default.call : default end |
#key ⇒ Object
16 17 18 |
# File 'lib/id/field/definition.rb', line 16 def key .fetch(:key, name).to_s end |
#optional? ⇒ Boolean
24 25 26 |
# File 'lib/id/field/definition.rb', line 24 def optional? .fetch(:optional, false) end |
#to_s ⇒ Object
28 29 30 |
# File 'lib/id/field/definition.rb', line 28 def to_s Id::Field::Summary.new(self).to_s end |
#type ⇒ Object
20 21 22 |
# File 'lib/id/field/definition.rb', line 20 def type .fetch(:type, Object) end |
#value(data) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/id/field/definition.rb', line 32 def value(data) # the following code is a bit verbose but can't use || as false is valid here value = data[key] value = data[key.to_sym] if value.nil? value = default! if value.nil? value = Option[value] if optional? Id::Coercion.coerce(value, type) unless value.nil? end |