Sha256: 902cb6a839dfd6621f10b3c28b0a3dcc83d1afe8f6d79804a9221291446ac7ef

Contents?: true

Size: 1.77 KB

Versions: 15

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

class Sinclair
  class MethodDefinition
    # @api private
    # @author darthjee
    #
    # Define an instance method from string
    class StringDefinition < MethodDefinition
      build_with MethodBuilder::StringMethodBuilder

      # @param name    [String,Symbol] name of the method
      # @param code    [String] code to be evaluated as method
      # @param options [Hash] Options of construction
      # @option options cached [Boolean] Flag telling to create
      #   a method with cache
      def initialize(name, code = nil, **options)
        @code = code
        super(name, **options)
      end

      default_value :block?, false
      default_value :string?, true

      # string with the code to be defined
      #
      # @return [String]
      def code_definition
        <<-CODE
          def #{name}#{parameters_string}
            #{code_line}
          end
        CODE
      end

      private

      # @private
      # String for parameters
      #
      # @return [String]
      def parameters_string
        ParameterBuilder.from(
          options_object.parameters, options_object.named_parameters
        )
      end

      # @private
      # codeline to be run inside the code
      #
      # @return [String]
      def code_line
        cached? ? code_with_cache : code
      end

      # @method code
      # @private
      #
      # Code to be evaluated as method
      #
      # @return [String]
      attr_reader :code

      # @private
      #
      # Returns string code when {#cached?}
      #
      # @return [String]
      def code_with_cache
        case cached
        when :full
          "defined?(@#{name}) ? @#{name} : (@#{name} = #{code})"
        else
          "@#{name} ||= #{code}"
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sinclair-2.1.1 lib/sinclair/method_definition/string_definition.rb
sinclair-2.1.0 lib/sinclair/method_definition/string_definition.rb
sinclair-2.0.1 lib/sinclair/method_definition/string_definition.rb
sinclair-2.0.0 lib/sinclair/method_definition/string_definition.rb
sinclair-1.16.3 lib/sinclair/method_definition/string_definition.rb
sinclair-1.16.2 lib/sinclair/method_definition/string_definition.rb
sinclair-1.16.1 lib/sinclair/method_definition/string_definition.rb
sinclair-1.16.0 lib/sinclair/method_definition/string_definition.rb
sinclair-1.15.0 lib/sinclair/method_definition/string_definition.rb
sinclair-1.14.2 lib/sinclair/method_definition/string_definition.rb
sinclair-1.14.1 lib/sinclair/method_definition/string_definition.rb
sinclair-1.14.0 lib/sinclair/method_definition/string_definition.rb
sinclair-1.13.0 lib/sinclair/method_definition/string_definition.rb
sinclair-1.12.1 lib/sinclair/method_definition/string_definition.rb
sinclair-1.12.0 lib/sinclair/method_definition/string_definition.rb