Class: Related::Node::Query

Inherits:
Object
  • Object
show all
Includes:
DistributedFallback, 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.



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

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.



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

def depth=(value)
  @depth = value
end

#destination=(value) ⇒ Object (writeonly)

Sets the attribute destination

Parameters:

  • value

    the value to set the attribute destination to.



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

def destination=(value)
  @destination = value
end

#direction=(value) ⇒ Object (writeonly)

Sets the attribute direction

Parameters:

  • value

    the value to set the attribute direction to.



119
120
121
# File 'lib/related/node.rb', line 119

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.



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

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.



120
121
122
# File 'lib/related/node.rb', line 120

def limit=(value)
  @limit = value
end

#options=(value) ⇒ Object (writeonly)

Sets the attribute options

Parameters:

  • value

    the value to set the attribute options to.



126
127
128
# File 'lib/related/node.rb', line 126

def options=(value)
  @options = value
end

#page=(value) ⇒ Object (writeonly)

Sets the attribute page

Parameters:

  • value

    the value to set the attribute page to.



121
122
123
# File 'lib/related/node.rb', line 121

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.



118
119
120
# File 'lib/related/node.rb', line 118

def relationship_type=(value)
  @relationship_type = value
end

#resultObject (readonly)

Returns the value of attribute result.



115
116
117
# File 'lib/related/node.rb', line 115

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.



117
118
119
# File 'lib/related/node.rb', line 117

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.



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

def search_algorithm=(value)
  @search_algorithm = value
end

Instance Method Details

#as_json(options = {}) ⇒ Object



207
208
209
# File 'lib/related/node.rb', line 207

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

#countObject



152
153
154
155
156
157
# File 'lib/related/node.rb', line 152

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

#diff(query) ⇒ Object



195
196
197
198
199
# File 'lib/related/node.rb', line 195

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

#each(&block) ⇒ Object



135
136
137
# File 'lib/related/node.rb', line 135

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

#find(node) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/related/node.rb', line 177

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)


163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/related/node.rb', line 163

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



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

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

#map(&block) ⇒ Object



139
140
141
# File 'lib/related/node.rb', line 139

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

#sizeObject



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

def size
  @count || count
end

#to_aObject



143
144
145
146
147
148
149
150
# File 'lib/related/node.rb', line 143

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



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

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

#union(query) ⇒ Object



189
190
191
192
193
# File 'lib/related/node.rb', line 189

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