Class: DataMapper::Query
- Inherits:
-
Object
- Object
- DataMapper::Query
- Includes:
- Assertions
- Defined in:
- lib/dm-core/query.rb
Defined Under Namespace
Classes: Direction, Operator, Path
Constant Summary collapse
- OPTIONS =
[ :reload, :offset, :limit, :order, :add_reversed, :fields, :links, :includes, :conditions, :unique ]
Instance Attribute Summary collapse
-
#add_reversed ⇒ Object
writeonly
Sets the attribute add_reversed.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #bind_values ⇒ Object
-
#inheritance_property_index(repository) ⇒ Object
TODO: spec this.
- #inspect ⇒ Object
-
#key_property_indexes(repository) ⇒ Object
TODO: spec this.
- #merge(other) ⇒ Object
-
#merge_subquery(operator, property, value) ⇒ Object
find the point in self.conditions where the sub select tuple is located.
- #reload? ⇒ Boolean
- #reverse ⇒ Object
- #reverse! ⇒ Object
- #unique? ⇒ Boolean
- #update(other) ⇒ Object
Methods included from Assertions
Instance Attribute Details
#add_reversed=(value) ⇒ Object (writeonly)
Sets the attribute add_reversed
10 11 12 |
# File 'lib/dm-core/query.rb', line 10 def add_reversed=(value) @add_reversed = value end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/dm-core/query.rb', line 67 def ==(other) return true if super return false unless other.kind_of?(self.class) # TODO: add a #hash method, and then use it in the comparison, eg: # return hash == other.hash @model == other.model && @reload == other.reload? && @unique == other.unique? && @offset == other.offset && @limit == other.limit && @order == other.order && # order is significant, so do not sort this @add_reversed == other.add_reversed? && @fields == other.fields && # TODO: sort this so even if the order is different, it is equal @links == other.links && # TODO: sort this so even if the order is different, it is equal @includes == other.includes && # TODO: sort this so even if the order is different, it is equal @conditions.sort_by { |c| c.at(0).hash + c.at(1).hash + c.at(2).hash } == other.conditions.sort_by { |c| c.at(0).hash + c.at(1).hash + c.at(2).hash } end |
#bind_values ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/dm-core/query.rb', line 88 def bind_values bind_values = [] conditions.each do |tuple| next if tuple.size == 2 operator, property, bind_value = *tuple if :raw == operator bind_values.push(*bind_value) else bind_values << bind_value end end bind_values end |
#inheritance_property_index(repository) ⇒ Object
TODO: spec this
103 104 105 |
# File 'lib/dm-core/query.rb', line 103 def inheritance_property_index(repository) fields.index(model.inheritance_property(repository.name)) end |
#inspect ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/dm-core/query.rb', line 134 def inspect attrs = [ [ :repository, repository.name ], [ :model, model ], [ :fields, fields ], [ :links, links ], [ :conditions, conditions ], [ :order, order ], [ :limit, limit ], [ :offset, offset ], [ :reload, reload? ], [ :unique, unique? ], ] "#<#{self.class.name} #{attrs.map { |(k,v)| "@#{k}=#{v.inspect}" } * ' '}>" end |
#key_property_indexes(repository) ⇒ Object
TODO: spec this
108 109 110 111 112 |
# File 'lib/dm-core/query.rb', line 108 def key_property_indexes(repository) if (key_property_indexes = model.key(repository.name).map { |property| fields.index(property) }).all? key_property_indexes end end |
#merge(other) ⇒ Object
63 64 65 |
# File 'lib/dm-core/query.rb', line 63 def merge(other) dup.update(other) end |
#merge_subquery(operator, property, value) ⇒ Object
find the point in self.conditions where the sub select tuple is located. Delete the tuple and add value.conditions. value must be a <DM::Query>
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/dm-core/query.rb', line 118 def merge_subquery(operator, property, value) assert_kind_of 'value', value, self.class new_conditions = [] conditions.each do |tuple| if tuple.at(0).to_s == operator.to_s && tuple.at(1) == property && tuple.at(2) == value value.conditions.each do |subquery_tuple| new_conditions << subquery_tuple end else new_conditions << tuple end end @conditions = new_conditions end |
#reload? ⇒ Boolean
13 14 15 |
# File 'lib/dm-core/query.rb', line 13 def reload? @reload end |
#reverse ⇒ Object
21 22 23 |
# File 'lib/dm-core/query.rb', line 21 def reverse dup.reverse! end |
#reverse! ⇒ Object
25 26 27 28 29 30 |
# File 'lib/dm-core/query.rb', line 25 def reverse! # reverse the sort order update(:order => self.order.map { |o| o.reverse }) self end |
#unique? ⇒ Boolean
17 18 19 |
# File 'lib/dm-core/query.rb', line 17 def unique? @unique end |
#update(other) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dm-core/query.rb', line 32 def update(other) assert_kind_of 'other', other, self.class, Hash assert_valid_other(other) if other.kind_of?(Hash) return self if other.empty? other = self.class.new(@repository, model, other) end return self if self == other # TODO: update this so if "other" had a value explicitly set # overwrite the attributes in self # only overwrite the attributes with non-default values @reload = other.reload? unless other.reload? == false @unique = other.unique? unless other.unique? == false @offset = other.offset if other.reload? || other.offset != 0 @limit = other.limit unless other.limit == nil @order = other.order unless other.order == model.default_order @add_reversed = other.add_reversed? unless other.add_reversed? == false @fields = other.fields unless other.fields == @properties.defaults @links = other.links unless other.links == [] @includes = other.includes unless other.includes == [] update_conditions(other) self end |