Class: Tutor::Attributes::Method

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Method

Returns a new instance of Method.



9
10
11
12
13
14
# File 'lib/tutor/attributes/method.rb', line 9

def initialize(name, options = {}, &block)
  self.name = name.to_sym
  self.body = block ? Tutor::Attributes::Block.new(&block) : options[:body]
  self.pre_execute = options[:pre_execute]
  self.post_execute = options[:post_execute]
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/tutor/attributes/method.rb', line 3

def body
  @body
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/tutor/attributes/method.rb', line 3

def name
  @name
end

#post_executeObject

Returns the value of attribute post_execute.



3
4
5
# File 'lib/tutor/attributes/method.rb', line 3

def post_execute
  @post_execute
end

#pre_executeObject

Returns the value of attribute pre_execute.



3
4
5
# File 'lib/tutor/attributes/method.rb', line 3

def pre_execute
  @pre_execute
end

Instance Method Details

#define_on(klass, override: false) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/tutor/attributes/method.rb', line 16

def define_on(klass, override: false)
  if !override && klass.method_defined?(self.name)
    raise NameError.new("Attribute name conflicts with existing method!", self.name) 
  else
    klass.send(:define_method, self.name, &method_block)
  end
end

#method_blockObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/tutor/attributes/method.rb', line 24

def method_block
  method = self
  Proc.new do |*args|
    return_value = nil
    method.pre_execute.call(self, *args) unless method.pre_execute.nil?
    return_value = method.body.block.call(self, *args) unless method.body.nil?
    method.post_execute.call(self, *args) unless method.post_execute.nil?
    return_value
  end
end