Module: Filemaker::Model::Fields::ClassMethods

Defined in:
lib/filemaker/model/fields.rb

Instance Method Summary collapse

Instance Method Details

#add_field(name, type, options) ⇒ Object



71
72
73
74
75
# File 'lib/filemaker/model/fields.rb', line 71

def add_field(name, type, options)
  name = name.to_s.freeze
  fields[name] = Filemaker::Model::Field.new(name, type, options)
  self.identity = fields[name] if options[:identity]
end

#attribute_namesObject



55
56
57
# File 'lib/filemaker/model/fields.rb', line 55

def attribute_names
  fields.keys
end

#create_accessors(name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/filemaker/model/fields.rb', line 77

def create_accessors(name)
  # Normalize it so ActiveModel::Serialization can work
  name = name.to_s

  define_attribute_methods name

  # Reader
  define_method(name) do
    instance_variable_get("@#{name}")
  end

  # Writer - We try to map to the correct type, if not we just return
  # original.
  define_method("#{name}=") do |value|
    new_value = fields[name].serialize_for_update(value)

    public_send("#{name}_will_change!") \
      if new_value != public_send(name)

    instance_variable_set("@#{name}", new_value)
  end

  # Predicate
  define_method("#{name}?") do
    # See ActiveRecord::AttributeMethods::Query implementation
    public_send(name) == true || public_send(name).present?
  end
end

#find_field_by_name(name) ⇒ Object

Find FileMaker’s real name given either the attribute name or the real FileMaker name.



108
109
110
111
112
113
# File 'lib/filemaker/model/fields.rb', line 108

def find_field_by_name(name)
  name = name.to_s
  fields.values.find do |f|
    f.name == name || f.fm_name == name
  end
end