Class: Datapathy::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/datapathy/collection.rb

Constant Summary collapse

TRIGGER_METHODS =

Since @elements is an array, pretty much every array method should trigger a load. The exceptions are the ones defined above.

(Array.instance_methods - self.instance_methods).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*elements) ⇒ Collection

Collection.new(query) Collection.new(model, …) Collection.new(Model, record, …)



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/datapathy/collection.rb', line 8

def initialize(*elements)
  if elements.first.is_a?(Datapathy::Query)
    query = elements.shift
  elsif elements.first.is_a?(Datapathy::Model)
    query = Datapathy::Query.new(elements.first.model)
  elsif elements.first.ancestors.include?(Datapathy::Model)
    query = Datapathy::Query.new(elements.shift)
  else
    raise "First element must be a query, model, or Model class"
  end

  @query, @model, @adapter = query, query.model, query.model.adapter
  @elements = elements.first.is_a?(Hash) ? Array.wrap(query.model.new(*elements)) : elements
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



3
4
5
# File 'lib/datapathy/collection.rb', line 3

def adapter
  @adapter
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/datapathy/collection.rb', line 3

def model
  @model
end

#queryObject (readonly)

Returns the value of attribute query.



3
4
5
# File 'lib/datapathy/collection.rb', line 3

def query
  @query
end

Instance Method Details

#create(*attributes) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/datapathy/collection.rb', line 55

def create(*attributes)
  query.instrumenter.instrument('query.datapathy', :name => "Create #{model.to_s}", :query => attributes.inspect) do
    if attributes.empty?
      adapter.create(self)
      each { |r| r.new_record = false }
      size == 1 ? first : self
    else
      self.class.new(query, *attributes).create
    end
  end
end

#delete(&blk) ⇒ Object



74
75
76
77
78
79
# File 'lib/datapathy/collection.rb', line 74

def delete(&blk)
  query.add(&blk)
  query.instrumenter.instrument('query.datapathy', :name => "Delete #{model.to_s}", :query => query.to_s) do
    @elements = query.initialize_resources(adapter.delete(self))
  end
end

#detect(*attrs, &blk) ⇒ Object Also known as: find, first



27
28
29
30
31
# File 'lib/datapathy/collection.rb', line 27

def detect(*attrs, &blk)
  slice(0, 1)
  select(*attrs, &blk)
  to_a.first
end

#equality_predicate_sqlObject



116
117
118
# File 'lib/datapathy/collection.rb', line 116

def equality_predicate_sql
  "IN"
end

#inequality_predicate_sqlObject



120
121
122
# File 'lib/datapathy/collection.rb', line 120

def inequality_predicate_sql
  "NOT IN"
end

#load!Object



102
103
104
105
106
# File 'lib/datapathy/collection.rb', line 102

def load!
  query.instrumenter.instrument('query.datapathy', :name => "Read #{model.to_s}", :query => query.to_s) do
    @elements = query.initialize_and_filter(adapter.read(self))
  end
end

#loaded?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/datapathy/collection.rb', line 81

def loaded?
  !@elements.empty?
end

#new(*attributes) ⇒ Object



23
24
25
# File 'lib/datapathy/collection.rb', line 23

def new(*attributes)
  self.model.new(*attributes)
end

#select(*attrs, &blk) ⇒ Object Also known as: find_all



35
36
37
38
# File 'lib/datapathy/collection.rb', line 35

def select(*attrs, &blk)
  query.add(*attrs, &blk)
  self
end

#slice(index_or_start_or_range, length = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/datapathy/collection.rb', line 41

def slice(index_or_start_or_range, length = nil)
  if index_or_start_or_range.is_a?(Range)
    range = index_or_start_or_range
    count, offset = (range.last - range.first), range.first
  elsif length
    start = index_or_start_or_range
    count, offset = length, start
  else
    count, offset = 1, index_or_start_or_range
  end

  query.limit(count, offset)
end

#to_aObject



97
98
99
100
# File 'lib/datapathy/collection.rb', line 97

def to_a
  self.load! unless loaded?
  @elements
end

#to_sql(formatter = nil) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/datapathy/collection.rb', line 108

def to_sql(formatter = nil)
  if any?
    "(" + collect { |e| e.to_sql(formatter) }.join(', ') + ")"
  else
    "(NULL)"
  end
end

#update(attributes = {}, &blk) ⇒ Object



67
68
69
70
71
72
# File 'lib/datapathy/collection.rb', line 67

def update(attributes = {}, &blk)
  query.add(&blk)
  query.instrumenter.instrument('query.datapathy', :name => "Update #{model.to_s}", :query => attributes.inspect) do
    @elements = query.initialize_resources(adapter.update(attributes, self))
  end
end