class Lab42::NHash

Constants

IllegalStateError
VERSION

Attributes

hashy[R]
hierarchies[R]
parent[R]

Private Class Methods

new(hashy={}) click to toggle source
# File lib/lab42/nhash.rb, line 97
def initialize hashy={}, options={}
  @hashy  = hashy
  init_options options
end

Private Instance Methods

__recursive_indifferent_access__(for_nhash, cache) click to toggle source
# File lib/lab42/nhash.rb, line 66
def __recursive_indifferent_access__ for_nhash, cache
  return if cache[ for_nhash.object_id ]

  for_nhash.with_indifferent_access
  cache.update for_nhash.object_id => true

  for_nhash.hierarchies.each do | hier |
    __recursive_indifferent_access__ hier, cache
  end
end
_get(keys) click to toggle source

TODO: This is inefficent, either cache it or split on pushing onto stack

# File lib/lab42/nhash.rb, line 83
def _get keys
  keys.inject hashy do | partial_hash, key_element |
    raise KeyError, keys.join('.') unless Hash === partial_hash
    partial_hash.fetch key_element
  end
end
_get_indiff(keys) click to toggle source
# File lib/lab42/nhash.rb, line 90
def _get_indiff keys
  keys.inject hashy do | partial_hash, key_element |
    raise KeyError, keys.join('.') unless Hash === partial_hash
    partial_hash.fetch( key_element ){ partial_hash.fetch key_element.to_sym }
  end
end
complete_keys(keys, use_prefix: false, use_suffix: false) click to toggle source
# File lib/lab42/nhash.rb, line 76
def complete_keys keys, use_prefix: false, use_suffix: false
  keys =  current_prefix + keys if use_prefix
  keys += current_suffix if use_suffix
  keys
end
export_options() click to toggle source
# File lib/lab42/nhash.rb, line 23
def export_options
  { indifferent_access: @indifferent_access,
    binding_stack: @binding_stack.dup,
    parent: @parent
  }
end
fallback_or_hierarchy(keyexpr, keyexc) click to toggle source
# File lib/lab42/nhash.rb, line 41
def fallback_or_hierarchy keyexpr, keyexc
  fallback keyexpr, keyexc
rescue KeyError => k
  get_from_hierarchies keyexpr, k
end
fetch(keyexpr, *default, &defblock)
Alias for: get
get(keyexpr, *default, &defblock) click to toggle source
# File lib/lab42/nhash.rb, line 30
def get keyexpr, *default, &defblock
  keys = keyexpr.to_s.split( '.' )
  keys = complete_keys keys.reject(&:empty?), use_prefix: keys.first.empty?, use_suffix: keyexpr[-1] == '.'
  found = @indifferent_access ? _get_indiff( keys ) : _get( keys )
  self.class.from_value found, export_options
rescue KeyError => k
  return fallback_or_hierarchy keyexpr, k if default.empty? && defblock.nil?
  default.empty? ? defblock.(keyexpr) : default.first
end
Also aliased as: fetch
import_options(options) click to toggle source
# File lib/lab42/nhash.rb, line 47
def import_options options
  @indifferent_access = options[:indifferent_access]
  @binding_stack      = options[:binding_stack] || []
  @parent             = options[:parent]
  self
end
init_options(options) click to toggle source
# File lib/lab42/nhash.rb, line 102
def init_options options
  @parent = self

  @indifferent_access       = options.fetch(:indifferent_access, false)
  @suffix_stack             = options.fetch(:suffix_stack, [])
  @prefix_stack             = options.fetch(:prefix_stack, [])
  @binding_stack            = options.fetch(:binding_stack, [])

  @fallbacks                = []
  @current_fallback_pointer = 0

  @hierarchies              = []
end
with_indifferent_access() click to toggle source
# File lib/lab42/nhash.rb, line 54
def with_indifferent_access
  self.class.new( @hashy ).tap do |rv|
    rv.instance_variable_set :@indifferent_access, true
  end
end
with_indifferent_access!(cache={}) click to toggle source
# File lib/lab42/nhash.rb, line 61
def with_indifferent_access! cache={}
  __recursive_indifferent_access__ self, {}
end