Sha256: 1f8876b5627af24e7d804d1b3111362a22d8d87f390a2028d0aa660521047924

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

class Sinclair
  class MethodDefinition
    # @api private
    # @author darthjee
    #
    # Module responsible for building class method definitions
    module ClassMethodDefinition
      # Returns an instance method definition
      #
      # When block is given returns an instance of
      # {InstanceBlockDefinition}, and when not, returns
      # {InstanceStringDefinition}
      #
      # @param name    [String,Symbol] name of the method
      # @param code    [String] code to be evaluated as method
      # @param block   [Proc] block with code to be added as method
      # @param options [Hash] Options of construction
      # @option options cached [Boolean] Flag telling to create
      #   a method with cache
      #
      # @example 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
      #
      # @return MethodDefinition
      def self.from(name, code = nil, **options, &block)
        if block
          ClassBlockDefinition.new(name, **options, &block)
        else
          ClassStringDefinition.new(name, code, **options)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinclair-1.4.2 lib/sinclair/method_definition/class_method_definition.rb