Module: ApacheAge::Entities::CommonMethods

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

Instance Method Summary collapse

Instance Method Details

#age_aliasObject



135
136
137
138
139
140
# File 'lib/apache_age/entities/common_methods.rb', line 135

def age_alias
  return nil if id.blank?

  # we start the alias with a since we can't start with a number
  'a' + Digest::SHA256.hexdigest(id.to_s).to_i(16).to_s(36)[0..9]
end

#age_graphObject

for now we just can just use one schema



5
# File 'lib/apache_age/entities/common_methods.rb', line 5

def age_graph = 'age_schema'

#age_hashObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/apache_age/entities/common_methods.rb', line 115

def age_hash
  hash =
    {
      id:,
      label: age_label,
      properties: age_properties
    }
  hash.merge!(end_id:, start_id:) if age_type == 'edge'
  hash.transform_keys(&:to_s)
end

#age_labelObject



6
# File 'lib/apache_age/entities/common_methods.rb', line 6

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

#age_propertiesObject

private



109
110
111
112
113
# File 'lib/apache_age/entities/common_methods.rb', line 109

def age_properties
  attrs = attributes.except('id')
  attrs = attrs.except('end_node', 'start_node', 'end_id', 'start_id') if age_type == 'edge'
  attrs.symbolize_keys
end

#destroyObject Also known as: destroy!, delete



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/apache_age/entities/common_methods.rb', line 83

def destroy
  match_clause = (age_type == 'vertex' ? "(done:#{age_label})" : "()-[done:#{age_label}]->()")
  delete_clause = (age_type == 'vertex' ? 'DETACH DELETE done' : 'DELETE done')
  sanitized_id = ActiveRecord::Base.sanitize_sql(["id(done) = ?", id])
  cypher_sql =
    "    SELECT *\n    FROM cypher('\#{age_graph}', $$\n        MATCH \#{match_clause}\n        WHERE \#{sanitized_id}\n        \#{delete_clause}\n        return done\n    $$) as (deleted agtype);\n    SQL\n\n  hash = execute_sql(cypher_sql)\n  return nil if hash.blank?\n\n  self.id = nil\n  self\nend\n"

#displayObject

default display value



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

def display
  info = age_properties&.values&.first
  info.blank? ? "#{age_label} (#{id})" : "#{info} (#{age_label})"
end

#execute_sql(cypher_sql) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/apache_age/entities/common_methods.rb', line 142

def execute_sql(cypher_sql)
  age_result = ActiveRecord::Base.connection.execute(cypher_sql)
  age_type = age_result.values.first.first.split('::').last
  json_data = age_result.values.first.first.split('::').first
  # json_data = age_result.to_a.first.values.first.split("::#{age_type}").first

  JSON.parse(json_data)
end

#persisted?Boolean

Returns:

  • (Boolean)


7
# File 'lib/apache_age/entities/common_methods.rb', line 7

def persisted? = id.present?

#properties_to_sObject



126
127
128
129
130
131
132
133
# File 'lib/apache_age/entities/common_methods.rb', line 126

def properties_to_s
  string_values =
    age_properties.each_with_object([]) do |(key, val), array|
      sanitized_val = ActiveRecord::Base.sanitize_sql(["?", val])
      array << "#{key}: #{sanitized_val}"
    end
  "{#{string_values.join(', ')}}"
end

#saveObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/apache_age/entities/common_methods.rb', line 64

def save
  return false unless valid?

  cypher_sql = (persisted? ? update_sql : create_sql)
  response_hash = execute_sql(cypher_sql)

  self.id = response_hash['id']

  if age_type == 'edge'
    self.end_id = response_hash['end_id']
    self.start_id = response_hash['start_id']
    # reload the nodes? (can we change the nodes?)
    # self.end_node = ApacheAge::Entity.find(end_id)
    # self.start_node = ApacheAge::Entity.find(start_id)
  end

  self
end

#to_hObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/apache_age/entities/common_methods.rb', line 16

def to_h
  base_h = attributes.to_hash
  if age_type == 'edge'
    # remove the nodes (in attribute form and re-add in hash form)
    base_h = base_h.except('start_node', 'end_node')
    base_h[:end_node] = end_node.to_h if end_node
    base_h[:start_node] = start_node.to_h if start_node
  end
  base_h.symbolize_keys
end

#to_rich_hObject

Enhanced hash representation with display information This provides more context without modifying the core attributes



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/apache_age/entities/common_methods.rb', line 29

def to_rich_h
  # Start with the basic id and add the info string
  result = { _meta: "#{age_label} (#{age_type})", id: id }

  # Group all other attributes under properties
  properties_hash = {}
  attributes.to_hash.except('id').each do |key, value|
    # Skip node objects (we'll handle them specially below)
    next if %w[start_node end_node].include?(key)

    properties_hash[key.to_sym] = value
  end

  result[:properties] = properties_hash

  # Handle nested objects for edges
  if age_type == 'edge'
    result[:start_node] = start_node.to_rich_h if start_node&.respond_to?(:to_rich_h)
    result[:end_node] = end_node.to_rich_h if end_node&.respond_to?(:to_rich_h)
  end

  result
end

#to_sObject



8
# File 'lib/apache_age/entities/common_methods.rb', line 8

def to_s = ":#{age_label} #{properties_to_s}"

#update(attribs) ⇒ Object



59
60
61
62
# File 'lib/apache_age/entities/common_methods.rb', line 59

def update(attribs)
  update_attributes(attribs)
  save
end

#update_attributes(attribs) ⇒ Object



53
54
55
56
57
# File 'lib/apache_age/entities/common_methods.rb', line 53

def update_attributes(attribs)
  attribs.except(id:).each do |key, value|
    send("#{key}=", value) if respond_to?("#{key}=")
  end
end