Class: Surrogate::MethodDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/surrogate/method_definition.rb

Overview

A surrogate’s ‘define` keyword results in one of these

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, default_proc) ⇒ MethodDefinition

Returns a new instance of MethodDefinition.



9
10
11
# File 'lib/surrogate/method_definition.rb', line 9

def initialize(name, options, default_proc)
  self.name, self.options, self.default_proc = name, options, default_proc
end

Instance Attribute Details

#default_procObject

Returns the value of attribute default_proc.



7
8
9
# File 'lib/surrogate/method_definition.rb', line 7

def default_proc
  @default_proc
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/surrogate/method_definition.rb', line 7

def name
  @name
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/surrogate/method_definition.rb', line 7

def options
  @options
end

Instance Method Details

#[](key) ⇒ Object



17
18
19
# File 'lib/surrogate/method_definition.rb', line 17

def [](key)
  options[key]
end

#default(instance, invocation) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/surrogate/method_definition.rb', line 29

def default(instance, invocation)
  if options.has_key? :default
    options[:default]
  elsif default_proc
    default_proc_as_method_on(instance).call(*invocation.args, &invocation.block)
  else
    raise "You should never get here. Please open a issue at http://github.com/JoshCheek/surrogate"
  end
end

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/surrogate/method_definition.rb', line 13

def has?(name)
  options.has_key? name
end

#must_match!(args) ⇒ Object



25
26
27
# File 'lib/surrogate/method_definition.rb', line 25

def must_match!(args)
  default_proc && errorizer.match!(*args)
end

#to_hashObject



21
22
23
# File 'lib/surrogate/method_definition.rb', line 21

def to_hash
  options
end