module Filemaker::Model::Relations::ClassMethods

Public Instance Methods

belongs_to(name, options = {}) click to toggle source
# File lib/filemaker/model/relations.rb, line 20
def belongs_to(name, options = {})
  relate_single(Relations::BelongsTo, name, options)
end
has_many(name, options = {}) click to toggle source
# File lib/filemaker/model/relations.rb, line 16
def has_many(name, options = {})
  relate_collection(Relations::HasMany, name, options)
end
has_portal(name, options = {}) click to toggle source
# File lib/filemaker/model/relations.rb, line 24
def has_portal(name, options = {})
  relate_portal(Relations::HasPortal, name, options)
end

Protected Instance Methods

relate_collection(type, name, options) click to toggle source

For collection, we will return criteria and not cache anything.

# File lib/filemaker/model/relations.rb, line 72
def relate_collection(type, name, options)
  name = name.to_s

  define_method(name) do
    type.new(self, name, options)
  end
end
relate_portal(type, name, options) click to toggle source
# File lib/filemaker/model/relations.rb, line 30
def relate_portal(type, name, options)
  name = name.to_s

  define_method(name) do
    type.new(self, name, options)
  end
end
relate_single(type, name, options) click to toggle source

Get the single model and cache it to `relations`

# File lib/filemaker/model/relations.rb, line 39
def relate_single(type, name, options)
  name = name.to_s

  # Reader
  #
  # @example Reload the record
  #   job.company(true)
  define_method(name) do |force_reload = false|
    if force_reload
      @relations[name] = type.init(self, name, options)
    else
      @relations[name] ||= type.init(self, name, options)
    end
  end

  # Writer
  #
  # TODO: What happen if `object` is brand new? We would want to save
  # the child as well as the parent. We need to wait for the child to
  # save and return the identity ID, then we update the parent's
  # reference_key.
  define_method("#{name}=") do |object|
    return nil if object.nil?

    @relations[name] = object
  end

  # Creator
  # define_method("create_#{name}") do |attrs = {}|
  # end
end