Class: Yoda::Store::Adapters::GdbmAdapter::NamespaceAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb

Constant Summary collapse

KEYS_ADDRESS =
"%keys"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database:, namespace:) ⇒ NamespaceAccessor

Returns a new instance of NamespaceAccessor.

Parameters:

  • database (GDBM)
  • namespace (String, Symbol, nil)


16
17
18
19
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 16

def initialize(database:, namespace:)
  @database = database
  @namespace = namespace&.to_s
end

Instance Attribute Details

#databaseGDBM (readonly)

Returns:

  • (GDBM)


9
10
11
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 9

def database
  @database
end

#namespaceString? (readonly)

Returns:

  • (String, nil)


12
13
14
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 12

def namespace
  @namespace
end

Instance Method Details

#batch_write(data, bar) ⇒ Object

This method is abstract.

Parameters:

  • data (Enumerator<(String, Object)>)
  • bar (#increment, nil)


103
104
105
106
107
108
109
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 103

def batch_write(data, bar)
  data.each do |(k, v)|
    put(k, v, modify_keys: false)
    bar&.increment
  end
  add_addresses(data.map(&:first).map(&:to_s).compact)
end

#clearvoid

This method is abstract.

This method returns an undefined value.

Clear all contents in the database.



73
74
75
76
77
78
79
80
81
82
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 73

def clear
  if namespace
    keys.each do |key|
      delete(key)
    end
    delete(KEYS_ADDRESS)
  else
    database.clear
  end
end

#delete(address) ⇒ void

This method returns an undefined value.

Parameters:

  • address (String, Symbol)


37
38
39
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 37

def delete(address)
  database.delete(build_key(address))
end

#empty?Boolean

This method is abstract.

Returns:

  • (Boolean)


86
87
88
89
90
91
92
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 86

def empty?
  if namespace
    keys.empty?
  else
    database.empty?
  end
end

#exist?(address) ⇒ Boolean

This method is abstract.

Parameters:

  • address (String, Symbol)

Returns:

  • (Boolean)


44
45
46
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 44

def exist?(address)
  database.has_key?(build_key(address))
end

#get(address) ⇒ Object?

Parameters:

  • address (String, Symbol)

Returns:

  • (Object, nil)


23
24
25
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 23

def get(address, **)
  JSON.load(database[build_key(address)], symbolize_names: true)
end

#inspectString

Returns:

  • (String)


112
113
114
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 112

def inspect
  "#<#{self.class.name}: #{self.class.type}>"
end

#keysInteger

This method is abstract.

Returns:

  • (Integer)


50
51
52
53
54
55
56
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 50

def keys
  if namespace
    get(KEYS_ADDRESS) || []
  else
    database.keys
  end
end

#persistable?Boolean

This method is abstract.

Returns:

  • (Boolean)


96
97
98
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 96

def persistable?
  true
end

#put(address, object, modify_keys: true) ⇒ void

This method returns an undefined value.

Parameters:

  • address (String, Symbol)
  • object (#to_json)


30
31
32
33
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 30

def put(address, object, modify_keys: true)
  database[build_key(address)] = object.to_json
  add_addresses([address]) if modify_keys
end

#statsObject

This method is abstract.

Returns:

  • (Object)


60
61
62
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 60

def stats
  {}
end

#syncvoid

This method is abstract.

This method returns an undefined value.



66
67
68
# File 'lib/yoda/store/adapters/gdbm_adapter/namespace_accessor.rb', line 66

def sync
  database.sync
end