Class: RDFMapper::Scope::Query

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/lib/scope/query.rb

Overview

-

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#debug, #fatal, #warn

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, options = {})
  @options = options
  @conditions = []
  @options[:include] ||= []
  @cls = cls
  @modifier = :and

  case @options[:conditions]
    when Hash   then parse_hash
    when Array  then parse_array
  end
  
end

Instance Attribute Details

#clsObject (readonly)

Returns the value of attribute cls.



10
11
12
# File 'lib/lib/scope/query.rb', line 10

def cls
  @cls
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



11
12
13
# File 'lib/lib/scope/query.rb', line 11

def conditions
  @conditions
end

#sqlObject (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

#includeObject

-


59
60
61
# File 'lib/lib/scope/query.rb', line 59

def include
  @options[:include]
end

#include!(name) ⇒ Object

-


66
67
68
# File 'lib/lib/scope/query.rb', line 66

def include!(name)
  @options[:include] << name
end

#inspectString

Developer-friendly representation of the instance

Returns:

  • (String)


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

#limitObject

-


52
53
54
# File 'lib/lib/scope/query.rb', line 52

def limit
  @options[:limit]
end

#matches?(object) ⇒ Boolean

Checks whether specified object passes all conditions of the query.

Parameters:

Returns:

  • (Boolean)


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

#modifierObject

-


31
32
33
# File 'lib/lib/scope/query.rb', line 31

def modifier
  @modifier.to_s.upcase
end

#offsetObject

-


45
46
47
# File 'lib/lib/scope/query.rb', line 45

def offset
  @options[:offset] || 0
end

#orderObject



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

def order
  nil
end

#strict?Boolean

-

Returns:

  • (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>

Parameters:

  • required (Array<Symbol>) (defaults to: [])

    association attributes that should be preloaded

Returns:



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 }).

Parameters:

  • required (Array<Symbol>) (defaults to: [])

    association attributes that should be preloaded

Returns:

  • (Hash)

See Also:



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_statementsObject

-


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_triplesObject

-


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