Class: Opium::Model::Criteria
Instance Attribute Summary collapse
Instance Method Summary
collapse
#all, #between, #cache, #cached?, #dont_select, #exists, #gt, #gte, #in, #keys, #limit, #lt, #lte, #ne, #nin, #or, #order, #pluck, #select, #skip, #where
Constructor Details
#initialize(model_name) ⇒ Criteria
Returns a new instance of Criteria.
10
11
12
13
|
# File 'lib/opium/model/criteria.rb', line 10
def initialize( model_name )
@model_name = model_name.respond_to?(:name) ? model_name.name : model_name
constraints[:count] = 1
end
|
Instance Attribute Details
#model_name ⇒ Object
Returns the value of attribute model_name.
15
16
17
|
# File 'lib/opium/model/criteria.rb', line 15
def model_name
@model_name
end
|
Instance Method Details
#==(other) ⇒ Object
69
70
71
|
# File 'lib/opium/model/criteria.rb', line 69
def ==( other )
other.is_a?( self.class ) && self.model_name == other.model_name && self.constraints == other.constraints && self.variables == other.variables
end
|
25
26
27
|
# File 'lib/opium/model/criteria.rb', line 25
def chain
Marshal.load( Marshal.dump( self ) ).tap {|m| m.instance_variable_set( :@cache, nil )}
end
|
#constraints ⇒ Object
29
30
31
|
# File 'lib/opium/model/criteria.rb', line 29
def constraints
@constraints ||= {}.with_indifferent_access
end
|
#constraints? ⇒ Boolean
41
42
43
|
# File 'lib/opium/model/criteria.rb', line 41
def constraints?
!constraints.except(:count).empty?
end
|
65
66
67
|
# File 'lib/opium/model/criteria.rb', line 65
def criteria
self
end
|
#each(&block) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/opium/model/criteria.rb', line 73
def each(&block)
if !block_given?
to_enum(:each)
elsif cached? && @cache
@cache.each(&block)
else
response = self.model.http_get( query: self.constraints )
@cache = []
if response && response['results']
variables[:total_count] = response['count']
response['results'].each do |attributes|
instance = self.model.new( attributes )
@cache << instance if cached?
block.call instance
end
end
end
end
|
61
62
63
|
# File 'lib/opium/model/criteria.rb', line 61
def empty?
count == 0
end
|
92
93
94
95
96
|
# File 'lib/opium/model/criteria.rb', line 92
def inspect
inspected_constraints = constraints.map {|k, v| [k, v.inspect].join(': ')}.join(', ')
inspected_constraints.prepend ' ' if inspected_constraints.size > 0
"#<#{ self.class.name }<#{ model_name }>#{ inspected_constraints }>"
end
|
21
22
23
|
# File 'lib/opium/model/criteria.rb', line 21
def model
models[model_name] ||= model_name.constantize
end
|
98
99
100
101
102
103
|
# File 'lib/opium/model/criteria.rb', line 98
def to_parse
{}.with_indifferent_access.tap do |result|
result[:query] = { where: constraints[:where], className: model_name } if constraints[:where]
result[:key] = constraints[:keys] if constraints[:keys]
end
end
|
#to_partial_path ⇒ Object
17
18
19
|
# File 'lib/opium/model/criteria.rb', line 17
def to_partial_path
model._to_partial_path
end
|
#total_count ⇒ Object
111
112
113
|
# File 'lib/opium/model/criteria.rb', line 111
def total_count
count && variables[:total_count]
end
|
105
106
107
108
109
|
# File 'lib/opium/model/criteria.rb', line 105
def uncache
super.tap do |criteria|
criteria.instance_variable_set(:@cache, nil)
end
end
|
#update_constraint(constraint, value) ⇒ Object
33
34
35
|
# File 'lib/opium/model/criteria.rb', line 33
def update_constraint( constraint, value )
chain.tap {|c| c.update_constraint!( constraint, value )}
end
|
#update_constraint!(constraint, value) ⇒ Object
37
38
39
|
# File 'lib/opium/model/criteria.rb', line 37
def update_constraint!( constraint, value )
update_hash_value :constraints, constraint, value
end
|
#update_variable(variable, value) ⇒ Object
49
50
51
|
# File 'lib/opium/model/criteria.rb', line 49
def update_variable( variable, value )
chain.tap {|c| c.update_variable!( variable, value )}
end
|
#update_variable!(variable, value) ⇒ Object
53
54
55
|
# File 'lib/opium/model/criteria.rb', line 53
def update_variable!( variable, value )
update_hash_value :variables, variable, value
end
|
#variables ⇒ Object
45
46
47
|
# File 'lib/opium/model/criteria.rb', line 45
def variables
@variables ||= {}.with_indifferent_access
end
|
57
58
59
|
# File 'lib/opium/model/criteria.rb', line 57
def variables?
!variables.empty?
end
|