Class: Altair::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/altair/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Field

Returns a new instance of Field.



9
10
11
12
# File 'lib/altair/field.rb', line 9

def initialize(options)
  @type = options[:type]
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#parse!(value, args = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/altair/field.rb', line 14

def parse!(value, args = {})
  begin
    resolve_proc(Altair.converter[value.class][type], value)
  rescue
    raise ConversionError.new(type, value)
  end
end

#validate!(value, args = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/altair/field.rb', line 22

def validate!(value, args = {})
  Altair.validators.each do |name, validator|
    if options.has_key? name
      raise ValidationError.new(name, value) unless validator.call(resolve_proc(options[name]), value)
    end
  end

  value
end