module ORMivore::ArAdapter
Attributes
converter[R]
Public Class Methods
included(base)
click to toggle source
# File lib/ormivore/ar_adapter.rb, line 39 def self.included(base) base.extend(ClassMethods) end
new(converter = nil)
click to toggle source
# File lib/ormivore/ar_adapter.rb, line 43 def initialize(converter = nil) @converter = converter || self.class.default_converter_class.new end
Public Instance Methods
create(attrs)
click to toggle source
# File lib/ormivore/ar_adapter.rb, line 57 def create(attrs) record = ar_class.create!( extend_with_defaults( converter.to_storage(attrs))) { |o| o.id = attrs[:id] } attrs.merge(id: record.id) rescue ActiveRecord::ActiveRecordError => e raise StorageError.new(e) end
find(conditions, attributes_to_load, options = {})
click to toggle source
# File lib/ormivore/ar_adapter.rb, line 47 def find(conditions, attributes_to_load, options = {}) order = options.fetch(:order, {}) ar_class.all( select: converter.attributes_list_to_storage(attributes_to_load), conditions: conditions, order: order_by_clause(order) ).map { |r| entity_attributes(r) } end
update(attrs, conditions)
click to toggle source
# File lib/ormivore/ar_adapter.rb, line 66 def update(attrs, conditions) ar_class.update_all(converter.to_storage(attrs), conditions) rescue ActiveRecord::ActiveRecordError => e raise StorageError.new(e) end
Private Instance Methods
ar_class()
click to toggle source
# File lib/ormivore/ar_adapter.rb, line 103 def ar_class self.class.ar_class end
entity_attributes(record)
click to toggle source
# File lib/ormivore/ar_adapter.rb, line 107 def entity_attributes(record) converter.from_storage(record.attributes.symbolize_keys) end
extend_with_defaults(attrs)
click to toggle source
# File lib/ormivore/ar_adapter.rb, line 76 def extend_with_defaults(attrs) expansion = self.class.instance_variable_get(:@expand_on_create) if expansion attrs.merge(expansion.call(attrs)) else attrs end end
order_by_clause(order)
click to toggle source
# File lib/ormivore/ar_adapter.rb, line 85 def order_by_clause(order) return '' if order.empty? order.map { |k, v| direction = case v when :ascending 'asc' when :descending 'desc' else raise BadArgumentError, "Order direction #{v} is invalid" end "#{k} #{direction}" }.join(', ') end