Class: Store2::File
- Inherits:
-
Struct
- Object
- Struct
- Store2::File
- Defined in:
- lib/store2/file.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
Class Method Summary collapse
Instance Method Summary collapse
- #_reset ⇒ Object
- #build ⇒ Object
- #fetch(keys, &block) ⇒ Object
- #get(keys) ⇒ Object
- #get_or_set(keys, value) ⇒ Object
- #has?(keys) ⇒ Boolean
-
#initialize(*args) ⇒ File
constructor
A new instance of File.
- #save ⇒ Object
- #scoped(*keys) ⇒ Object
- #set(keys, value) ⇒ Object
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
#filename ⇒ Object
Returns the value of attribute filename
2 3 4 |
# File 'lib/store2/file.rb', line 2 def filename @filename end |
Class Method Details
._reset ⇒ Object
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
#_reset ⇒ Object
43 44 45 |
# File 'lib/store2/file.rb', line 43 def _reset create end |
#build ⇒ Object
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
25 26 27 |
# File 'lib/store2/file.rb', line 25 def has?(keys) get(keys[0...-1]).has_key?(keys[-1]) end |
#save ⇒ Object
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 |