Class: Compendium::OpenHash

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/compendium/open_hash.rb

Direct Known Subclasses

Params

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/compendium/open_hash.rb', line 31

def method_missing(name, *args, &block)
  method = name.to_s

  case method
    when %r{.=$}
      super unless args.length == 1
      return self[method[0...-1]] = args.first

    when %r{.\?$}
      super unless args.empty?
      return self.key?(method[0...-1].to_sym)

    when %r{^_.}
      super unless args.empty?
      return self[method[1..-1]] if self.key?(method[1..-1].to_sym)

    else
      return self[method] if key?(method) || !respond_to?(method)
  end

  super
end

Class Method Details

.[](hash = {}) ⇒ Object



6
7
8
# File 'lib/compendium/open_hash.rb', line 6

def [](hash = {})
  new(hash)
end

Instance Method Details

#dupObject



11
12
13
# File 'lib/compendium/open_hash.rb', line 11

def dup
  self.class.new(self)
end