data_container 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/data_container.rb +31 -12
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 03c86dfbb52a0999baa5294ecc389e03093c9b29
4
- data.tar.gz: 65bce46c1b4a9d3fc0726dc36c7bcf4900fea413
3
+ metadata.gz: b88ea5add05a1fb7df10622d57484fdc7c8a814f
4
+ data.tar.gz: c6295f6b401ad8483402bd5ac08c657bd283d4db
5
5
  SHA512:
6
- metadata.gz: f5c14ecb983b260d46c959d33735c8ade3ff2beb5e2a4b2bff98ee9433387a5d8692175febfc98cb519c5182045d655ebb5af2f5edfc33f521b9ff47d369241c
7
- data.tar.gz: dfc41ed0bca4730cff932ad27ec8fe0889a15a15a5a2ff0bbfecb8d66776b18f3f702af1084d06bb2ba115f8706d6cd05da7568af69b34f4ad3737b1f7389a81
6
+ metadata.gz: 5b6ca4132e75294b3fae13ce4ec6882211de1eddcc70e080f00e5d9edbee44bc56121d8d191fc9eb7a03ec99de6bfc8738216f02d86a0318793706bd502c6768
7
+ data.tar.gz: f2a74f902c0a9ea69c2b810375426bc1fc656c99e0f868dea5c1916716630b9a3fc0ebc3aa7162a2aaf443438a88a396acceed29b6f7b67828e90fbebcc9b649
@@ -1,28 +1,39 @@
1
1
  class DataContainer < Struct
2
+ class AttributeError < StandardError; end
3
+
2
4
  def self.new(*args)
3
- super.new # send back an INSTANCE, not a class
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
- send(ivar) if include?(ivar)
19
+ do_if_included(ivar) { send(ivar) }
8
20
  end
9
21
 
10
22
  def set(ivar, value)
11
- send("#{ivar}=", value) if include?(ivar)
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
- '@' + variable_value_pairs_string.join(', @')
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 merge!(other_data_container)
23
- other_data_container.each_pair do |var, val|
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.1
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.4.1
41
+ rubygems_version: 2.2.2
42
42
  signing_key:
43
43
  specification_version: 4
44
44
  summary: DataContainer class