Class: OpenHash::BlackHole

Inherits:
BasicObject
Defined in:
lib/open_hash.rb

Overview

BlackHole Class

Constant Summary collapse

NIL_METHODS =
::NilClass.instance_methods(false) | %i[== != ! equal? is_a? kind_of? instance_of?]

Instance Method Summary collapse

Constructor Details

#initialize(ohash, *args) ⇒ BlackHole

Returns a new instance of BlackHole.



45
46
47
48
# File 'lib/open_hash.rb', line 45

def initialize(ohash, *args)
  @ohash = ohash
  @chain_methods = args
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

:nodoc:



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

def method_missing(name, *args) # :nodoc:
  case name
  when /([^=]+)=\z/
    last_ohash = @ohash

    @chain_methods.each do |method_name|
      last_ohash = if last_ohash.respond_to?(method_name)
        last_ohash[method_name]
      else
        last_ohash[method_name] = ::OpenHash.new
      end
    end

    last_ohash[$1] = args[0]
  when /\A[^a-z_A-Z]+/
    nil.send(name, *args)
  else
    self << name
  end
end

Instance Method Details

#<<(method_name) ⇒ Object

Append a method name to chain methods



57
58
59
60
# File 'lib/open_hash.rb', line 57

def <<(method_name)
  @chain_methods << method_name
  self
end