Class: ObjectJSONMapper::Relation

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/object_json_mapper/relation.rb

Direct Known Subclasses

NullRelation

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @klass      ||= options[:klass]
  @path       ||= options[:path]
  @collection ||= options.fetch(:collection, [])
  @conditions ||= options.fetch(:conditions, {})
end

Instance Attribute Details

#collectionObject

TODO: refactor



52
53
54
# File 'lib/object_json_mapper/relation.rb', line 52

def collection
  @collection
end

#conditionsObject

Returns the value of attribute conditions.



5
6
7
# File 'lib/object_json_mapper/relation.rb', line 5

def conditions
  @conditions
end

#klassObject

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

Parameters:

  • value

    the value to set the attribute path to.



5
6
7
# File 'lib/object_json_mapper/relation.rb', line 5

def path=(value)
  @path = value
end

Instance Method Details

#deep_cloneObject



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

Returns:

  • (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

#localsActiveRecord::Relation

Find and return relation of local records by ‘id`

Returns:

  • (ActiveRecord::Relation)


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

#noneObject



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

Yields:



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+

Returns:

  • (Array, Array<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