Class: Multidispatch::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/multidispatch/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



6
7
8
9
# File 'lib/multidispatch/store.rb', line 6

def initialize
  @store = {}
  @locked = nil
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



4
5
6
# File 'lib/multidispatch/store.rb', line 4

def store
  @store
end

Instance Method Details

#get(klass, method_name, arity) ⇒ Object

Raises:



11
12
13
14
15
16
17
# File 'lib/multidispatch/store.rb', line 11

def get(klass, method_name, arity)
  class_methods = @store[klass.name] || {}
  methods = class_methods[method_name] || {}
  method_body = methods[-1] || methods[arity]
  raise NoMethodError unless method_body
  method_body
end

#lock!Object



32
33
34
35
# File 'lib/multidispatch/store.rb', line 32

def lock!
  @locked = true
  self
end

#locked?Boolean

Returns:



28
29
30
# File 'lib/multidispatch/store.rb', line 28

def locked?
  @locked
end

#release!Object



37
38
39
40
# File 'lib/multidispatch/store.rb', line 37

def release!
  @locked = nil
  self
end

#set(klass, method_name) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/multidispatch/store.rb', line 19

def set(klass, method_name)
  return nil if locked?

  class_methods = @store[klass.name] ||= {}
  methods = class_methods[method_name] ||= {}
  method = klass.instance_method(method_name)
  methods[method.arity] = method
end