class ObjectJSONMapper::Relation
Attributes
collection[RW]
conditions[RW]
klass[RW]
path[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/object_json_mapper/relation.rb, line 20 def initialize(options = {}) @klass ||= options[:klass] @path ||= options[:path] @collection ||= options.fetch(:collection, []) @conditions ||= options.fetch(:conditions, {}) end
Public Instance Methods
deep_clone()
click to toggle source
# File lib/object_json_mapper/relation.rb, line 82 def deep_clone clone.tap do |object| object.conditions = conditions.clone object.collection = @collection.clone end end
exists?(id)
click to toggle source
# File lib/object_json_mapper/relation.rb, line 37 def exists?(id) find(id).present? end
find(id)
click to toggle source
# File lib/object_json_mapper/relation.rb, line 33 def find(id) find_by(id: id.to_i) end
find_by(conditions = {})
click to toggle source
# File lib/object_json_mapper/relation.rb, line 27 def find_by(conditions = {}) collection.find do |record| conditions.all? { |k, v| record.public_send(k) == v } end end
locals()
click to toggle source
Find and return relation of local records by `id` @return [ActiveRecord::Relation]
# File lib/object_json_mapper/relation.rb, line 95 def locals return [] if collection.empty? klass.local.where(id: collection.map(&:id)) end
none()
click to toggle source
# File lib/object_json_mapper/relation.rb, line 89 def none NullRelation.new(klass: klass, conditions: conditions) end
paginate() { |chunk| ... }
click to toggle source
@yield [ObjectJSONMapper::Relation] with next page until last page reached.
# File lib/object_json_mapper/relation.rb, line 66 def paginate i = 1 loop do chunk = page(i) break if chunk.out_of_range? yield chunk if block_given? break if chunk.last_page? i += 1 end end
pluck(*attributes)
click to toggle source
@return [Array,Array<Array>]
# File lib/object_json_mapper/relation.rb, line 46 def pluck(*attributes) map { |record| record.slice(*attributes).values } .tap { |result| result.flatten! if attributes.size == 1 } end
where(conditions = {})
click to toggle source
# File lib/object_json_mapper/relation.rb, line 41 def where(conditions = {}) deep_clone.tap { |relation| relation.conditions.merge!(conditions) } end
Private Instance Methods
prepare_params(conditions)
click to toggle source
# File lib/object_json_mapper/relation.rb, line 102 def prepare_params(conditions) conditions.deep_merge(conditions) do |_, _, value| case value when Array value.join(',') else value end end end