Class: Dry::System::MagicCommentsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/system/magic_comments_parser.rb

Constant Summary collapse

VALID_LINE_RE =
/^(#.*)?$/
COMMENT_RE =
/^#\s+(?<name>[A-Za-z]{1}[A-Za-z0-9_]+):\s+(?<value>.+?)$/
COERCIONS =
{
  "true" => true,
  "false" => false
}.freeze

Class Method Summary collapse

Class Method Details

.call(file_name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dry/system/magic_comments_parser.rb', line 14

def self.call(file_name)
  {}.tap do |options|
    File.foreach(file_name) do |line|
      break unless line =~ VALID_LINE_RE

      if (comment = line.match(COMMENT_RE))
        options[comment[:name].to_sym] = coerce(comment[:value])
      end
    end
  end
end

.coerce(value) ⇒ Object



26
27
28
# File 'lib/dry/system/magic_comments_parser.rb', line 26

def self.coerce(value)
  COERCIONS.fetch(value) { value }
end