Class: Tardvig::HashContainer
- Inherits:
-
Object
- Object
- Tardvig::HashContainer
- Includes:
- DataIdentifier, Events
- Defined in:
- lib/tardvig/hash_container.rb
Overview
It is container for data, which is similar to Ruby’s Hash, but it has more narrow purpose and corresponding changes in the list of methods. It is not Enumerable, so it has no ‘sort` method, `each`, `map`, etc. It allows to read, write, delete elements, save itself to/load from a file or IO, it has events and data identifier. **It is expected that you already know what is hash**.
Events:
-
‘save` happen before saving data to a file. Arguments: self
-
‘load` happen after loading data from a file. Arguments: self
-
‘change` happen when there are some objects have been deleted from/added to this container. Arguments: self
Instance Method Summary collapse
-
#[](key) ⇒ Object
Value associated with the given key through the #[]= method or ‘nil` if there is no such value.
-
#[]=(key, value) ⇒ Object
Adds your value to this container, so you can access it through the #[] method later using your key.
-
#delete(key) ⇒ Object
Removes the given key and corresponding value from this container, so you can not access it further.
-
#initialize(hash = {}) ⇒ HashContainer
constructor
A new instance of HashContainer.
-
#load(io) ⇒ Object
Loads the data from the YAML file to itself.
-
#save(io) ⇒ Object
Saves this container’s content to the file through YAML.
-
#to_json ⇒ Object
Converts current hash to JSON.
-
#to_yaml(*args) ⇒ Object
Converts current hash to YAML.
Methods included from Events
#happen, #listeners, #on, #on_first, #remove_listener
Methods included from DataIdentifier
Constructor Details
#initialize(hash = {}) ⇒ HashContainer
Returns a new instance of HashContainer.
21 22 23 |
# File 'lib/tardvig/hash_container.rb', line 21 def initialize(hash = {}) @hash = hash end |
Instance Method Details
#[](key) ⇒ Object
Returns value associated with the given key through the #[]= method or ‘nil` if there is no such value.
30 31 32 |
# File 'lib/tardvig/hash_container.rb', line 30 def [](key) @hash[key] end |
#[]=(key, value) ⇒ Object
Adds your value to this container, so you can access it through the #[] method later using your key.
40 41 42 43 44 |
# File 'lib/tardvig/hash_container.rb', line 40 def []=(key, value) @hash[key] = value trigger :change, self value end |
#delete(key) ⇒ Object
Removes the given key and corresponding value from this container, so you can not access it further.
51 52 53 54 55 |
# File 'lib/tardvig/hash_container.rb', line 51 def delete(key) value = @hash.delete key trigger :change, self value end |
#load(io) ⇒ Object
Loads the data from the YAML file to itself. If there are old data in the hash, it overrides them.
71 72 73 74 75 76 77 |
# File 'lib/tardvig/hash_container.rb', line 71 def load(io) open_file io, 'r' do |f| @hash.merge! YAML.load(f.read) trigger :load, self trigger :change, self end end |
#save(io) ⇒ Object
Saves this container’s content to the file through YAML
60 61 62 63 64 65 |
# File 'lib/tardvig/hash_container.rb', line 60 def save(io) open_file io, 'w' do |f| trigger :save, self f.write YAML.dump(@hash) end end |
#to_json ⇒ Object
Converts current hash to JSON.
86 87 88 |
# File 'lib/tardvig/hash_container.rb', line 86 def to_json(*) JSON.generate @hash end |
#to_yaml(*args) ⇒ Object
Converts current hash to YAML. Arguments are similar to standard Ruby’s Object#to_yaml
81 82 83 |
# File 'lib/tardvig/hash_container.rb', line 81 def to_yaml(*args) @hash.to_yaml(*args) end |