Class: Lab42::NHash

Inherits:
Object
  • Object
show all
Extended by:
Forwarder, ClassMethods
Includes:
Afixes, Fallbacks, Hierarchy, Interpolation, Invocation, LookupChains
Defined in:
lib/lab42/nhash.rb,
lib/lab42/nhash/enum.rb,
lib/lab42/nhash/affixes.rb,
lib/lab42/nhash/version.rb,
lib/lab42/nhash/fallbacks.rb,
lib/lab42/nhash/exceptions.rb,
lib/lab42/nhash/invocation.rb,
lib/lab42/nhash/hierarchies.rb,
lib/lab42/nhash/class_methods.rb,
lib/lab42/nhash/interpolation.rb,
lib/lab42/nhash/lookup_chains.rb

Defined Under Namespace

Modules: Afixes, ClassMethods, Fallbacks, Hierarchy, Interpolation, Invocation, LookupChains Classes: Enum

Constant Summary collapse

VERSION =
'0.1.2'
IllegalStateError =
Class.new RuntimeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

from_sources, from_sources_with_indifferent_access, from_value

Methods included from LookupChains

#push_affix_lookup, #push_prefix_lookup, #push_suffix_lookup

Methods included from Interpolation

#current_binding, #get!, #get_with_binding, #get_with_current, #root?, #with_binding

Methods included from Hierarchy

#add_hierarchies, #add_hierarchy, #get_from_hierarchies

Methods included from Fallbacks

#again, #clear_fallbacks!, #pop_fallback, #push_fallback, #with_fallback

Methods included from Afixes

#current_prefix, #current_suffix, #pop_prefix, #pop_suffix, #push_prefix, #push_suffix, #with_affixes, #with_prefix, #with_suffix

Instance Attribute Details

#hashyObject (readonly)

Returns the value of attribute hashy.



21
22
23
# File 'lib/lab42/nhash.rb', line 21

def hashy
  @hashy
end

#hierarchiesObject (readonly)

Returns the value of attribute hierarchies.



21
22
23
# File 'lib/lab42/nhash.rb', line 21

def hierarchies
  @hierarchies
end

#parentObject (readonly)

Returns the value of attribute parent.



21
22
23
# File 'lib/lab42/nhash.rb', line 21

def parent
  @parent
end

Instance Method Details

#export_optionsObject



23
24
25
26
27
28
# 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) ⇒ Object



41
42
43
44
45
# 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

#get(keyexpr, *default, &defblock) ⇒ Object Also known as: fetch



30
31
32
33
34
35
36
37
38
# 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

#import_options(options) ⇒ Object



47
48
49
50
51
52
# 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

#with_indifferent_accessObject



54
55
56
57
58
# 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 = {}) ⇒ Object



61
62
63
# File 'lib/lab42/nhash.rb', line 61

def with_indifferent_access! cache={}
  __recursive_indifferent_access__ self, {}
end