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



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gumdrop/util/hash_object.rb', line 13

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 = {}) ⇒ Object



62
63
64
65
66
# File 'lib/gumdrop/util/hash_object.rb', line 62

def self.from(hash={})
  h= new
  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
# File 'lib/gumdrop/util/hash_object.rb', line 9

def []=(key, value)
  super(key.to_sym, value)
end

#get(key) ⇒ Object



25
26
27
# File 'lib/gumdrop/util/hash_object.rb', line 25

def get(key)
  self[key]
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/gumdrop/util/hash_object.rb', line 38

def has_key?(key)
  super key.to_sym
end

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



46
47
48
49
50
51
52
# File 'lib/gumdrop/util/hash_object.rb', line 46

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



54
55
56
57
58
59
60
# File 'lib/gumdrop/util/hash_object.rb', line 54

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



28
29
30
31
32
33
34
35
36
# File 'lib/gumdrop/util/hash_object.rb', line 28

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



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

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