Module: Sinclair::MethodDefinition::ClassMethodDefinition Private

Defined in:
lib/sinclair/method_definition/class_method_definition.rb

Overview

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

Module responsible for building class method definitions

Author:

  • darthjee

Class Method Summary collapse

Class Method Details

.from(name, code = nil, **options, &block) ⇒ Object

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 an instance method definition

When block is given returns an instance of InstanceBlockDefinition, and when not, returns InstanceStringDefinition

Examples:

With cache

klass = Class.new

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

method_definition.build(klass)

klass.sequence # returns 1
klass.sequence # returns 1

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

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

Returns:

  • MethodDefinition



41
42
43
44
45
46
47
# File 'lib/sinclair/method_definition/class_method_definition.rb', line 41

def self.from(name, code = nil, **options, &block)
  if block
    ClassBlockDefinition.new(name, **options, &block)
  else
    ClassStringDefinition.new(name, code, **options)
  end
end