Class: Cript::EHash

Inherits:
Object
  • Object
show all
Defined in:
lib/cript/ehash.rb

Overview

A hash backed by a Cript::Store object. All methods sent to an instance of this object are wrapped in a transaction and executed immediately.

Constant Summary collapse

METHODS =
Hash.new.methods - Object.new.methods
KEY =
:data

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ EHash

Returns a new instance of EHash.



13
14
15
16
17
18
19
20
# File 'lib/cript/ehash.rb', line 13

def initialize(file, options = {})
  @store = Store.new(file, options)
  @store.transaction do
    unless @store[KEY].is_a?(Hash)
      @store[KEY] = {}
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/cript/ehash.rb', line 26

def method_missing(sym, *args, &block)
  super if !METHODS.include?(sym) || block_given?

  @store.transaction do
    @store[KEY].send(sym, *args)
  end
end

Instance Method Details

#inspectObject



22
23
24
# File 'lib/cript/ehash.rb', line 22

def inspect
  "#<#{self.class.name} path='#{@store.path}'>"
end

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/cript/ehash.rb', line 34

def respond_to?(sym)
  METHODS.include?(sym)
end