Class: Sinclair::MethodDefinition Private

Inherits:
Object
  • Object
show all
Includes:
OptionsParser
Defined in:
lib/sinclair/method_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.

Definition of the code or block to be aded as method

Author:

  • darthjee

Constant Summary collapse

DEFAULT_OPTIONS =

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

Default options of initialization

{
  cached: false
}.freeze

Instance Attribute Summary

Attributes included from OptionsParser

#options

Instance Method Summary collapse

Methods included from OptionsParser

#options_object

Constructor Details

#initialize(name, code) ⇒ MethodDefinition #initialize(name, &block) ⇒ MethodDefinition

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 MethodDefinition

Overloads:

Parameters:

  • name (String, Symbol) —

    name of the method

  • code (String) (defaults to: nil) —

    code to be evaluated as 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



31
32
33
34
35
36
# File 'lib/sinclair/method_definition.rb', line 31

def initialize(name, code = nil, **options, &block)
  @name =    name
  @code =    code
  @options = DEFAULT_OPTIONS.merge(options)
  @block =   block
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 method to given klass

Examples:

Using string method with no options

class MyModel
end

instance = MyModel.new

method_definition = Sinclair::MethodDefinition.new(
  :sequence, '@x = @x.to_i ** 2 + 1'
)

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

instance.instance_variable_get(:@x)        # returns nil

instance.sequence               # returns 1
instance.sequence               # returns 2
instance.sequence               # returns 5

instance.instance_variable_get(:@x)        # returns 5

Using string method with no options

class MyModel
end

instance = MyModel.new

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

method_definition.build(klass)  # 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

Parameters:

  • klass (Class) —

    class which will receive the new method

Returns:

  • (Symbol) —

    name of the created method



86
87
88
89
90
91
92
# File 'lib/sinclair/method_definition.rb', line 86

def build(klass)
  if code.is_a?(String)
    build_code_method(klass)
  else
    build_block_method(klass)
  end
end