Class: RFilemaker::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, result_set) ⇒ Field

Returns a new instance of Field.



8
9
10
11
12
13
# File 'lib/rfilemaker/field.rb', line 8

def initialize(xml, result_set)
  @name       = xml['NAME']
  @type       = xml['TYPE'] ? xml['TYPE'].downcase.to_sym : :none
  @empty_ok   = xml['EMPTYOK'] == 'YES'
  @result_set = result_set
end

Instance Attribute Details

#nameObject (readonly)

Name of the field



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

def name
  @name
end

#typeObject (readonly)

Type of the field as symbol



6
7
8
# File 'lib/rfilemaker/field.rb', line 6

def type
  @type
end

Instance Method Details

#coerce(value) ⇒ Object

Coerce a value to the Ruby equivalent of the Filemaker Type

  • ‘DATE’ gets converted to Date

  • ‘TIME’ gets converted to DateTime

  • ‘TIMESTAMP’ gets converted to DateTime

  • ‘NUMBER’ gets converted to float or integer

  • everything else gets converted to a string



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rfilemaker/field.rb', line 22

def coerce(value)
  return nil if value.nil? || value == ''
  
  case type
    when :date
      Date.strptime(value, @result_set.date_format) unless @empty_ok
    when :time
      DateTime.strptime("1/1/-4712 #{value}", @result_set.time_format) unless @empty_ok
    when :timestamp
      DateTime.strptime(value, @result_set.time_format) unless @empty_ok
    when :number
      if value.include?('.')
        value.to_f
      elsif value.include?(',')
        value.gsub(/,/, '.').to_f
      else
        value.to_i
      end
    else
      value.to_s
  end
end