class BlackHoleStruct
BlackHoleStruct is a data structure similar to an OpenStruct, that allows infinite chaining of attributes or [autovivification](en.wikipedia.org/wiki/Autovivification).
Constants
- VERSION
Current version
Public Class Methods
BlackHoleStruct
can be optionally initialized with a Hash @param [Hash] hash Initialize with a hash @return [BlackHoleStruct]
# File lib/black_hole_struct.rb, line 10 def initialize(hash = {}) raise ArgumentError, "Argument should be a Hash" unless hash.is_a?(Hash) @table = {} hash.each do |key, value| value = self.class.new(value) if value.is_a?(Hash) @table[key.to_sym] = value end end
Public Instance Methods
Two BlackHoleStruct
are equal if they each contain the same number of keys and if each key-value pair is equal to the corresponding elements in the other BlackHoleStruct
. @param [BlackHoleStruct] @return [Boolean]
# File lib/black_hole_struct.rb, line 40 def ==(other) other.is_a?(self.class) && self.to_h == other.to_h end
Retrieves the value object corresponding to the key symbol. @param [Symbol] key @return the value object or a BlackHoleStruct
if nothing was found
# File lib/black_hole_struct.rb, line 23 def [](key) method_missing(key) end
Associates the value given by value with the key given by key. @param [Symbol] key @param value @return the value
# File lib/black_hole_struct.rb, line 31 def []=(key, value) method_missing("#{key}=", value) end
Returns a new hash with self and other_hash merged recursively. It only merges Hash recursively. @param [Hash] other_hash @return the final hash
# File lib/black_hole_struct.rb, line 85 def deep_merge(other_hash) self.dup.deep_merge!(other_hash) end
Same as deep_merge
, but modifies self. @param [Hash] other_hash @return the final hash
# File lib/black_hole_struct.rb, line 92 def deep_merge!(other_hash) other_hash.each_pair do |current_key, other_value| this_value = @table[current_key.to_sym] if (this_value.is_a?(Hash) || this_value.is_a?(self.class)) && (other_value.is_a?(Hash) || other_value.is_a?(self.class)) @table[current_key.to_sym] = this_value.deep_merge(other_value) else @table[current_key.to_sym] = other_value end end self end
Deletes the key-value pair and returns the value from hsh whose key is equal to key. If the key is not found, returns an instance of BlackHoleStruct
. @param [Symbol] key
# File lib/black_hole_struct.rb, line 48 def delete_field(key) @table[key.to_sym] = self.class.new end
Calls block once for each key passing the key-value pair as parameters. if no block is given, an Enumerator is returned instead. @yield [key, value]
# File lib/black_hole_struct.rb, line 55 def each_pair(&block) return @table.each_pair unless block @table.each_pair do |key, value| yield key, value end end
Return the contents of this instance as a string. @return [String]
# File lib/black_hole_struct.rb, line 119 def inspect "#<#{self.class}" .concat(@table.map { |k, v| " :#{k}=#{v.inspect}" }.join(" ")) .concat(">") end
Returns a new hash with self and other_hash merged. @param [Hash] other_hash @return the final hash
# File lib/black_hole_struct.rb, line 67 def merge(other_hash) self.dup.merge!(other_hash) end
Same as merge, but modifies self. @param [Hash] other_hash @return the final hash
# File lib/black_hole_struct.rb, line 74 def merge!(other_hash) # no deep merge @table = self.to_h.merge!(other_hash) self end
Converts self to the hash @return [Hash]
# File lib/black_hole_struct.rb, line 109 def to_h hash = {} @table.each do |key, value| hash[key] = value.is_a?(self.class) ? value.to_h : value end hash end
Private Instance Methods
# File lib/black_hole_struct.rb, line 128 def initialize_copy(other) super @table = @table.clone end
# File lib/black_hole_struct.rb, line 133 def method_missing(name, *args) if @table[name.to_sym] @table[name.to_sym] else if name[-1] == "=" @table[name[0..-2].to_sym] = args.first else @table[name.to_sym] = self.class.new end end end