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.



51
52
53
54
55
56
57
58
# File 'lib/ccp/persistent/versioned.rb', line 51

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

  @path.mkpath
end

Instance Attribute Details

#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

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



82
83
84
85
# File 'lib/ccp/persistent/versioned.rb', line 82

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

#defaultObject

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



72
73
74
# File 'lib/ccp/persistent/versioned.rb', line 72

def default
  latest || now
end

#inspectObject



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

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

#latestObject

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



61
62
63
64
# File 'lib/ccp/persistent/versioned.rb', line 61

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

#latest!Object

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



67
68
69
# File 'lib/ccp/persistent/versioned.rb', line 67

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

#nowObject

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



77
78
79
# File 'lib/ccp/persistent/versioned.rb', line 77

def now
  self[Time.now.to_i]
end