Sha256: 93d23ac8b6d9445c1f93f6ad755738e4e43e7dc8c73078a4d25f55c203d73466

Contents?: true

Size: 1.28 KB

Versions: 14

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

class Sinclair
  class MethodDefinition
    # @api private
    # @author darthjee
    #
    # @abstract
    #
    # Define an instance method from string
    class StringDefinition < MethodDefinition
      # @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

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

      private

      # @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

14 entries across 14 versions & 1 rubygems

Version Path
sinclair-1.9.0 lib/sinclair/method_definition/string_definition.rb
sinclair-1.8.0 lib/sinclair/method_definition/string_definition.rb
sinclair-1.7.0 lib/sinclair/method_definition/string_definition.rb
sinclair-1.6.7 lib/sinclair/method_definition/string_definition.rb
sinclair-1.6.6 lib/sinclair/method_definition/string_definition.rb
sinclair-1.6.5 lib/sinclair/method_definition/string_definition.rb
sinclair-1.6.4 lib/sinclair/method_definition/string_definition.rb
sinclair-1.6.3 lib/sinclair/method_definition/string_definition.rb
sinclair-1.6.2 lib/sinclair/method_definition/string_definition.rb
sinclair-1.6.1 lib/sinclair/method_definition/string_definition.rb
sinclair-1.6.0 lib/sinclair/method_definition/string_definition.rb
sinclair-1.5.2 lib/sinclair/method_definition/string_definition.rb
sinclair-1.5.1 lib/sinclair/method_definition/string_definition.rb
sinclair-1.5.0 lib/sinclair/method_definition/string_definition.rb