datastruct 0.0.4 → 0.0.5

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/datastruct.rb +32 -4
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f644f7e9308ad7fb0737ea29a56dd59f4592a9e3
4
- data.tar.gz: 94098a1bcc0ad05ce47a5482b5e02f1adad9fd1e
3
+ metadata.gz: 97e4fe05fadd386ab6fb773de809bbca0454ff94
4
+ data.tar.gz: 5aca9f5798bba35e3d368e24cfb860b94f64c155
5
5
  SHA512:
6
- metadata.gz: 0b0a57edae6816b175e35151069200253fe7b8bb7686f4321a34d40a066bc724aabb8ae63ceb96c87a94058ccfef173d418c4d92195408fda84345363fc22749
7
- data.tar.gz: b7a3399733c0b7ebe7bac3181c53134391571e11cdc057a3b167f74cdd16dcce9a172789bf96c64b4f2e60784fef2830f8cc78944e73aa5770f1d0e5548ad67a
6
+ metadata.gz: 4bf51c8bd127617ce97d72bd07dc12507763695b059e240a862914bcd28a6fd22e235b6df967ba4cd659f124fb473913d25241286c1f1bd31fa358cd65231707
7
+ data.tar.gz: 51ce097e91dcbda11e808af8efe7e181eae29f165b617a4117e46ba762a3cdc5d9468c8b360601c5d044c04dd62e6699b586063554f08cb8171d28da4d7deae9
@@ -11,11 +11,28 @@ def DataStruct(*props, &block)
11
11
  end
12
12
 
13
13
  class DataStruct
14
- VERSION = "0.0.4"
14
+ VERSION = "0.0.5"
15
15
 
16
+ ##
17
+ # Makes sure +@data+ is set before +initialize+ is called
18
+ #
19
+ # The +@data+ instance variable is set to a hash with each key in +PROPERTIES+
20
+ # set to +nil+ before the constructor is called. This avoids a host of
21
+ # annoying issues when users override the constructor.
22
+ #
16
23
  def self.new(*args, &block)
17
24
  instance = allocate()
18
- instance.instance_variable_set(:@data, {})
25
+
26
+ data_container = {}
27
+
28
+ if instance.class.const_defined? :PROPERTIES
29
+ properties = instance.class::PROPERTIES
30
+ else
31
+ properties = []
32
+ end
33
+
34
+ properties.each { |key| data_container[key] = nil }
35
+ instance.instance_variable_set(:@data, data_container)
19
36
 
20
37
  instance.send(:initialize, *args, &block)
21
38
 
@@ -64,6 +81,13 @@ class DataStruct
64
81
  @data.each(*args, &block)
65
82
  end
66
83
 
84
+ ##
85
+ # Returns a shallow copy
86
+ #
87
+ def dup
88
+ self.class.from_hash(@data.dup)
89
+ end
90
+
67
91
  ##
68
92
  # Returns a property using its getter method
69
93
  #
@@ -150,8 +174,12 @@ class DataStruct
150
174
  #
151
175
  # @return [Hash]
152
176
  #
153
- def to_hash
154
- @data.dup
177
+ def to_hash(string_keys: false)
178
+ if string_keys
179
+ Hash[@data.to_a.map { |key, value| [key.to_s, value] }]
180
+ else
181
+ @data.dup
182
+ end
155
183
  end
156
184
 
157
185
  alias_method :to_h, :to_hash
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datastruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Sandven
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-13 00:00:00.000000000 Z
11
+ date: 2016-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
138
  version: '0'
139
139
  requirements: []
140
140
  rubyforge_project:
141
- rubygems_version: 2.4.5.1
141
+ rubygems_version: 2.5.1
142
142
  signing_key:
143
143
  specification_version: 4
144
144
  summary: A great base class for data structures