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

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 56

def hydrate_record(record, node_alias)
  raise NotImplementedError, "#{self.class} must implement #hydrate_record"
end

#inspectString

Override inspect to hide sensitive information

Returns:

  • (String)

    Safe representation of the adapter



67
68
69
70
71
72
# File 'lib/active_cypher/connection_adapters/abstract_adapter.rb', line 67

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



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

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