Class: Related::Node::Query

Inherits:
Object
  • Object
show all
Includes:
QueryMethods
Defined in:
lib/related/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from QueryMethods

#depth, #include_start_node, #incoming, #limit, #nodes, #options, #outgoing, #page, #path_to, #per_page, #relationships, #shortest_path_to

Constructor Details

#initialize(node) ⇒ Query

Returns a new instance of Query.



98
99
100
101
102
103
# File 'lib/related/node.rb', line 98

def initialize(node)
  @node = node
  @result_type = :nodes
  @depth = 4
  @options = {}
end

Instance Attribute Details

#depth=(value) ⇒ Object (writeonly)

Sets the attribute depth

Parameters:

  • value

    the value to set the attribute depth to.



92
93
94
# File 'lib/related/node.rb', line 92

def depth=(value)
  @depth = value
end

#destination=(value) ⇒ Object (writeonly)

Sets the attribute destination

Parameters:

  • value

    the value to set the attribute destination to.



94
95
96
# File 'lib/related/node.rb', line 94

def destination=(value)
  @destination = value
end

#direction=(value) ⇒ Object (writeonly)

Sets the attribute direction

Parameters:

  • value

    the value to set the attribute direction to.



89
90
91
# File 'lib/related/node.rb', line 89

def direction=(value)
  @direction = value
end

#include_start_node=(value) ⇒ Object (writeonly)

Sets the attribute include_start_node

Parameters:

  • value

    the value to set the attribute include_start_node to.



93
94
95
# File 'lib/related/node.rb', line 93

def include_start_node=(value)
  @include_start_node = value
end

#limit=(value) ⇒ Object (writeonly)

Sets the attribute limit

Parameters:

  • value

    the value to set the attribute limit to.



90
91
92
# File 'lib/related/node.rb', line 90

def limit=(value)
  @limit = value
end

#options=(value) ⇒ Object (writeonly)

Sets the attribute options

Parameters:

  • value

    the value to set the attribute options to.



96
97
98
# File 'lib/related/node.rb', line 96

def options=(value)
  @options = value
end

#page=(value) ⇒ Object (writeonly)

Sets the attribute page

Parameters:

  • value

    the value to set the attribute page to.



91
92
93
# File 'lib/related/node.rb', line 91

def page=(value)
  @page = value
end

#relationship_type=(value) ⇒ Object (writeonly)

Sets the attribute relationship_type

Parameters:

  • value

    the value to set the attribute relationship_type to.



88
89
90
# File 'lib/related/node.rb', line 88

def relationship_type=(value)
  @relationship_type = value
end

#resultObject (readonly)

Returns the value of attribute result.



85
86
87
# File 'lib/related/node.rb', line 85

def result
  @result
end

#result_type=(value) ⇒ Object (writeonly)

Sets the attribute result_type

Parameters:

  • value

    the value to set the attribute result_type to.



87
88
89
# File 'lib/related/node.rb', line 87

def result_type=(value)
  @result_type = value
end

#search_algorithm=(value) ⇒ Object (writeonly)

Sets the attribute search_algorithm

Parameters:

  • value

    the value to set the attribute search_algorithm to.



95
96
97
# File 'lib/related/node.rb', line 95

def search_algorithm=(value)
  @search_algorithm = value
end

Instance Method Details

#as_json(options = {}) ⇒ Object



210
211
212
# File 'lib/related/node.rb', line 210

def as_json(options = {})
  self.to_a
end

#countObject



122
123
124
125
126
127
# File 'lib/related/node.rb', line 122

def count
  @count = @result_type == :nodes ?
    Related.redis.scard(key) :
    Related.redis.zcard(key)
  @limit && @count > @limit ? @limit : @count
end

#diff(query) ⇒ Object



176
177
178
179
180
# File 'lib/related/node.rb', line 176

def diff(query)
  @result_type = :nodes
  @result = Related.redis.sdiff(key, query.key)
  self
end

#diff_with_distributed_fallback(query) ⇒ Object



182
183
184
185
186
187
188
189
# File 'lib/related/node.rb', line 182

def diff_with_distributed_fallback(query)
  diff_without_distributed_fallback(query)
rescue Redis::Distributed::CannotDistribute
  s1 = Related.redis.smembers(key)
  s2 = Related.redis.smembers(query.key)
  @result = s1 - s2
  self
end

#each(&block) ⇒ Object



105
106
107
# File 'lib/related/node.rb', line 105

def each(&block)
  self.to_a.each(&block)
end

#find(node) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/related/node.rb', line 147

def find(node)
  if @result_type == :nodes
    if Related.redis.sismember(key, node.id)
      node_class.find(node.id, @options)
    end
  else
    if id = Related.redis.get(dir_key(node))
      Related::Relationship.find(id, @options)
    end
  end
end

#include?(entity) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/related/node.rb', line 133

def include?(entity)
  if @destination
    self.to_a.include?(entity)
  else
    if entity.is_a?(node_class)
      @result_type = :nodes
      Related.redis.sismember(key, entity.id)
    elsif entity.is_a?(Related::Relationship)
      @result_type = :relationships
      Related.redis.sismember(key, entity.id)
    end
  end
end

#intersect(query) ⇒ Object



193
194
195
196
197
# File 'lib/related/node.rb', line 193

def intersect(query)
  @result_type = :nodes
  @result = Related.redis.sinter(key, query.key)
  self
end

#intersect_with_distributed_fallback(query) ⇒ Object



199
200
201
202
203
204
205
206
# File 'lib/related/node.rb', line 199

def intersect_with_distributed_fallback(query)
  intersect_without_distributed_fallback(query)
rescue Redis::Distributed::CannotDistribute
  s1 = Related.redis.smembers(key)
  s2 = Related.redis.smembers(query.key)
  @result = s1 & s2
  self
end

#map(&block) ⇒ Object



109
110
111
# File 'lib/related/node.rb', line 109

def map(&block)
  self.to_a.map(&block)
end

#sizeObject



129
130
131
# File 'lib/related/node.rb', line 129

def size
  @count || count
end

#to_aObject



113
114
115
116
117
118
119
120
# File 'lib/related/node.rb', line 113

def to_a
  perform_query unless @result
  if @result_type == :nodes
    node_class.find(@result, @options)
  else
    Related::Relationship.find(@result, @options)
  end
end

#to_json(options = {}) ⇒ Object



214
215
216
# File 'lib/related/node.rb', line 214

def to_json(options = {})
  self.as_json.to_json(options)
end

#union(query) ⇒ Object



159
160
161
162
163
# File 'lib/related/node.rb', line 159

def union(query)
  @result_type = :nodes
  @result = Related.redis.sunion(key, query.key)
  self
end

#union_with_distributed_fallback(query) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/related/node.rb', line 165

def union_with_distributed_fallback(query)
  union_without_distributed_fallback(query)
rescue Redis::Distributed::CannotDistribute
  s1 = Related.redis.smembers(key)
  s2 = Related.redis.smembers(query.key)
  @result = s1 | s2
  self
end