Class: Gumdrop::Util::HashObject

Inherits:
Hash
  • Object
show all
Defined in:
lib/gumdrop/util/hash_object.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#ends_with?, #starts_with?, #to_hash_object, #to_symbolized_hash

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gumdrop/util/hash_object.rb', line 16

def method_missing(sym, *args, &block)
  if self.has_key? sym.to_s or self.has_key? sym
    self[sym]
  elsif sym.to_s.ends_with? '='
    key= sym.to_s.chop
    self[key]= args[0]
  else
    # super sym, *args, &block # ???
    nil
  end
end

Class Method Details

.from(hash = {}, recurse = true) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gumdrop/util/hash_object.rb', line 65

def self.from(hash={}, recurse=true)
  h= new
  if recurse
    hash.each do |key, value|
      if value.is_a? Hash
        hash[key]= HashObject.from value
      end
    end
  end
  h.merge!(hash)
  h
end

Instance Method Details

#[](key) ⇒ Object

All keys are symbols, internally



6
7
8
# File 'lib/gumdrop/util/hash_object.rb', line 6

def [](key)
  super(key.to_sym)
end

#[]=(key, value) ⇒ Object



9
10
11
12
13
14
# File 'lib/gumdrop/util/hash_object.rb', line 9

def []=(key, value)
  if value.is_a? Hash
    value= HashObject.from value, true
  end
  super(key.to_sym, value)
end

#get(key) ⇒ Object



28
29
30
# File 'lib/gumdrop/util/hash_object.rb', line 28

def get(key)
  self[key]
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/gumdrop/util/hash_object.rb', line 41

def has_key?(key)
  super key.to_sym
end

#merge(other_hash = nil, &block) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/gumdrop/util/hash_object.rb', line 49

def merge(other_hash=nil, &block)
  unless other_hash.nil?
    super(other_hash.to_symbolized_hash, &block)
  else
    super(other_hash, &block)
  end
end

#merge!(other_hash = nil, &block) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/gumdrop/util/hash_object.rb', line 57

def merge!(other_hash=nil, &block)
  unless other_hash.nil?
    super(other_hash.to_symbolized_hash, &block)
  else
    super(other_hash, &block)
  end
end

#set(key, value = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/gumdrop/util/hash_object.rb', line 31

def set(key,value=nil)
  if key.is_a? Hash
    key.each do |k,v|
      self[k]= v
    end
  else
    self[key]= value
  end
end

#store(key, value) ⇒ Object



45
46
47
# File 'lib/gumdrop/util/hash_object.rb', line 45

def store(key,value)
  super(key.to_sym, value)
end