Module: ApacheAge::Entities::ClassMethods

Defined in:
lib/apache_age/entities/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#age_graphObject

Private stuff



28
# File 'lib/apache_age/entities/class_methods.rb', line 28

def age_graph = 'age_schema'

#age_labelObject



29
# File 'lib/apache_age/entities/class_methods.rb', line 29

def age_label = name.gsub('::', '__')

#age_typeObject



30
# File 'lib/apache_age/entities/class_methods.rb', line 30

def age_type = name.constantize.new.age_type

#allObject



16
# File 'lib/apache_age/entities/class_methods.rb', line 16

def all = QueryBuilder.new(self).all

#create(attributes) ⇒ Object

for now we only allow one predertimed graph



5
6
7
8
9
# File 'lib/apache_age/entities/class_methods.rb', line 5

def create(attributes)
  instance = new(**attributes)
  instance.save
  instance
end

#execute_find(cypher_sql) ⇒ Object



47
# File 'lib/apache_age/entities/class_methods.rb', line 47

def execute_find(cypher_sql) = execute_where(cypher_sql).first

#execute_sql(cypher_sql) ⇒ Object



45
# File 'lib/apache_age/entities/class_methods.rb', line 45

def execute_sql(cypher_sql) = ActiveRecord::Base.connection.execute(cypher_sql)

#execute_where(cypher_sql) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/apache_age/entities/class_methods.rb', line 49

def execute_where(cypher_sql)
  age_results = ActiveRecord::Base.connection.execute(cypher_sql)
  return [] if age_results.values.count.zero?

  age_results.values.map do |value|
    json_data = value.first.split('::').first
    hash = JSON.parse(json_data)
    # once we have the record we use the label to find the class
    klass = hash['label'].gsub('__', '::').constantize
    attribs = hash.except('label', 'properties').merge(hash['properties']).symbolize_keys

    # knowing the class and attributes we can create a new instance (wether all results are of the same class or not)
    # This allows us to return results for, ApacheAge::Node, ApacheAge::Edge, or any specific type of node or edge
    klass.new(**attribs)
  end
end

#find(id) ⇒ Object



18
# File 'lib/apache_age/entities/class_methods.rb', line 18

def find(id) = where(id: id).first

#find_by(attributes) ⇒ Object



20
21
22
23
24
# File 'lib/apache_age/entities/class_methods.rb', line 20

def find_by(attributes)
  return nil if attributes.reject { |_k, v| v.blank? }.empty?

  where(attributes).limit(1).first
end

#firstObject



17
# File 'lib/apache_age/entities/class_methods.rb', line 17

def first = QueryBuilder.new(self).limit(1).first

#match_clauseObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/apache_age/entities/class_methods.rb', line 32

def match_clause
  case age_type
  when 'vertex'
    # this allows us to Query for all nodes or a specific class of nodes
    self == ApacheAge::Node ? '(find)' : "(find:#{age_label})"
  when 'edge'
    # this allows us to Query for all edges or a specific class of edges
    self == ApacheAge::Edge ? '(start_node)-[find]->(end_node)' : "(start_node)-[find:#{age_label}]->(end_node)"
  when 'path'
    "(start_node)-[edge:#{@path_edge.gsub('::', '__')}*#{@path_length} #{path_properties}]->(end_node)"
  end
end

#where(*attributes) ⇒ Object



11
12
13
14
# File 'lib/apache_age/entities/class_methods.rb', line 11

def where(*attributes)
  query_builder = QueryBuilder.new(self)
  query_builder.where(*attributes)
end