Class: Store2::File

Inherits:
Struct
  • Object
show all
Defined in:
lib/store2/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ File

Returns a new instance of File.



3
4
5
6
7
# File 'lib/store2/file.rb', line 3

def initialize(*args)
  super
  @data = load
  self.class.register(self)
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename

Returns:

  • (Object)

    the current value of filename



2
3
4
# File 'lib/store2/file.rb', line 2

def filename
  @filename
end

Class Method Details

._resetObject



78
79
80
# File 'lib/store2/file.rb', line 78

def _reset
  instances.each(&:_reset)
end

.register(instance) ⇒ Object



74
75
76
# File 'lib/store2/file.rb', line 74

def register(instance)
  instances << instance
end

Instance Method Details

#_resetObject



43
44
45
# File 'lib/store2/file.rb', line 43

def _reset
  create
end

#buildObject



9
10
11
# File 'lib/store2/file.rb', line 9

def build
  scoped
end

#fetch(keys, &block) ⇒ Object



34
35
36
37
# File 'lib/store2/file.rb', line 34

def fetch(keys, &block)
  return block.call unless has?(keys)
  get(keys)
end

#get(keys) ⇒ Object



17
18
19
# File 'lib/store2/file.rb', line 17

def get(keys)
  keys.inject(data) { |data, key| data.fetch(key) }
end

#get_or_set(keys, value) ⇒ Object



29
30
31
32
# File 'lib/store2/file.rb', line 29

def get_or_set(keys, value)
  set(keys, value) unless has?(keys)
  get(keys)
end

#has?(keys) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/store2/file.rb', line 25

def has?(keys)
  get(keys[0...-1]).has_key?(keys[-1])
end

#saveObject



39
40
41
# File 'lib/store2/file.rb', line 39

def save
  ::File.write(filename, to_yaml)
end

#scoped(*keys) ⇒ Object



13
14
15
# File 'lib/store2/file.rb', line 13

def scoped(*keys)
  Scoped.new(self, keys)
end

#set(keys, value) ⇒ Object



21
22
23
# File 'lib/store2/file.rb', line 21

def set(keys, value)
  get(keys[0...-1])[keys[-1]] = value
end