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_valueObject (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_nameObject (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

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#typeObject (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.

Date and DateTime will be special. If the value is a String, the query may be ‘2016’, ‘3/2016’ or ‘3/24/2016’ for example.



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

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
    return value.to_s if value.is_a? String
    Date.parse(value.to_s)
  elsif @type == DateTime
    return value if value.is_a? DateTime
    return value.to_s if value.is_a? String
    DateTime.parse(value.to_s)
  else
    value
  end
rescue Exception => e
  warn "[#{e.message}] Could not coerce #{name}: #{value}"
  value
end