Class: Ccp::Persistent::Versioned

Inherits:
Object
  • Object
show all
Defined in:
lib/ccp/persistent/versioned.rb

Defined Under Namespace

Modules: StorageScanner Classes: Storage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, options = {}) ⇒ Versioned

Returns a new instance of Versioned.



54
55
56
57
58
59
60
61
# File 'lib/ccp/persistent/versioned.rb', line 54

def initialize(dir, options = {})
  @path     = Pathname(dir)
  @kvs      = options[:kvs] || :dir
  @ext      = options[:ext] || :msgpack
  @storages = {}

  @path.mkpath
end

Instance Attribute Details

#extObject (readonly)

Returns the value of attribute ext.



52
53
54
# File 'lib/ccp/persistent/versioned.rb', line 52

def ext
  @ext
end

#kvsObject (readonly)

Returns the value of attribute kvs.



51
52
53
# File 'lib/ccp/persistent/versioned.rb', line 51

def kvs
  @kvs
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/ccp/persistent/versioned.rb', line 4

def path
  @path
end

Instance Method Details

#[](key) ⇒ Object

指定したストレージを返す。存在しなければ作成して返す



85
86
87
88
# File 'lib/ccp/persistent/versioned.rb', line 85

def [](key)
  storage = Storage.complete(key, path, @kvs, @ext)
  @storages[storage.to_s] ||= storage.create
end

#defaultObject

最新のストレージを返す。存在しなければ作成



75
76
77
# File 'lib/ccp/persistent/versioned.rb', line 75

def default
  latest || now
end

#inspectObject



90
91
92
# File 'lib/ccp/persistent/versioned.rb', line 90

def inspect
  "<Kvs::Versioned dir=#{path} kvs=#{@kvs} ext=#{@ext}>"
end

#latestObject

最新のストレージを返す。存在しなければnil



64
65
66
67
# File 'lib/ccp/persistent/versioned.rb', line 64

def latest
  storage = StorageScanner.scan(path).last
  storage ? self[storage] : nil
end

#latest!Object

最新のストレージを返す。存在しなければ例外



70
71
72
# File 'lib/ccp/persistent/versioned.rb', line 70

def latest!
  latest.must.exist { raise Ccp::Persistent::NotFound, "#{path}/*" }
end

#nowObject

現在の時刻で新しいストレージを作成して返す



80
81
82
# File 'lib/ccp/persistent/versioned.rb', line 80

def now
  self[Time.now.to_i]
end