Class: PassiveRecord::Core::Query
- Inherits:
-
Object
- Object
- PassiveRecord::Core::Query
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/passive_record/core/query.rb
Instance Attribute Summary collapse
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#klass ⇒ Object
Returns the value of attribute klass.
Instance Method Summary collapse
- #==(other_query) ⇒ Object
- #all ⇒ Object
- #create(attrs = {}) ⇒ Object
- #evaluate_nested_conditions(instance, field, value) ⇒ Object protected
- #first_or_create ⇒ Object
-
#initialize(klass, conditions = {}) ⇒ Query
constructor
A new instance of Query.
- #where(new_conditions = {}) ⇒ Object
Constructor Details
#initialize(klass, conditions = {}) ⇒ Query
Returns a new instance of Query.
9 10 11 12 |
# File 'lib/passive_record/core/query.rb', line 9 def initialize(klass,conditions={}) @klass = klass @conditions = conditions end |
Instance Attribute Details
#conditions ⇒ Object
Returns the value of attribute conditions.
7 8 9 |
# File 'lib/passive_record/core/query.rb', line 7 def conditions @conditions end |
#klass ⇒ Object
Returns the value of attribute klass.
7 8 9 |
# File 'lib/passive_record/core/query.rb', line 7 def klass @klass end |
Instance Method Details
#==(other_query) ⇒ Object
41 42 43 |
# File 'lib/passive_record/core/query.rb', line 41 def ==(other_query) @klass == other_query.klass && @conditions == other_query.conditions end |
#all ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/passive_record/core/query.rb', line 14 def all return [] unless conditions klass.select do |instance| conditions.all? do |(field,value)| if value.is_a?(Hash) evaluate_nested_conditions(instance, field, value) else instance.send(field) == value end end end end |
#create(attrs = {}) ⇒ Object
28 29 30 |
# File 'lib/passive_record/core/query.rb', line 28 def create(attrs={}) klass.create(conditions.merge(attrs)) end |
#evaluate_nested_conditions(instance, field, value) ⇒ Object (protected)
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/passive_record/core/query.rb', line 46 def evaluate_nested_conditions(instance, field, value) association = instance.send(field) association && value.all? do |(association_field,val)| if association.is_a?(Associations::Relation) && !association.singular? association.where(association_field => val).any? else association.send(association_field) == val end end end |
#first_or_create ⇒ Object
32 33 34 |
# File 'lib/passive_record/core/query.rb', line 32 def first_or_create first || create end |
#where(new_conditions = {}) ⇒ Object
36 37 38 39 |
# File 'lib/passive_record/core/query.rb', line 36 def where(new_conditions={}) @conditions = new_conditions.merge(conditions) self end |