Sha256: 56f54e67df577400abb5d95eea872f8d6d19cc1f2a02f1af4f1df929b1b0a053

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

class Sinclair
  # @api private
  # @author darthjee
  #
  # Definition of the code or block to be aded as method
  class MethodDefinition
    # Returns a new instance of MethodDefinition
    #
    # @overload initialize(name, code)
    # @overload initialize(name, &block)
    #
    # @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
    #
    # @example
    #   Sinclair::Method.new(:name, '@name')
    #
    # @example
    #   Sinclair::Method.new(:name) { @name }
    def initialize(name, code = nil, &block)
      @name = name
      @code = code
      @block = block
    end

    # Adds the method to given klass
    #
    # @param klass [Class] class which will receive the new method
    #
    # @return [Symbol] name of the created method
    def build(klass)
      if code.is_a?(String)
        build_code_method(klass)
      else
        build_block_method(klass)
      end
    end

    private

    # @private
    attr_reader :name, :code, :block

    # @private
    #
    # Add method from block
    #
    # @return [Symbol] name of the created method
    def build_block_method(klass)
      klass.send(:define_method, name, block)
    end

    # @private
    #
    # Add method from String code
    #
    # @return [Symbol] name of the created method
    def build_code_method(klass)
      klass.module_eval(code_definition, __FILE__, __LINE__ + 1)
    end

    # @private
    #
    # Builds full code of method
    #
    # @return [String]
    def code_definition
      <<-CODE
      def #{name}
        #{code}
      end
      CODE
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinclair-1.1.3 lib/sinclair/method_definition.rb