class Libis::Tools::DeepStruct

A class that derives from OpenStruct through the RecursiveOpenStruct. By wrapping a Hash recursively, it allows for easy access to the content by method names. A RecursiveOpenStruct is derived from stdlib's OpenStruct, but can be made recursive. DeepStruct enforces this behaviour and adds a clear! method.

Public Class Methods

new(hash = {}, opts = {}) click to toggle source

Create a new DeepStruct from a Hash and configure the behaviour.

@param [Hash] hash the initial data structure. @param [Hash] opts optional configuration options:

* recurse_over_arrays: also wrap the Hashes that are enbedded in Arrays. Default: true.
* preserver_original_keys: creating a Hash from the wrapper preserves symbols and strings as keys. Default: true.
Calls superclass method
# File lib/libis/tools/deep_struct.rb, line 20
def initialize(hash = {}, opts = {})
  hash = {} unless hash
  opts = {} unless opts
  hash = {default: hash} unless hash.is_a? Hash
  super(hash, {recurse_over_arrays: true, preserve_original_keys: true}.merge(opts))
end

Public Instance Methods

clear!() click to toggle source

Delete all data fields

# File lib/libis/tools/deep_struct.rb, line 64
def clear!
  @table.keys.each { |key| delete_field(key) }
  @sub_elements = {}
end
each(&block) click to toggle source
# File lib/libis/tools/deep_struct.rb, line 59
def each(&block)
  self.each_pair &block
end
has_key?(key)
Alias for: key?
key?(key) click to toggle source
# File lib/libis/tools/deep_struct.rb, line 50
def key?(key)
  self.respond_to?(key)
end
Also aliased as: has_key?
keys() click to toggle source
# File lib/libis/tools/deep_struct.rb, line 55
def keys
  @table.keys
end
merge(hash) click to toggle source
# File lib/libis/tools/deep_struct.rb, line 27
def merge(hash)
  return self unless hash.respond_to?(:to_hash)
  hash.to_hash.inject(self.dup) do |ds, (key, value)|
    ds[key] = DeepDup.new(
        recurse_over_arrays: @options[:recurse_over_arrays],
        preserve_original_keys: @options[:preserve_original_keys]
    ).call(value)
    ds
  end
end
merge!(hash) click to toggle source
# File lib/libis/tools/deep_struct.rb, line 38
def merge!(hash)
  return self unless hash.respond_to?(:to_hash)
  hash.to_hash.inject(self) do |ds, (key, value)|
    ds[key] = DeepDup.new(
        recurse_over_arrays: @options[:recurse_over_arrays],
        preserve_original_keys: @options[:preserve_original_keys]
    ).call(value)
    ds
  end
  self
end