Class: Tardvig::HashContainer

Inherits:
Object
  • Object
show all
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

Methods included from Events

#happen, #listeners, #on, #on_first, #remove_listener

Methods included from DataIdentifier

#get_this

Constructor Details

#initialize(hash = {}) ⇒ HashContainer

Returns a new instance of HashContainer.

Parameters:

  • hash (Hash) (defaults to: {})

    default Ruby’s Hash. If it is given, the container will be created from it (it will have the same key => value pairs).



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.

Parameters:

  • key (Object)

    the object which you previously associated with value

Returns:

  • value associated with the given key through the #[]= method or ‘nil` if there is no such value.

See Also:



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.

Parameters:

  • key (Object)

    object which is used as an identifier of your value

  • value (Object)

    anything you want to store in this container

Returns:

  • value

See Also:

  • Hash


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.

Parameters:

  • key (Object)

    the object which you previously associated with value

Returns:

  • value associated with the given key through the #[]= method or ‘nil` if there is no such value.

See Also:

  • Hash


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.

Parameters:

  • io (IO, String)

    the IO instance or the name of the file. The data will be loaded from here.



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

Parameters:

  • io (IO, String)

    the IO instance or the name of the file. The content will be saved here.



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_jsonObject

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