tardvig 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6d19399953954acf68f31e5a9fb17befd2599a4b
4
- data.tar.gz: adeb97c978d563c469aee6490d317adf174569eb
3
+ metadata.gz: 9e5efc7f9212fc72c23ca3e0eb60ea50c41370b7
4
+ data.tar.gz: b471b0c34344b11fa98c475a18aebcac5ec20ea9
5
5
  SHA512:
6
- metadata.gz: d219400808b22aad2ec4cb4c7d920af26b8b1227880b9b4f2292fa1c08a8df0434a8cdbf6b5e4a693f60987b12e1762389d74e3229443900a867b03b47bee594
7
- data.tar.gz: 2cc8af78421f3b9877373b72993b10756703f5200c5f2aacb714e7398290d7502040a44f2a794bed415893baeb116420fed02f02911c5a859eca62f668a6307b
6
+ metadata.gz: 861878ba9b5a3fb3cc2cfd77dd3f3d6f6caab5dfd3c7ed805cc0866f89a43f60a9b4a50938fab37f7ef1ba7769f23d4976c9aa80914c5633c6b56ba9beeba626
7
+ data.tar.gz: 8a696bb84b3e59391b8fd087ff3ff454f8cb732e6ca66fe0bf934ff48f1ea8acfc58dd806aaf636fd30addcabc3fb2400f37129a52150a0a45cc63fcdcdea857
@@ -0,0 +1,91 @@
1
+ module Tardvig
2
+ # It is container for data, which is similar to Ruby's Hash, but it has more
3
+ # narrow purpose and corresponding changes in the list of methods.
4
+ # It is not Enumerable, so it has no `sort` method, `each`, `map`, etc.
5
+ # It allows to read, write, delete elements, save itself to/load from a file
6
+ # or IO, it has events and data identifier.
7
+ # **It is expected that you already know what is hash**.
8
+ #
9
+ # Events:
10
+ # * `save` happen before saving data to a file. Arguments: self
11
+ # * `load` happen after loading data from a file. Arguments: self
12
+ # * `change` happen when there are some objects have been
13
+ # deleted from/added to this container. Arguments: self
14
+ class HashContainer
15
+ include DataIdentifier
16
+ include Events
17
+
18
+ # @param hash [Hash] default Ruby's Hash.
19
+ # If it is given, the container will be created from it (it will have the
20
+ # same key => value pairs).
21
+ def initialize(hash = {})
22
+ @hash = hash
23
+ end
24
+
25
+ # @param key [Object] the object which you previously associated with value
26
+ # @return value associated with the given key through the {#[]=} method or
27
+ # `nil` if there is no such value.
28
+ # @see #[]=
29
+ # @see Ruby's Hash
30
+ def [](key)
31
+ @hash[key]
32
+ end
33
+
34
+ # Adds your value to this container, so you can access it through the {#[]}
35
+ # method later using your key.
36
+ # @param key [Object] object which is used as an identifier of your value
37
+ # @param value [Object] anything you want to store in this container
38
+ # @return value
39
+ # @see Ruby's Hash
40
+ def []=(key, value)
41
+ @hash[key] = value
42
+ trigger :change, self
43
+ value
44
+ end
45
+
46
+ # Removes the given key and corresponding value from this container, so
47
+ # you can not access it further.
48
+ # @param (see #[])
49
+ # @return (see #[])
50
+ # @see Ruby's Hash
51
+ def delete(key)
52
+ value = @hash.delete key
53
+ trigger :change, self
54
+ value
55
+ end
56
+
57
+ # Saves this container's content to the file through YAML
58
+ # @param io [IO, String] the IO instance or the name of the file.
59
+ # The content will be saved here.
60
+ def save(io)
61
+ open_file io, 'w' do |f|
62
+ trigger :save, self
63
+ f.write YAML.dump(@hash)
64
+ end
65
+ end
66
+
67
+ # Loads the data from the YAML file to itself. If there are old data in the
68
+ # hash, it overrides them.
69
+ # @param io [IO, String] the IO instance or the name of the file.
70
+ # The data will be loaded from here.
71
+ def load(io)
72
+ open_file io, 'r' do |f|
73
+ @hash.merge! YAML.load(f.read)
74
+ trigger :load, self
75
+ trigger :change, self
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ def open_file(io, mode)
82
+ if io.respond_to?(:read) && io.respond_to?(:write)
83
+ yield io
84
+ elsif io.is_a? String
85
+ File.open(io, mode) { |f| yield f }
86
+ else
87
+ raise ArgumentError
88
+ end
89
+ end
90
+ end
91
+ end
@@ -8,6 +8,8 @@ module Tardvig
8
8
  # Events:
9
9
  # * `save` happen before saving data to a file. Arguments: the hash itself
10
10
  # * `load` happen after loading data from a file. Arguments: the hash itself
11
+ #
12
+ # @deprecated Use HashContainer instead.
11
13
  class SavedHash < Hash
12
14
  include Events
13
15
 
@@ -1,3 +1,3 @@
1
1
  module Tardvig
2
- VERSION = '0.2.1'.freeze
2
+ VERSION = '0.2.2'.freeze
3
3
  end
data/lib/tardvig.rb CHANGED
@@ -9,6 +9,7 @@ require_relative 'tardvig/gameio.rb'
9
9
  require_relative 'tardvig/act.rb'
10
10
  require_relative 'tardvig/data_identifier.rb'
11
11
  require_relative 'tardvig/events/proxy.rb'
12
+ require_relative 'tardvig/hash_container.rb'
12
13
 
13
14
  # The main namespace for the gem
14
15
  module Tardvig
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tardvig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Saprykin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-24 00:00:00.000000000 Z
11
+ date: 2016-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -84,6 +84,7 @@ files:
84
84
  - lib/tardvig/events.rb
85
85
  - lib/tardvig/events/proxy.rb
86
86
  - lib/tardvig/gameio.rb
87
+ - lib/tardvig/hash_container.rb
87
88
  - lib/tardvig/saved_hash.rb
88
89
  - lib/tardvig/toolkit.rb
89
90
  - lib/tardvig/version.rb