Class: Ciesta::Field
- Inherits:
-
Object
- Object
- Ciesta::Field
- Defined in:
- lib/ciesta/field.rb
Overview
Class for storing form field
Constant Summary collapse
- DEFAULT_TYPE =
Default type when another one is not passed
Ciesta::Types::Any
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
Field name.
-
#type ⇒ Ciesta::Types::Declaration
readonly
Field type.
Instance Method Summary collapse
-
#bind(obj) ⇒ Object
private
Binds current field to object.
-
#clear! ⇒ Object
Clear field.
-
#initialize(name, **options) ⇒ Field
constructor
Constructor.
-
#value ⇒ any
Returns current value.
-
#value=(val) ⇒ Object
Sets a new value for field.
Constructor Details
#initialize(name, **options) ⇒ Field
Constructor
19 20 21 22 23 |
# File 'lib/ciesta/field.rb', line 19 def initialize(name, **) @name = name.to_sym @type = .delete(:type) || DEFAULT_TYPE @default = .delete(:default) end |
Instance Attribute Details
#name ⇒ Symbol (readonly)
Field name
7 8 9 |
# File 'lib/ciesta/field.rb', line 7 def name @name end |
#type ⇒ Ciesta::Types::Declaration (readonly)
Field type
7 8 9 |
# File 'lib/ciesta/field.rb', line 7 def type @type end |
Instance Method Details
#bind(obj) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Binds current field to object
50 51 52 |
# File 'lib/ciesta/field.rb', line 50 def bind(obj) @binding = obj end |
#clear! ⇒ Object
Clear field
55 56 57 |
# File 'lib/ciesta/field.rb', line 55 def clear! @value = nil end |
#value ⇒ any
Returns current value
40 41 42 43 44 |
# File 'lib/ciesta/field.rb', line 40 def value return @value if @was_set @value || default end |
#value=(val) ⇒ Object
Sets a new value for field
30 31 32 33 34 35 |
# File 'lib/ciesta/field.rb', line 30 def value=(val) @value = type[val] @was_set = true rescue Dry::Types::ConstraintError raise Ciesta::ViolatesConstraints, "#{val} is not a valid #{name} (#{type.name})" end |