Class: ConvenientService::Examples::Dry::Gemfile::Services::ParseContent

Inherits:
Object
  • Object
show all
Includes:
DryService::Config
Defined in:
lib/convenient_service/examples/dry/gemfile/services/parse_content.rb

Constant Summary collapse

RUBY_REGEX =
/\A\s*ruby/
SOURCE_REGEX =
/\A\s*source/
GIT_SOURCE_REGEX =
/\A\s*git_source/
GROUP_REGEX =
/\A\s*group/
GEM_REGEX =
/\A\s*gem/
BLOCK_END_REGEX =
/\A\s*end/
ENV_SCAN_REGEX =
/:(\w+)/

Instance Method Summary collapse

Instance Method Details

#parse_contentObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/convenient_service/examples/dry/gemfile/services/parse_content.rb', line 58

def parse_content
  hash = ::Hash.new { |h, k| h[k] = [] }

  ::StringIO.open(content) do |io|
    envs = []

    until io.eof?
      line = io.readline.rstrip

      case line
      when RUBY_REGEX
        hash[:ruby] << line
      when SOURCE_REGEX
        hash[:source] << line
      when GIT_SOURCE_REGEX
        hash[:git_source] << line
      when GROUP_REGEX
        envs = line.scan(ENV_SCAN_REGEX).map(&:first).map(&:to_sym)
      when GEM_REGEX
        hash[:gems] << {envs: envs, line: line.lstrip}
      when BLOCK_END_REGEX
        envs = []
      else
        hash[:rest] << line
      end
    end
  end

  hash
end

#resultObject



54
55
56
# File 'lib/convenient_service/examples/dry/gemfile/services/parse_content.rb', line 54

def result
  success(parsed_content: parse_content)
end