Class: PassiveRecord::Core::Query

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, ArithmeticHelpers
Defined in:
lib/passive_record/core/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ArithmeticHelpers

#average, #mode, #pluck, #sum

Constructor Details

#initialize(klass, conditions = {}, scope = nil) ⇒ Query

Returns a new instance of Query.



10
11
12
13
14
# File 'lib/passive_record/core/query.rb', line 10

def initialize(klass,conditions={},scope=nil)
  @klass = klass
  @conditions = conditions
  @scope = scope
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/passive_record/core/query.rb', line 102

def method_missing(meth,*args,&blk)
  if @klass.methods.include?(meth)
    scope_query = @klass.send(meth,*args,&blk)
    if negated? && @scope.nil? && @conditions.empty?
      @scope = scope_query
      self
    elsif basic? && scope_query.basic?
      @conditions.merge!(scope_query.conditions)
      self
    else
      scope_query.and(self)
    end
  else
    super(meth,*args,&blk)
  end
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



8
9
10
# File 'lib/passive_record/core/query.rb', line 8

def conditions
  @conditions
end

Instance Method Details

#allObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/passive_record/core/query.rb', line 24

def all
  if @scope
    matching = @scope.method(:matching_instances)
    if negated?
      raw_all.reject(&matching)
    else
      raw_all.select(&matching)
    end
  else
    matching = method(:matching_instances)
    raw_all.select(&matching)
  end
end

#and(scope_query) ⇒ Object



98
99
100
# File 'lib/passive_record/core/query.rb', line 98

def and(scope_query)
  ConjoinedQuery.new(@klass, self, scope_query)
end

#basic?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/passive_record/core/query.rb', line 94

def basic?
  !negated? && !disjoined? && !conjoined?
end

#conjoined?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/passive_record/core/query.rb', line 90

def conjoined?
  false
end

#create(attrs = {}) ⇒ Object



69
70
71
# File 'lib/passive_record/core/query.rb', line 69

def create(attrs={})
  @klass.create(@conditions.merge(attrs))
end

#disjoined?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/passive_record/core/query.rb', line 86

def disjoined?
  false
end

#eachObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/passive_record/core/query.rb', line 43

def each
  if @scope
    matching = @scope.method(:matching_instances)
    if negated?
      raw_all.each do |instance|
        yield instance unless matching[instance]
      end
    else
      raw_all.each do |instance|
        yield instance if matching[instance]
      end
    end
  else
    matching = method(:matching_instances)
    @klass.all.each do |instance|
      yield instance if matching[instance]
    end
  end
end

#evaluate_condition(instance, field, value) ⇒ Object (protected)



120
121
122
123
124
125
126
127
128
# File 'lib/passive_record/core/query.rb', line 120

def evaluate_condition(instance, field, value)
  case value
  when Hash  then evaluate_nested_conditions(instance, field, value)
  when Range then value.cover?(instance.send(field))
  when Array then value.include?(instance.send(field))
  else
    instance.send(field) == value
  end
end

#evaluate_nested_conditions(instance, field, value) ⇒ Object (protected)



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/passive_record/core/query.rb', line 130

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?
    elsif val.is_a?(Hash)
      evaluate_nested_conditions(association, association_field, val)
    else
      association.send(association_field) == val
    end
  end
end

#first_or_createObject



73
74
75
# File 'lib/passive_record/core/query.rb', line 73

def first_or_create
  first || create
end

#matching_instances(instance) ⇒ Object



63
64
65
66
67
# File 'lib/passive_record/core/query.rb', line 63

def matching_instances(instance)
  @conditions.all? do |(field,value)|
    evaluate_condition(instance, field, value)
  end
end

#negated?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/passive_record/core/query.rb', line 82

def negated?
  false
end

#not(new_conditions = {}) ⇒ Object



16
17
18
# File 'lib/passive_record/core/query.rb', line 16

def not(new_conditions={})
  NegatedQuery.new(@klass, new_conditions)
end

#or(query = nil) ⇒ Object



20
21
22
# File 'lib/passive_record/core/query.rb', line 20

def or(query=nil)
  DisjoinedQuery.new(@klass, self, query)
end

#raw_allObject



39
40
41
# File 'lib/passive_record/core/query.rb', line 39

def raw_all
  @klass.all
end

#where(new_conditions = {}) ⇒ Object



77
78
79
80
# File 'lib/passive_record/core/query.rb', line 77

def where(new_conditions={})
  @conditions.merge!(new_conditions)
  self
end