Class: Filemaker::Model::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Field.



6
7
8
9
10
11
12
13
14
# File 'lib/filemaker/model/field.rb', line 6

def initialize(name, type, options = {})
  @name = name
  @type = type
  @default_value = coerce(options.fetch(:default) { nil })

  # We need to downcase because Filemaker::Record is
  # HashWithIndifferentAndCaseInsensitiveAccess
  @fm_name = (options.fetch(:fm_name) { name }).to_s.downcase
end

Instance Attribute Details

#default_value ⇒ Object (readonly)

Returns the value of attribute default_value.



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

def default_value
  @default_value
end

#fm_name ⇒ Object (readonly)

Returns the value of attribute fm_name.



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

def fm_name
  @fm_name
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#coerce(value) ⇒ Object

From FileMaker to Ruby.

If the value is == (match empty) or =* (match record), then we will skip coercion.



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

def coerce(value)
  return nil if value.nil?
  return value if value == '==' || value == '=*'
  return value if value =~ /(\.\.\.)/

  if @type == String
    value.to_s
  elsif @type == Integer
    value.to_i
  elsif @type == BigDecimal
    BigDecimal.new(value.to_s)
  elsif @type == Date
    return value if value.is_a? Date
    Date.parse(value.to_s)
  elsif @type == DateTime
    return value if value.is_a? DateTime
    DateTime.parse(value.to_s)
  else
    value
  end
rescue
  warn "Could not coerce #{name}: #{value}"
  value
end