Class: ConvenientService::Examples::Rails::Gemfile::Services::ParseContent

Inherits:
Object
  • Object
show all
Includes:
RailsService::Config
Defined in:
lib/convenient_service/examples/rails/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

Methods included from Config

included

Instance Method Details

#parse_contentObject

[View source]

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
88
# File 'lib/convenient_service/examples/rails/gemfile/services/parse_content.rb', line 59

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

[View source]

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

def result
  success(parsed_content: parse_content)
end