Class: FmRest::Spyke::Base::OrmAdapter

Inherits:
OrmAdapter::Base
  • Object
show all
Defined in:
lib/orm_adapter/fmrest/adapter.rb

Instance Method Summary collapse

Instance Method Details

#column_namesObject

Get a list of the attribute names (field names) in the Filemaker Layout. Map attribute names



24
25
26
27
28
29
30
31
32
33
# File 'lib/orm_adapter/fmrest/adapter.rb', line 24

def column_names
  return [] unless klass.respond_to? :layout

  field_meta = layout_meta klass.layout
  fields = field_meta.map{|f| f['name']}
  return fields unless klass.respond_to? :mapped_attributes

  # map attribute names
  fields + klass.mapped_attributes.keys - klass.mapped_attributes.values
end

#create!(attributes = {}) ⇒ Object

Create a model using attributes



84
85
86
# File 'lib/orm_adapter/fmrest/adapter.rb', line 84

def create!(attributes = {})
  klass.new(attributes).save
end

#destroy(object) ⇒ Object

Destroy an instance by passing in the instance itself.



89
90
91
# File 'lib/orm_adapter/fmrest/adapter.rb', line 89

def destroy(object)
  object.destroy if valid_object?(object)
end

#find_all(options = {}) ⇒ Object

Find all models, optionally matching conditions, and specifying order

Returns:

  • Enumerable

See Also:

  • for how to specify order and conditions


79
80
81
# File 'lib/orm_adapter/fmrest/adapter.rb', line 79

def find_all(options = {})
  base_relation(options)
end

#find_first(options = {}) ⇒ Object

Find the first instance, optionally matching conditions, and specifying order

You can call with just conditions, providing a hash

User.to_adapter.find_first :name => "Fred", :age => 23

Or you can specify :order, and :conditions as keys

User.to_adapter.find_first :conditions => {:name => "Fred", :age => 23}
User.to_adapter.find_first :order => [:age, :desc]
User.to_adapter.find_first :order => :name, :conditions => {:age => 18}

When specifying :order, it may be

  • a single arg e.g. :order => :name

  • a single pair with :asc, or :desc as last, e.g. :order => [:name, :desc]

  • an array of single args or pairs (with :asc or :desc as last), e.g. :order => [[:name, :asc], [:age, :desc]]



72
73
74
# File 'lib/orm_adapter/fmrest/adapter.rb', line 72

def find_first(options = {})
  base_relation(options).first
end

#get(id) ⇒ Object

Get an instance by id of the model. Returns nil if a model is not found. This should comply with ActiveModel#to_key API, i.e.:

User.to_adapter.get(@user.to_key) == @user


49
50
51
52
53
# File 'lib/orm_adapter/fmrest/adapter.rb', line 49

def get(id)
  klass.find wrap_key(id)
rescue FmRest::APIError::RecordMissingError
  nil
end

#get!(id) ⇒ Object

Get an instance by id of the model. Raises an error if a model is not found. This should comply with ActiveModel#to_key API, i.e.:

User.to_adapter.get!(@user.to_key) == @user


40
41
42
# File 'lib/orm_adapter/fmrest/adapter.rb', line 40

def get!(id)
  klass.find wrap_key(id)
end