Class: Unobtainium::PathedHash

Inherits:
Object
  • Object
show all
Defined in:
lib/unobtainium/pathed_hash.rb

Overview

The PathedHash class wraps Hash by offering pathed access on top of regular access, i.e. instead of h[“second”] you can write h

Direct Known Subclasses

Config

Constant Summary collapse

READ_METHODS =
[
  :[], :default, :delete, :fetch, :has_key?, :include?, :key?, :member?,
].freeze
WRITE_METHODS =
[
  :[]=, :store,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init = {}) ⇒ PathedHash

Initializer



19
20
21
22
# File 'lib/unobtainium/pathed_hash.rb', line 19

def initialize(init = {})
  @data = init
  @separator = '.'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



88
89
90
91
92
93
# File 'lib/unobtainium/pathed_hash.rb', line 88

def method_missing(meth, *args, &block)
  if not @data.nil? and @data.respond_to?(meth)
    return @data.send(meth.to_s, *args, &block)
  end
  return super
end

Instance Attribute Details

#separatorObject

The separator is the character or pattern splitting paths



25
26
27
# File 'lib/unobtainium/pathed_hash.rb', line 25

def separator
  @separator
end

Instance Method Details

#respond_to?(meth) ⇒ Boolean

Map any missing method to the driver implementation

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/unobtainium/pathed_hash.rb', line 81

def respond_to?(meth)
  if not @data.nil? and @data.respond_to?(meth)
    return true
  end
  return super
end

#split_patternObject

Returns the pattern to split paths at



36
37
38
# File 'lib/unobtainium/pathed_hash.rb', line 36

def split_pattern
  /(?<!\\)#{Regexp.escape(@separator)}/
end

#to_sObject



75
76
77
# File 'lib/unobtainium/pathed_hash.rb', line 75

def to_s
  @data.to_s
end