Class: Sinclair::MethodDefinition::StringDefinition Abstract Private

Inherits:
Sinclair::MethodDefinition show all
Defined in:
lib/sinclair/method_definition/string_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 an instance method from string

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, code = nil, **options) ⇒ StringDefinition

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 StringDefinition.

Parameters:

  • name (String, Symbol)

    name of the method

  • code (String) (defaults to: nil)

    code to be evaluated as method

  • options (Hash)

    Options of construction

Options Hash (**options):

  • cached (Boolean)

    Flag telling to create a method with cache



17
18
19
20
# File 'lib/sinclair/method_definition/string_definition.rb', line 17

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

Examples:

Using instance string method with no options

class MyModel
end

instance = MyModel.new

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

method_definition.build(MyModel)    # 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 class string method with cache options

class MyModel
end

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

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

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

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

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

Parameters:

  • klass (Class)

    class which will receive the new method

Returns:

  • (Symbol)

    name of the created method

See Also:



72
73
74
# File 'lib/sinclair/method_definition/string_definition.rb', line 72

def build(klass)
  klass.module_eval(code_definition, __FILE__, __LINE__ + 1)
end