Class: AngryHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/angry_hash.rb,
lib/angry_hash/merge_string.rb

Defined Under Namespace

Modules: MergeString

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/angry_hash.rb', line 64

def method_missing(method,*args,&blk)
  method_s = method.to_s

  key = method_s[0..-2]

  case method_s[-1]
  when ?=
    self[ key ] = args.first

  when ??
    !! self[key]

  when ?!
    self[key] ||= AngryHash.new

  else
    self[method_s]
  end
end

Class Method Details

.[](other) ⇒ Object



2
3
4
# File 'lib/angry_hash.rb', line 2

def self.[](other)
  super(__convert(other))
end

.__convert(hash) ⇒ Object



91
92
93
94
95
96
# File 'lib/angry_hash.rb', line 91

def self.__convert(hash)
  hash.inject(AngryHash.new) do |hash,(k,v)|
    hash[__convert_key(k)] = __convert_value(v)
    hash
  end
end

.__convert_key(key) ⇒ Object



87
88
89
# File 'lib/angry_hash.rb', line 87

def self.__convert_key(key)
  Symbol === key ? key.to_s : key
end

.__convert_value(v) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/angry_hash.rb', line 98

def self.__convert_value(v)
  v = v.to_hash if v.respond_to?(:to_hash)

  case v
  when AngryHash
    v
  when Hash
    __convert(v)
  when Array
    v.map {|v| Hash === v ? __convert_value(v) : v}
  else
    v
  end
end

Instance Method Details

#[](key) ⇒ Object



13
14
15
# File 'lib/angry_hash.rb', line 13

def [](key)
  regular_reader(__convert_key(key))
end

#[]=(key, value) ⇒ Object



10
11
12
# File 'lib/angry_hash.rb', line 10

def []=(key, value)
  regular_writer(__convert_key(key), AngryHash.__convert_value(value))
end

#__convert_key(key) ⇒ Object



84
85
86
# File 'lib/angry_hash.rb', line 84

def __convert_key(key)
  Symbol === key ? key.to_s : key
end

#deep_merge(other_hash) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/angry_hash.rb', line 23

def deep_merge(other_hash)
  self.merge(other_hash) do |key, oldval, newval|
    oldval = AngryHash.__convert_value(oldval)
    newval = AngryHash.__convert_value(newval)

    AngryHash === oldval && AngryHash === newval ? oldval.deep_merge(newval) : newval
  end
end

#deep_merge!(other_hash) ⇒ Object



32
33
34
# File 'lib/angry_hash.rb', line 32

def deep_merge!(other_hash)
  replace(deep_merge(other_hash))
end

#delete(key) ⇒ Object



56
57
58
# File 'lib/angry_hash.rb', line 56

def delete(key)
  super __convert_key(key)
end

#fetch(key, *extras) ⇒ Object



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

def fetch(key, *extras)
  super(__convert_key(key), *extras)
end

#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?

Returns:

  • (Boolean)


41
42
43
# File 'lib/angry_hash.rb', line 41

def key?(key)
  super(__convert_key(key))
end

#regular_readerObject



7
# File 'lib/angry_hash.rb', line 7

alias_method :regular_reader, :[]

#regular_updateObject



8
# File 'lib/angry_hash.rb', line 8

alias_method :regular_update, :update

#regular_writerObject



6
# File 'lib/angry_hash.rb', line 6

alias_method :regular_writer, :[]=

#reverse_deep_merge!(other_hash) ⇒ Object



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

def reverse_deep_merge!(other_hash)
  replace(self.class.__convert_value(other_hash).deep_merge(self))
end

#to_hashObject



60
61
62
# File 'lib/angry_hash.rb', line 60

def to_hash
  self
end

#update(other_hash) ⇒ Object Also known as: merge!



17
18
19
20
# File 'lib/angry_hash.rb', line 17

def update(other_hash)
  other_hash.each_pair { |key, value| self[key] = value }
  self
end

#values_at(*indices) ⇒ Object



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

def values_at(*indices)
  indices.collect {|key| self[__convert_key(key)]}
end