Class: Sinclair::MethodDefinition::BlockDefinition Abstract Private

Inherits:
Sinclair::MethodDefinition show all
Defined in:
lib/sinclair/method_definition/block_definition.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class is abstract.

Define a method from block

Author:

  • darthjee

Constant Summary

Constants inherited from Sinclair::MethodDefinition

DEFAULT_OPTIONS

Instance Attribute Summary

Attributes included from OptionsParser

#options

Instance Method Summary collapse

Methods included from OptionsParser

#options_object

Constructor Details

#initialize(name, **options, &block) ⇒ BlockDefinition

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of BlockDefinition.

Parameters:

  • name (String, Symbol)

    name of the method

  • block (Proc)

    block with code to be added as method

  • options (Hash)

    Options of construction

Options Hash (**options):

  • cached (Boolean)

    Flag telling to create a method with cache



15
16
17
18
# File 'lib/sinclair/method_definition/block_definition.rb', line 15

def initialize(name, **options, &block)
  @block = block
  super(name, **options)
end

Instance Method Details

#build(klass) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds the instance method to given klass

Examples:

Using instance block method with cached options

class MyModel
end

instance = MyModel.new

method_definition = Sinclair::MethodDefinition::InstanceBlockDefinition.new(
  :sequence, cached: true
) do
  @x = @x.to_i ** 2 + 1
end

method_definition.build(MyModel) # adds instance_method :sequence to
                                 # MyModel instances

instance.instance_variable_get(:@sequence) # returns nil
instance.instance_variable_get(:@x)        # returns nil

instance.sequence               # returns 1
instance.sequence               # returns 1 (cached value)

instance.instance_variable_get(:@sequence) # returns 1
instance.instance_variable_get(:@x)        # returns 1

Using class block method without options

class MyModel
end

method_definition = Sinclair::MethodDefinition::ClassBlockDefinition.new(:sequence) do
  @x = @x.to_i ** 2 + 1
end

method_definition.build(MyModel)    # adds instance_method :sequence to
                                    # MyModel instances

MyModel.instance_variable_get(:@x) # returns nil

MyModel.sequence                   # returns 1
MyModel.sequence                   # returns 2
MyModel.sequence                   # returns 5

MyModel.instance_variable_get(:@x) # returns 5

Parameters:

  • klass (Class)

    class which will receive the new method

Returns:

  • (Symbol)

    name of the created method

See Also:



70
71
72
# File 'lib/sinclair/method_definition/block_definition.rb', line 70

def build(klass)
  klass.send(method_definer, name, method_block)
end