Class: Ciesta::Field

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name, **options) ⇒ Field

Constructor



19
20
21
22
23
# File 'lib/ciesta/field.rb', line 19

def initialize(name, **options)
  @name = name.to_sym
  @type = options.delete(:type) || DEFAULT_TYPE
  @default = options.delete(:default)
end

Instance Attribute Details

#nameSymbol (readonly)

Field name



7
8
9
# File 'lib/ciesta/field.rb', line 7

def name
  @name
end

#typeCiesta::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

#valueany

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

Raises:

  • Ciesta::ViolatesConstraints



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