Class: Sinclair::MethodDefinition Private

Inherits:
Object
  • Object
show all
Includes:
OptionsParser
Defined in:
lib/sinclair/method_definition.rb,
lib/sinclair/method_definition/block_definition.rb,
lib/sinclair/method_definition/string_definition.rb,
lib/sinclair/method_definition/class_block_definition.rb,
lib/sinclair/method_definition/class_method_definition.rb,
lib/sinclair/method_definition/class_string_definition.rb,
lib/sinclair/method_definition/instance_block_definition.rb,
lib/sinclair/method_definition/instance_method_definition.rb,
lib/sinclair/method_definition/instance_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.

Definition of the code or block to be aded as method

Author:

  • darthjee

Direct Known Subclasses

BlockDefinition, StringDefinition

Defined Under Namespace

Modules: ClassMethodDefinition, InstanceMethodDefinition Classes: BlockDefinition, ClassBlockDefinition, ClassStringDefinition, InstanceBlockDefinition, InstanceStringDefinition, StringDefinition

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, **options) ⇒ 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.

Parameters:

  • name (String, Symbol)

    name of the method

  • options (Hash)

    Options of construction

Options Hash (**options):

  • cached (Boolean)

    Flag telling to create a method with cache



29
30
31
32
# File 'lib/sinclair/method_definition.rb', line 29

def initialize(name, **options)
  @name =    name
  @options = DEFAULT_OPTIONS.merge(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.

This method is abstract.

Adds the method to given klass

This should be implemented on child classes

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

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



46
47
48
49
# File 'lib/sinclair/method_definition.rb', line 46

def build(_klass)
  raise 'Build is implemented in subclasses. ' \
    "Use #{self.class}.from to initialize a proper object"
end