Sha256: 19e1e62c9ace966f6f3c4bf809164a016f858432de6386ab9d1bf801858d68b5

Contents?: true

Size: 772 Bytes

Versions: 2

Compression:

Stored size: 772 Bytes

Contents

# frozen_string_literal: true

module Marameters
  module Sources
    # Extracts the literal source of a Proc's body.
    class Extractor
      PATTERN = /
        proc          # Proc statement.
        \s*           # Optional space.
        \{            # Block open.
        (?<body>.*?)  # Source code body.
        \}            # Block close.
      /x

      def initialize pattern: PATTERN, reader: Reader.new
        @pattern = pattern
        @reader = reader
        @fallback = "nil"
        freeze
      end

      def call function
        reader.call(function).then do |line|
          line.match?(pattern) ? line.match(pattern)[:body].strip : fallback
        end
      end

      private

      attr_reader :pattern, :reader, :fallback
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
marameters-4.0.1 lib/marameters/sources/extractor.rb
marameters-4.0.0 lib/marameters/sources/extractor.rb