Class: RDFMapper::Scope::Query
- Inherits:
-
Object
- Object
- RDFMapper::Scope::Query
- Includes:
- Logger
- Defined in:
- lib/lib/scope/query.rb
Overview
[-]
Instance Attribute Summary collapse
-
#cls ⇒ Object
readonly
Returns the value of attribute cls.
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#sql ⇒ Object
readonly
Remaining unparsed conditions.
Instance Method Summary collapse
-
#[](name) ⇒ Object
[-].
-
#flatten(required = []) ⇒ Object
[-].
-
#include ⇒ Object
[-].
-
#include!(name) ⇒ Object
[-].
-
#initialize(cls, options = {}) ⇒ Query
constructor
A new instance of Query.
-
#inspect ⇒ String
Developer-friendly representation of the instance.
-
#limit ⇒ Object
[-].
-
#matches?(object) ⇒ Boolean
Checks whether specified object passes all conditions of the query.
-
#modifier ⇒ Object
[-].
-
#offset ⇒ Object
[-].
- #order ⇒ Object
-
#strict? ⇒ Boolean
[-].
-
#to_a(required = []) ⇒ Array<Condition>
(also: #check)
Returns an Array of search conditions.
-
#to_hash(required = []) ⇒ Hash
Follows the same logic as
to_amethod and returns a Hash instead of an Array (:name => { :eq => value, :value => value }). -
#to_statements ⇒ Object
[-].
-
#to_triples ⇒ Object
[-].
Methods included from Logger
Constructor Details
#initialize(cls, options = {}) ⇒ Query
Returns a new instance of Query.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/lib/scope/query.rb', line 14 def initialize(cls, = {}) = @conditions = [] [:include] ||= [] @cls = cls @modifier = :and case [:conditions] when Hash then parse_hash when Array then parse_array end end |
Instance Attribute Details
#cls ⇒ Object (readonly)
Returns the value of attribute cls.
10 11 12 |
# File 'lib/lib/scope/query.rb', line 10 def cls @cls end |
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
11 12 13 |
# File 'lib/lib/scope/query.rb', line 11 def conditions @conditions end |
#sql ⇒ Object (readonly)
Remaining unparsed conditions
12 13 14 |
# File 'lib/lib/scope/query.rb', line 12 def sql @sql end |
Instance Method Details
#[](name) ⇒ Object
[-]
97 98 99 100 101 102 103 |
# File 'lib/lib/scope/query.rb', line 97 def [](name) @conditions.select do |condition| condition.name == name end.map do |condition| condition.value end.first end |
#flatten(required = []) ⇒ Object
[-]
169 170 171 172 173 |
# File 'lib/lib/scope/query.rb', line 169 def flatten(required = []) to_a(required).map do |condition| (condition.class == self.class) ? condition.flatten(required) : condition end.flatten end |
#include ⇒ Object
[-]
59 60 61 |
# File 'lib/lib/scope/query.rb', line 59 def include [:include] end |
#include!(name) ⇒ Object
[-]
66 67 68 |
# File 'lib/lib/scope/query.rb', line 66 def include!(name) [:include] << name end |
#inspect ⇒ String
Developer-friendly representation of the instance
182 183 184 185 186 |
# File 'lib/lib/scope/query.rb', line 182 def inspect #nodoc "#<Query%s>" % to_a.map do |condition| condition.inspect end.inspect end |
#limit ⇒ Object
[-]
52 53 54 |
# File 'lib/lib/scope/query.rb', line 52 def limit [:limit] end |
#matches?(object) ⇒ Boolean
Checks whether specified object passes all conditions of the query.
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/lib/scope/query.rb', line 82 def matches?(object) unless object.kind_of? RDFMapper::Model return false end unless object.class == @cls return false end to_a.reject do |condition| condition.matches?(object) end.empty? end |
#modifier ⇒ Object
[-]
31 32 33 |
# File 'lib/lib/scope/query.rb', line 31 def modifier @modifier.to_s.upcase end |
#offset ⇒ Object
[-]
45 46 47 |
# File 'lib/lib/scope/query.rb', line 45 def offset [:offset] || 0 end |
#order ⇒ Object
73 74 75 |
# File 'lib/lib/scope/query.rb', line 73 def order nil end |
#strict? ⇒ Boolean
[-]
38 39 40 |
# File 'lib/lib/scope/query.rb', line 38 def strict? @modifier == :and end |
#to_a(required = []) ⇒ Array<Condition> Also known as: check
Returns an Array of search conditions. Will preload any associated
models if their properties are undefined (e.g. in case of REST and
SPARQL models are required to have id as RDF::URI, and rails_id
in case of Rails).
Note that any foreign-key associations will be renamed. For instance:
:employee_id => #<RDF::URI(http://example.org/people/1354534)>
is transformed into :employee => #RDFMapper::Model:217132856
134 135 136 137 138 139 140 141 |
# File 'lib/lib/scope/query.rb', line 134 def to_a(required = []) unless required.kind_of? Array required = [required] end @conditions.each do |condition| condition.check(required) end end |
#to_hash(required = []) ⇒ Hash
Follows the same logic as to_a method and returns a Hash instead
of an Array (:name => { :eq => value, :value => value }).
114 115 116 117 118 |
# File 'lib/lib/scope/query.rb', line 114 def to_hash(required = []) Hash[to_a(required).map do |name, eq, value| [name, { :eq => eq, :value => value }] end] end |
#to_statements ⇒ Object
[-]
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/lib/scope/query.rb', line 155 def to_statements target = if self[:id].nil? RDF::Query::Variable.new else RDF::URI.new(self[:id].to_s) end to_a(:id).map do |condition| condition.to_statements(target) end.flatten.compact + @cls.to_statements(:short => true) end |
#to_triples ⇒ Object
[-]
146 147 148 149 150 |
# File 'lib/lib/scope/query.rb', line 146 def to_triples to_statements.map do |statement| [ statement[:subject], statement[:predicate], statement[:object] ] end end |