data_container 0.0.1 → 0.0.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 +4 -4
- data/lib/data_container.rb +31 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b88ea5add05a1fb7df10622d57484fdc7c8a814f
|
4
|
+
data.tar.gz: c6295f6b401ad8483402bd5ac08c657bd283d4db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b6ca4132e75294b3fae13ce4ec6882211de1eddcc70e080f00e5d9edbee44bc56121d8d191fc9eb7a03ec99de6bfc8738216f02d86a0318793706bd502c6768
|
7
|
+
data.tar.gz: f2a74f902c0a9ea69c2b810375426bc1fc656c99e0f868dea5c1916716630b9a3fc0ebc3aa7162a2aaf443438a88a396acceed29b6f7b67828e90fbebcc9b649
|
data/lib/data_container.rb
CHANGED
@@ -1,28 +1,39 @@
|
|
1
1
|
class DataContainer < Struct
|
2
|
+
class AttributeError < StandardError; end
|
3
|
+
|
2
4
|
def self.new(*args)
|
3
|
-
|
5
|
+
if args.size == 1 && args.first.is_a?(Hash) # actually given a hash, not an array of values
|
6
|
+
new_class = super(*args.first.keys)
|
7
|
+
new_class.new(*args.first.values)
|
8
|
+
else
|
9
|
+
new_class = super(*args) # Struct constructor
|
10
|
+
new_class.new # send back an INSTANCE, not a class
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(*args)
|
15
|
+
super # Instance of Struct/new class constructor
|
4
16
|
end
|
5
17
|
|
6
18
|
def get(ivar)
|
7
|
-
|
19
|
+
do_if_included(ivar) { send(ivar) }
|
8
20
|
end
|
9
21
|
|
10
22
|
def set(ivar, value)
|
11
|
-
|
23
|
+
if include?(ivar)
|
24
|
+
send("#{ivar}=", value)
|
25
|
+
else
|
26
|
+
raise AttributeError, attribute_error_message(ivar)
|
27
|
+
end
|
12
28
|
end
|
13
29
|
|
14
30
|
def to_s
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
def inspect
|
19
|
-
'#<DataContainer: ' + variable_value_pairs_string.join(' ') + '>'
|
31
|
+
"#<#{name} " + variable_value_pairs_string.join(', ') + '>'
|
20
32
|
end
|
33
|
+
alias_method :inspect, :to_s
|
21
34
|
|
22
|
-
def
|
23
|
-
|
24
|
-
set(var, val) unless val.nil?
|
25
|
-
end
|
35
|
+
def name
|
36
|
+
'DataContainer'
|
26
37
|
end
|
27
38
|
|
28
39
|
def include?(var)
|
@@ -40,4 +51,12 @@ class DataContainer < Struct
|
|
40
51
|
def variable_value_pairs_string
|
41
52
|
each_pair.map { |ivar, value| "#{ivar}=#{value.inspect}" }
|
42
53
|
end
|
54
|
+
|
55
|
+
def attribute_error_message(attr)
|
56
|
+
"undefined attribute '#{attr}' for #{inspect} with backtrace:"
|
57
|
+
end
|
58
|
+
|
59
|
+
def do_if_included(ivar)
|
60
|
+
include?(ivar) ? yield(ivar) : raise(AttributeError, attribute_error_message(ivar))
|
61
|
+
end
|
43
62
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_container
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kelli Searfos
|
@@ -38,7 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
38
|
version: '0'
|
39
39
|
requirements: []
|
40
40
|
rubyforge_project:
|
41
|
-
rubygems_version: 2.
|
41
|
+
rubygems_version: 2.2.2
|
42
42
|
signing_key:
|
43
43
|
specification_version: 4
|
44
44
|
summary: DataContainer class
|