Class: Conformity::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Field

Returns a new instance of Field.



6
7
8
9
10
# File 'lib/conformity/field.rb', line 6

def initialize(name, options = {})
  @name = name
  @options = options[:options]
  @required = options[:required] || false
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/conformity/field.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/conformity/field.rb', line 3

def options
  @options
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/conformity/field.rb', line 4

def type
  @type
end

Instance Method Details

#required?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/conformity/field.rb', line 12

def required?
  @required
end

#valueObject



16
17
18
19
20
21
22
# File 'lib/conformity/field.rb', line 16

def value
  if @value
    @value
  elsif required?
    raise FieldError, "Required field '#{name}' not set"
  end
end

#value=(value) ⇒ Object



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

def value=(value)
  if @options && !@options.include?(value)
    raise FieldError, "#{value} not in #{@options}"
  end

  @value = value
end