Class: CMDB::Source::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdb/source/memory.rb

Overview

Data source that is backed by the an in-memory Ruby hash; used solely for testing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, prefix) ⇒ Memory

Construct a new Source::Memory.



12
13
14
15
16
# File 'lib/cmdb/source/memory.rb', line 12

def initialize(hash, prefix)
  uri = URI.parse("memory:#{hash.object_id}")
  super(uri, prefix)
  @hash = hash
end

Instance Attribute Details

#prefixString (readonly)

Returns the empty string.

Returns:

  • (String)

    the empty string



9
10
11
# File 'lib/cmdb/source/memory.rb', line 9

def prefix
  @prefix
end

Instance Method Details

#each_pair {|name, value| ... } ⇒ Object

Enumerate the keys in this source, and their values.

Yields:

  • every key/value in the source

Yield Parameters:

  • name (String)

    of key

  • value (Object)

    of key



36
37
38
# File 'lib/cmdb/source/memory.rb', line 36

def each_pair(&block)
  @hash.each_pair(&block)
end

#get(key) ⇒ nil, ...

Get the value of key.

Returns:

  • (nil, String, Numeric, TrueClass, FalseClass, Array)

    the key’s value, or nil if not found



21
22
23
# File 'lib/cmdb/source/memory.rb', line 21

def get(key)
  @hash[key]
end

#set(key, value) ⇒ Object

Set the value of a key.



26
27
28
29
# File 'lib/cmdb/source/memory.rb', line 26

def set(key, value)
  value = JSON.dump(value) unless value.is_a?(String)
  @hash[key] = value
end