Class: Hashish

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/puppet/feature/aviator/core/utils/hashish.rb

Overview

Hash-ish!

This class is implemented using composition rather than inheritance so that we have control over what operations it exposes to peers.

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Hashish

Returns a new instance of Hashish.



10
11
12
13
14
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 10

def initialize(hash={})
  @hash = hash.dup
  stringify_keys
  hashishify_values
end

Instance Method Details

#==(other_obj) ⇒ Object



16
17
18
19
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 16

def ==(other_obj)
  other_obj.class == self.class &&
  other_obj.hash == self.hash
end

#[](key) ⇒ Object



21
22
23
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 21

def [](key)
  @hash[normalize(key)]
end

#[]=(key, value) ⇒ Object



25
26
27
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 25

def []=(key, value)
  @hash[normalize(key)] = value
end

#dupObject



29
30
31
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 29

def dup
  Hashish.new(JSON.parse(@hash.to_json))
end

#each(&block) ⇒ Object



33
34
35
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 33

def each(&block)
  @hash.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 37

def empty?
  @hash.empty?
end

#has_key?(name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 41

def has_key?(name)
  @hash.has_key? normalize(name)
end

#hashObject



45
46
47
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 45

def hash
  @hash
end

#keysObject



49
50
51
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 49

def keys
  @hash.keys
end

#lengthObject



53
54
55
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 53

def length
  @hash.length
end

#merge(other_hash) ⇒ Object



57
58
59
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 57

def merge(other_hash)
  Hashish.new(@hash.merge(other_hash))
end

#merge!(other_hash) ⇒ Object



61
62
63
64
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 61

def merge!(other_hash)
  @hash.merge! other_hash
  self
end

#to_json(obj) ⇒ Object



66
67
68
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 66

def to_json(obj)
  @hash.to_json(obj)
end

#to_sObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/puppet/feature/aviator/core/utils/hashish.rb', line 70

def to_s
  str = "{"
  @hash.each do |key, value|
    if value.kind_of? String
      value = "'#{value}'"
    elsif value.nil?
      value = "nil"
    elsif value.kind_of? Array
      value = "[#{value.join(", ")}]"
    end

    str += " #{key}: #{value},"
  end

  str = str[0...-1] + " }"
  str
end