class FmRest::Spyke::Base::OrmAdapter
Public Instance Methods
Get a list of the attribute names (field names) in the Filemaker Layout. Map attribute names
# 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 a model using attributes
# File lib/orm_adapter/fmrest/adapter.rb, line 84 def create!(attributes = {}) klass.new(attributes).save end
Destroy an instance by passing in the instance itself.
# File lib/orm_adapter/fmrest/adapter.rb, line 89 def destroy(object) object.destroy if valid_object?(object) end
Find all models, optionally matching conditions, and specifying order @see OrmAdapter::Base#find_first for how to specify order and conditions @return Enumerable
# File lib/orm_adapter/fmrest/adapter.rb, line 79 def find_all(options = {}) base_relation(options) end
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]]
# File lib/orm_adapter/fmrest/adapter.rb, line 72 def find_first(options = {}) base_relation(options).first end
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
# File lib/orm_adapter/fmrest/adapter.rb, line 49 def get(id) klass.find wrap_key(id) rescue FmRest::APIError::RecordMissingError nil end
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
# File lib/orm_adapter/fmrest/adapter.rb, line 40 def get!(id) klass.find wrap_key(id) end
Private Instance Methods
FmRest
with converted query and sort parameters @return FmRest::Spyke::Relation
# File lib/orm_adapter/fmrest/adapter.rb, line 97 def base_relation(options) conditions, order, limit, offset = extract_conditions!(options) relation = klass relation = klass.query(exact_conditions(conditions.stringify_keys)) if conditions.present? relation = relation.sort(*order_clause(order)) if order.present? relation = relation.limit(limit) if limit relation = relation.offset(offset) if offset relation end
# File lib/orm_adapter/fmrest/adapter.rb, line 109 def exact_conditions(conditions) conditions.transform_values { |v| v.is_a?(String) && !v.match?(/^[<=>]/) ? "==#{v}" : v } end
request field meta data for layout of resource class
# File lib/orm_adapter/fmrest/adapter.rb, line 130 def layout_meta(layout) FmRest::V1.build_connection(klass.try(:fmrest_config)) .get("layouts/#{layout}") .body.dig('response', 'fieldMetaData') end
given an order argument, returns an array of attributes (symbols) postfixed with __desc if order is reversed
# File lib/orm_adapter/fmrest/adapter.rb, line 118 def normalize_order(order) return if order.blank? if order.is_a? Array order = [order] unless order.first.is_a?(Array) order.map { |key, dir| (dir.to_s.downcase == 'desc' ? "key__desc" : key).to_sym } else [order.to_sym] end end
# File lib/orm_adapter/fmrest/adapter.rb, line 113 def order_clause(order) order.map {|col, dir| (dir == :desc ? "#{col}__desc" : col).to_sym } end