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.

[View source]

9
10
11
12
13
14
15
16
17
# File 'lib/filemaker/model/field.rb', line 9

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.


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

def default_value
  @default_value
end

#fm_nameObject (readonly)

Returns the value of attribute fm_name.


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

def fm_name
  @fm_name
end

#nameObject (readonly)

Returns the value of attribute name.


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

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.


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

def type
  @type
end

Instance Method Details

#coerce(value, klass = nil) ⇒ 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.

[View source]

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/filemaker/model/field.rb', line 26

def coerce(value, klass = nil)
  return nil if value.nil?
  return value if 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)
  elsif @type == Filemaker::Model::Types::Email
    return value if value.is_a? Filemaker::Model::Types::Email
    Filemaker::Model::Types::Email.new(value)
  elsif @type == Filemaker::Model::Types::Attachment
    return value if value.is_a? Filemaker::Model::Types::Attachment
    Filemaker::Model::Types::Attachment.new(value, klass)
  else
    value
  end
rescue StandardError => e
  warn "[#{e.message}] Could not coerce #{name}: #{value}"
  value
end