Module: Filemaker::Model::Fields

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/filemaker/model/fields.rb

Overview

‘Fields` help to give `Model` their perceived schema. To find out your fields, use `Model.fm_fields` or use Rails generator like:

rails generate filemaker:model filename database layout

Examples:

class Model
  include Filemaker::Model

  string :id, identity: true
  string :title, fm_name: 'A_Title', default: 'Untitled'
  money  :salary
end

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#apply_defaultsObject

Apply default value when you instantiate a new model.



29
30
31
32
33
34
# File 'lib/filemaker/model/fields.rb', line 29

def apply_defaults
  attribute_names.each do |name|
    field = fields[name]
    instance_variable_set("@#{name}", field.default_value)
  end
end

#attribute_namesObject



36
37
38
# File 'lib/filemaker/model/fields.rb', line 36

def attribute_names
  self.class.attribute_names
end

#attributesObject



44
45
46
47
48
49
50
51
52
# File 'lib/filemaker/model/fields.rb', line 44

def attributes
  fields.keys.each_with_object({}) do |field, hash|
    # Attributes must be strings, not symbols - See
    # http://api.rubyonrails.org/classes/ActiveModel/Serialization.html
    hash[field.to_s] = instance_variable_get("@#{field}")

    # If we use public_send(field) will encounter Stack Too Deep
  end
end

#fm_namesObject



40
41
42
# File 'lib/filemaker/model/fields.rb', line 40

def fm_names
  fields.values.map(&:fm_name)
end