Class: Tutor::Attributes::Method
- Inherits:
-
Object
- Object
- Tutor::Attributes::Method
- Defined in:
- lib/tutor/attributes/method.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#name ⇒ Object
Returns the value of attribute name.
-
#post_execute ⇒ Object
Returns the value of attribute post_execute.
-
#pre_execute ⇒ Object
Returns the value of attribute pre_execute.
Instance Method Summary collapse
- #define_on(klass, override: false) ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Method
constructor
A new instance of Method.
- #method_block ⇒ Object
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, = {}, &block) self.name = name.to_sym self.body = block ? Tutor::Attributes::Block.new(&block) : [:body] self.pre_execute = [:pre_execute] self.post_execute = [:post_execute] end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
3 4 5 |
# File 'lib/tutor/attributes/method.rb', line 3 def body @body end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/tutor/attributes/method.rb', line 3 def name @name end |
#post_execute ⇒ Object
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_execute ⇒ Object
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_block ⇒ Object
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 |