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



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

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

#attribute_namesObject



53
54
55
# File 'lib/filemaker/model/fields.rb', line 53

def attribute_names
  fields.keys
end

#create_accessors(name) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/filemaker/model/fields.rb', line 76

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

  define_attribute_methods name

  # Reader
  define_method(name) { attributes[name] }

  # Writer - We try to map to the correct type, if not we just return
  # original.
  define_method("#{name}=") do |value|
    public_send("#{name}_will_change!") unless value == attributes[name]
    attributes[name] = fields[name].coerce(value)
  end

  # Predicate
  define_method("#{name}?") do
    attributes[name] == true || attributes[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.



99
100
101
102
103
104
# File 'lib/filemaker/model/fields.rb', line 99

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