Class: ObjectJSONMapper::Relation
- Inherits:
-
Object
- Object
- ObjectJSONMapper::Relation
- Includes:
- Enumerable
- Defined in:
- lib/object_json_mapper/relation.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#collection ⇒ Object
TODO: refactor.
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#path ⇒ Object
writeonly
Sets the attribute path.
Instance Method Summary collapse
- #deep_clone ⇒ Object
- #exists?(id) ⇒ Boolean
- #find(id) ⇒ Object
- #find_by(conditions = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Relation
constructor
A new instance of Relation.
-
#locals ⇒ ActiveRecord::Relation
Find and return relation of local records by ‘id`.
- #none ⇒ Object
- #paginate {|ObjectJSONMapper::Relation| ... } ⇒ Object
- #pluck(*attributes) ⇒ Array+
- #where(conditions = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Relation
Returns a new instance of Relation.
20 21 22 23 24 25 |
# File 'lib/object_json_mapper/relation.rb', line 20 def initialize( = {}) @klass ||= [:klass] @path ||= [:path] @collection ||= .fetch(:collection, []) @conditions ||= .fetch(:conditions, {}) end |
Instance Attribute Details
#collection ⇒ Object
TODO: refactor
52 53 54 |
# File 'lib/object_json_mapper/relation.rb', line 52 def collection @collection end |
#conditions ⇒ Object
Returns the value of attribute conditions.
5 6 7 |
# File 'lib/object_json_mapper/relation.rb', line 5 def conditions @conditions end |
#klass ⇒ Object
Returns the value of attribute klass.
5 6 7 |
# File 'lib/object_json_mapper/relation.rb', line 5 def klass @klass end |
#path=(value) ⇒ Object
Sets the attribute path
5 6 7 |
# File 'lib/object_json_mapper/relation.rb', line 5 def path=(value) @path = value end |
Instance Method Details
#deep_clone ⇒ Object
82 83 84 85 86 87 |
# 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) ⇒ Boolean
37 38 39 |
# File 'lib/object_json_mapper/relation.rb', line 37 def exists?(id) find(id).present? end |
#find(id) ⇒ Object
33 34 35 |
# File 'lib/object_json_mapper/relation.rb', line 33 def find(id) find_by(id: id.to_i) end |
#find_by(conditions = {}) ⇒ Object
27 28 29 30 31 |
# 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 ⇒ ActiveRecord::Relation
Find and return relation of local records by ‘id`
95 96 97 98 |
# File 'lib/object_json_mapper/relation.rb', line 95 def locals return [] if collection.empty? klass.local.where(id: collection.map(&:id)) end |
#none ⇒ Object
89 90 91 |
# File 'lib/object_json_mapper/relation.rb', line 89 def none NullRelation.new(klass: klass, conditions: conditions) end |
#paginate {|ObjectJSONMapper::Relation| ... } ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# 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) ⇒ Array+
46 47 48 49 |
# 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 = {}) ⇒ Object
41 42 43 |
# File 'lib/object_json_mapper/relation.rb', line 41 def where(conditions = {}) deep_clone.tap { |relation| relation.conditions.merge!(conditions) } end |