Module: Architect4r::Model::Queries::ClassMethods

Defined in:
lib/architect4r/model/queries.rb

Instance Method Summary collapse

Instance Method Details

#allObject



39
40
41
# File 'lib/architect4r/model/queries.rb', line 39

def all
  find_by_cypher("start ModelRoot=node(#{self.model_root.id}) match ModelRoot<-[:model_type]-Item return Item", 'Item')
end

#count(opts = {}, &block) ⇒ Object



12
13
14
15
# File 'lib/architect4r/model/queries.rb', line 12

def count(opts = {}, &block)
  data = connection.execute_cypher("start s=node(#{self.model_root.id}) match (s)<-[:model_type]-(d) return count(d)")
  data['data'].flatten.first
end

#find_by_cypher(query, identifier) ⇒ Object

Use this method only to fetch items of the same class.



30
31
32
33
34
35
36
37
# File 'lib/architect4r/model/queries.rb', line 30

def find_by_cypher(query, identifier)
  if result_data = connection.execute_cypher(query)
    result_data = connection.transform_cypher_result_to_hash(result_data)
    result_data.map { |item| connection.convert_if_possible(item[identifier]) }
  else
    nil
  end
end

#find_by_id(id) ⇒ Object

Fetch a record of the specified model based on its id



19
20
21
22
23
# File 'lib/architect4r/model/queries.rb', line 19

def find_by_id(id)
  data = connection.execute_cypher("start s=node(#{self.model_root.id}), d=node(#{id.to_i}) match s<-[r:model_type]-d return d")
  data &&= data['data'] && data['data'].flatten.first
  self.build_from_database(data)
end

#find_by_id!(id) ⇒ Object



25
26
27
# File 'lib/architect4r/model/queries.rb', line 25

def find_by_id!(id)
  self.find_by_id(id) || raise(Architect4r::RecordNotFound.new("Could not find the #{self.name} with id #{id}!"))
end