Class: Hash

Inherits:
Object show all
Defined in:
lib/qooxview/additions.rb

Overview

Converts all keys of a hash to syms recursively

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/qooxview/additions.rb', line 53

def method_missing(s, *args)
  dputs(4) { "Method is missing: #{s.inspect}" }

  case s.to_s
    when 'to_ary'
      super(s, args)
    when /^_.*[^=]$/
      key = s.to_s.sub(/^_{1,2}/, '').to_sym
      dputs(4) { "Searching for #{s.inspect} -> #{key}" }
      self.class.class_eval <<-RUBY
        def _#{key}(ret = nil)
        self.has_key? :#{key} and return self[:#{key}]
          self.has_key? '#{key}' and return self['#{key}']
          ret ? (self[:#{key}] = {}) : nil
        end

        def __#{key}
          _#{key}(true)
        end
      RUBY
      self.send(s)
    when /^_.*=$/
      key = /^_{1,2}(.*)=$/.match(s.to_s)[1].to_sym
      dputs(4) { "Setting #{s.inspect} -> #{key} to #{args.inspect}" }
      self.has_key? key and return self[key] = args[0]
      self.has_key? key.to_s and return self[key.to_s] = args[0]
      return self[key] = args[0]
    else
      super(s, args)
  end
end

Instance Method Details

#to_symObject



41
42
43
44
45
46
47
# File 'lib/qooxview/additions.rb', line 41

def to_sym
  ret = {}
  each { |k, v|
    ret[k.to_sym] = v.class == Hash ? v.to_sym : v
  }
  ret
end

#to_sym!Object



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

def to_sym!
  self.replace(to_sym())
end