Class: Datapathy::Query
- Inherits:
-
Object
show all
- Defined in:
- lib/datapathy/query.rb
Defined Under Namespace
Classes: Condition, ConditionSet
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(model, conditions = {}, &blk) ⇒ Query
Returns a new instance of Query.
9
10
11
12
13
14
15
|
# File 'lib/datapathy/query.rb', line 9
def initialize(model, conditions = {}, &blk)
@model = model
@conditions = ConditionSet.new
@blocks = []
@instrumenter = ActiveSupport::Notifications.instrumenter
add(conditions, &blk)
end
|
Instance Attribute Details
#conditions ⇒ Object
Returns the value of attribute conditions.
6
7
8
|
# File 'lib/datapathy/query.rb', line 6
def conditions
@conditions
end
|
#count ⇒ Object
Returns the value of attribute count.
6
7
8
|
# File 'lib/datapathy/query.rb', line 6
def count
@count
end
|
#instrumenter ⇒ Object
Returns the value of attribute instrumenter.
6
7
8
|
# File 'lib/datapathy/query.rb', line 6
def instrumenter
@instrumenter
end
|
#model ⇒ Object
Returns the value of attribute model.
6
7
8
|
# File 'lib/datapathy/query.rb', line 6
def model
@model
end
|
#offset ⇒ Object
Returns the value of attribute offset.
6
7
8
|
# File 'lib/datapathy/query.rb', line 6
def offset
@offset
end
|
Instance Method Details
#add(conditions = {}, &blk) ⇒ Object
17
18
19
20
|
# File 'lib/datapathy/query.rb', line 17
def add(conditions = {}, &blk)
add_conditions_hash(conditions)
add_conditions(&blk) if block_given?
end
|
#add_conditions {|@conditions| ... } ⇒ Object
22
23
24
25
|
# File 'lib/datapathy/query.rb', line 22
def add_conditions(&blk)
@blocks << blk
yield @conditions
end
|
#add_conditions_hash(conditions = {}) ⇒ Object
27
28
29
30
31
|
# File 'lib/datapathy/query.rb', line 27
def add_conditions_hash(conditions = {})
conditions.each do |k,v|
add_conditions { |q| q.send(k) == v }
end
end
|
#filter(resources) ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/datapathy/query.rb', line 56
def filter(resources)
resources = match_resources(resources)
resources = order_resources(resources)
resources = limit_resources(resources)
resources
end
|
#initialize_and_filter(records) ⇒ Object
43
44
45
|
# File 'lib/datapathy/query.rb', line 43
def initialize_and_filter(records)
filter(initialize_resources(records))
end
|
#initialize_resources(records) ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/datapathy/query.rb', line 47
def initialize_resources(records)
return records if records.first.is_a?(Datapathy::Model)
records.map { |record|
resource = model.new(record)
resource.new_record = false
resource
}
end
|
#key ⇒ Object
39
40
41
|
# File 'lib/datapathy/query.rb', line 39
def key
@conditions.first.then.arguments.first
end
|
#key_lookup? ⇒ Boolean
33
34
35
36
37
|
# File 'lib/datapathy/query.rb', line 33
def key_lookup?
@conditions.size == 1 &&
(@conditions.first.operation == :key || @conditions.first.operation == model.key) &&
@conditions.first.then.operation == :==
end
|
#limit(count, offset = 0) ⇒ Object
81
82
83
|
# File 'lib/datapathy/query.rb', line 81
def limit(count, offset = 0)
@count, @offset = count, offset
end
|
#limit_resources(resources) ⇒ Object
76
77
78
79
|
# File 'lib/datapathy/query.rb', line 76
def limit_resources(resources)
return resources unless @offset || @count
resources.slice(@offset || 0, @count)
end
|
#match_resources(resources) ⇒ Object
64
65
66
67
68
69
70
|
# File 'lib/datapathy/query.rb', line 64
def match_resources(resources)
resources.select do |record|
@blocks.all? do |block|
block.call(record)
end
end
end
|
#order_resources(resources) ⇒ Object
72
73
74
|
# File 'lib/datapathy/query.rb', line 72
def order_resources(resources)
resources
end
|
#to_s ⇒ Object
85
86
87
88
89
90
91
|
# File 'lib/datapathy/query.rb', line 85
def to_s
string = ""
string << @conditions.inspect
string << " limit #{@limit}" if @limit
string << " offset #{@offset}" if @offset && @offset > 0
string
end
|