Class: ActiveCypher::ConnectionAdapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_cypher/connection_adapters/abstract_adapter.rb

Overview

Note:

Because every project needs an abstract class to remind you that nothing is ever truly implemented.

Minimal contract every graph adapter must fulfil.

Direct Known Subclasses

AbstractBoltAdapter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AbstractAdapter

Initializes the adapter, because you can’t spell “configuration” without “con.”

Parameters:

  • config (Hash)

    The configuration hash for the adapter



16
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 16

def initialize(config) = (@config = config)

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 12

def config
  @config
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


22
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 22

def active?                 = false

#begin_transactionObject

—- transactions (optional) —————————————— Transaction methods: for when you want to pretend you have ACID.



34
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 34

def begin_transaction       = nil

#commit_transaction(_) ⇒ Object



35
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 35

def commit_transaction(_)   = true

#connectObject

—- lifecycle ——————————————————— The lifecycle methods. Spoiler: most of them do nothing.



20
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 20

def connect                 = raise(AdapterNotFoundError)

#disconnectObject



21
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 21

def disconnect              = true

#execute_cypherObject

—- Cypher ———————————————————— Executes a Cypher query, or at least raises an error about it.

Raises:

  • (NotImplementedError)

    Always, unless implemented by subclass.



28
29
30
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 28

def execute_cypher(*)
  raise NotImplementedError, "#{self.class} must implement #execute_cypher"
end

#hydrate_record(record, node_alias) ⇒ Hash

Hydrates attributes from a database record

Parameters:

  • record (Hash)

    The raw record from the database

  • node_alias (Symbol)

    The alias used for the node in the query

Returns:

  • (Hash)

    The hydrated attributes



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 56

def hydrate_record(record, node_alias)
  attrs = {}
  node_data = record[node_alias] || record[node_alias.to_s]

  if node_data.is_a?(Array) && node_data.length >= 2
    properties_container = node_data[1]
    if properties_container.is_a?(Array) && properties_container.length >= 3
      properties = properties_container[2]
      properties.each { |k, v| attrs[k.to_sym] = v } if properties.is_a?(Hash)
    end
  elsif node_data.is_a?(Hash)
    node_data.each { |k, v| attrs[k.to_sym] = v }
  elsif node_data.respond_to?(:properties)
    attrs = node_data.properties.symbolize_keys
  end

  attrs[:internal_id] = record[:internal_id] || record['internal_id']
  attrs
end

#inspectString

Override inspect to hide sensitive information

Returns:

  • (String)

    Safe representation of the adapter



83
84
85
86
87
88
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 83

def inspect
  filtered_config = ActiveCypher::Redaction.filter_hash(config)

  # Return a safe representation
  "#<#{self.class}:0x#{object_id.to_s(16)} @config=#{filtered_config.inspect}>"
end

#prepare_params(raw) ⇒ Object

—- helpers ———————————————————– Prepares parameters for Cypher, because the database can’t read your mind. Yet.

Parameters:

  • raw (Object)

    The raw parameter value

Returns:

  • (Object)

    The prepared parameter



42
43
44
45
46
47
48
49
50
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 42

def prepare_params(raw)
  case raw
  when Hash  then raw.transform_keys(&:to_s).transform_values { |v| prepare_params(v) }
  when Array then raw.each_with_index.to_h { |v, i| ["p#{i + 1}", prepare_params(v)] }
  when Time, Date, DateTime then raw.iso8601
  when Symbol then raw.to_s
  else raw # String/Integer/Float/Boolean/NilClass
  end
end

#process_records(rows) ⇒ Array<Hash>

Turns rows into symbols, because Rubyists fear strings.

Parameters:

  • rows (Array<Hash>)

    The rows to process

Returns:

  • (Array<Hash>)

    The processed rows



79
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 79

def process_records(rows) = rows.map { |r| deep_symbolize(r) }

#reconnectObject



23
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 23

def reconnect               = disconnect && connect

#rollback_transaction(_) ⇒ Object



36
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 36

def rollback_transaction(_) = true