Class: Multimethod::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/multimethod/method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(impl_name, *args) ⇒ Method

Returns a new instance of Method.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/multimethod/method.rb', line 9

def initialize(impl_name, *args)
  if args.size == 1
    @signature = args[0]
  else
    mod, name, params = *args
    raise NameError, "multimethod method name not specified" unless name && name.to_s.size > 0
    raise NameError, "multimethod method impl_name not specified" unless impl_name && impl_name.to_s.size > 0
    
    @signature = Signature.new(:mod => mod, :name => name, :parameter => params)
  end

  impl_name = Multimethod.normalize_name(impl_name)

  @impl_name = impl_name
end

Instance Attribute Details

#impl_nameObject

Returns the value of attribute impl_name.



5
6
7
# File 'lib/multimethod/method.rb', line 5

def impl_name
  @impl_name
end

#multimethodObject

Returns the value of attribute multimethod.



7
8
9
# File 'lib/multimethod/method.rb', line 7

def multimethod
  @multimethod
end

#signatureObject

Returns the value of attribute signature.



4
5
6
# File 'lib/multimethod/method.rb', line 4

def signature
  @signature
end

Instance Method Details

#<=>(x) ⇒ Object

For score sort



39
40
41
# File 'lib/multimethod/method.rb', line 39

def <=>(x)
  0
end

#inspectObject



84
85
86
# File 'lib/multimethod/method.rb', line 84

def inspect
  to_s
end

#matches_signature(signature) ⇒ Object



26
27
28
# File 'lib/multimethod/method.rb', line 26

def matches_signature(signature)
  @signature == signature
end

#parameterObject

Parameters



45
46
47
# File 'lib/multimethod/method.rb', line 45

def parameter
  @signature.parameter
end

#remove_implementationObject



31
32
33
34
35
# File 'lib/multimethod/method.rb', line 31

def remove_implementation
  # Remove the method implementation
  # $stderr.puts "Removing implementation for #{signature.to_s} => #{impl_name}"
  signature.mod.class_eval("remove_method #{impl_name.inspect}")
end

#score(args) ⇒ Object

Scoring this method.



51
52
53
# File 'lib/multimethod/method.rb', line 51

def score(args)
  @signature.score(args)
end

#score_cached(args) ⇒ Object



56
57
58
# File 'lib/multimethod/method.rb', line 56

def score_cached(args)
  @signature.score_cached(args)
end

#to_ruby_argObject



79
80
81
# File 'lib/multimethod/method.rb', line 79

def to_ruby_arg
  @signature.to_ruby_arg
end

#to_ruby_def(name = nil) ⇒ Object



67
68
69
70
# File 'lib/multimethod/method.rb', line 67

def to_ruby_def(name = nil)
  name ||= @impl_name
  @signature.to_ruby_def(name)
end

#to_ruby_signature(name = nil) ⇒ Object



73
74
75
76
# File 'lib/multimethod/method.rb', line 73

def to_ruby_signature(name = nil)
  name ||= @impl_name
  @signature.to_ruby_signature(name)
end

#to_s(name = nil) ⇒ Object



61
62
63
64
# File 'lib/multimethod/method.rb', line 61

def to_s(name = nil)
  name ||= @impl_name
  @signature.to_s(name)
end