Class: Marameters::Sources::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/marameters/sources/extractor.rb

Overview

Extracts the literal source of a Proc’s body.

Constant Summary collapse

PATTERN =
/
  proc          # Proc statement.
  \s*           # Optional space.
  \{            # Block open.
  (?<body>.*?)  # Source code body.
  \}            # Block close.
/x

Instance Method Summary collapse

Constructor Details

#initialize(pattern: PATTERN, reader: Reader.new) ⇒ Extractor

Returns a new instance of Extractor.



15
16
17
18
19
20
21
22
23
# File 'lib/marameters/sources/extractor.rb', line 15

def initialize pattern: PATTERN, reader: Reader.new
  warn "`#{self.class}` is deprecated, use `Sourcers::Function` instead.",
       category: :deprecated

  @pattern = pattern
  @reader = reader
  @fallback = "nil"
  freeze
end

Instance Method Details

#call(function) ⇒ Object



25
26
27
28
29
# File 'lib/marameters/sources/extractor.rb', line 25

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