Class: HashDial::HashDialler

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_dial/hash_dialler.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash, *lookup) ⇒ HashDialler

Returns a new instance of HashDialler.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hash_dial/hash_dialler.rb', line 9

def initialize(hash, *lookup)
  if hash.is_a?(Hash)
    @hash = hash
  else
    @hash = {}
  end
  @lookup = []
  if lookup.length > 0
    dial!(*lookup)
  end
end

Instance Method Details

#+(key) ⇒ Object



52
53
54
# File 'lib/hash_dial/hash_dialler.rb', line 52

def +(key)
  return dial!(key)
end

#-(key) ⇒ Object



55
56
57
# File 'lib/hash_dial/hash_dialler.rb', line 55

def -(key)
  return undial!(key)
end

#[](key) ⇒ Object



49
50
51
# File 'lib/hash_dial/hash_dialler.rb', line 49

def [](key)
  return dial!(key)
end

#call(default = nil) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/hash_dial/hash_dialler.rb', line 27

def call(default = nil)
  begin
    value = @hash.dig(*@lookup)
  rescue
    value = default
  end
  return value
end

#dial!(*keys) ⇒ Object



21
22
23
24
25
# File 'lib/hash_dial/hash_dialler.rb', line 21

def dial!(*keys)
  #unless key.is_a(Symbol) || key.is_a(String)

  @lookup += keys
  return self
end

#hangupObject



36
37
38
# File 'lib/hash_dial/hash_dialler.rb', line 36

def hangup
  return @hash
end

#undial!(*keys) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/hash_dial/hash_dialler.rb', line 40

def undial!(*keys)
  if keys.length > 0
    @lookup -= keys
  elsif @lookup.length > 0
    @lookup.pop
  end
  return self
end