Class: Sheng::DataSet
- Inherits:
-
Object
- Object
- Sheng::DataSet
- Defined in:
- lib/sheng/data_set.rb
Defined Under Namespace
Classes: KeyNotFound
Instance Attribute Summary collapse
-
#raw_hash ⇒ Object
Returns the value of attribute raw_hash.
Instance Method Summary collapse
- #fetch(key, **options) ⇒ Object
-
#initialize(hsh) ⇒ DataSet
constructor
A new instance of DataSet.
- #raise_key_too_long(key, key_part) ⇒ Object
- #raise_key_too_short(key) ⇒ Object
Constructor Details
#initialize(hsh) ⇒ DataSet
Returns a new instance of DataSet.
7 8 9 10 |
# File 'lib/sheng/data_set.rb', line 7 def initialize(hsh) raise ArgumentError.new("must be initialized with a Hash") unless hsh.is_a?(Hash) @raw_hash = hsh.deep_symbolize_keys end |
Instance Attribute Details
#raw_hash ⇒ Object
Returns the value of attribute raw_hash.
5 6 7 |
# File 'lib/sheng/data_set.rb', line 5 def raw_hash @raw_hash end |
Instance Method Details
#fetch(key, **options) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sheng/data_set.rb', line 20 def fetch(key, **) raise ArgumentError.new("must provide a string") unless key.is_a?(String) key_parts = key.split(/\./) current_result = raw_hash key_parts.each_with_index do |key_part, i| begin value = current_result.fetch(key_part.to_sym) rescue KeyError if .has_key?(:default) value = [:default] else raise KeyNotFound, "#{key} (at #{key_part})" end end if (i + 1) < key_parts.length raise_key_too_long(key, key_part) if !(value.is_a?(Hash)) end current_result = value end raise_key_too_short(key) if current_result.is_a?(Hash) current_result end |
#raise_key_too_long(key, key_part) ⇒ Object
12 13 14 |
# File 'lib/sheng/data_set.rb', line 12 def raise_key_too_long(key, key_part) raise KeyNotFound, "in #{key}, #{key_part} did not return a Hash" end |
#raise_key_too_short(key) ⇒ Object
16 17 18 |
# File 'lib/sheng/data_set.rb', line 16 def raise_key_too_short(key) raise KeyNotFound, "result at #{key} is a Hash" end |