Class: AnnotateGem::Gemfile
- Inherits:
-
Object
- Object
- AnnotateGem::Gemfile
- Defined in:
- lib/annotate_gem/gemfile.rb
Constant Summary collapse
- GEM_LINE_REGEX =
/\A\s*gem[\s(]+["'](?<name>[^'"]*)["']/.freeze
Instance Attribute Summary collapse
-
#gem_lines ⇒ Object
Returns the value of attribute gem_lines.
-
#gemfile_path ⇒ Object
Returns the value of attribute gemfile_path.
-
#options ⇒ Object
Returns the value of attribute options.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
-
#initialize(gemfile_path, options = {}) ⇒ Gemfile
constructor
A new instance of Gemfile.
- #parse ⇒ Object
- #write_comments ⇒ Object
Constructor Details
#initialize(gemfile_path, options = {}) ⇒ Gemfile
Returns a new instance of Gemfile.
8 9 10 11 12 13 |
# File 'lib/annotate_gem/gemfile.rb', line 8 def initialize(gemfile_path, = {}) @gemfile_path = gemfile_path @source = [] @gem_lines = [] @options = end |
Instance Attribute Details
#gem_lines ⇒ Object
Returns the value of attribute gem_lines.
6 7 8 |
# File 'lib/annotate_gem/gemfile.rb', line 6 def gem_lines @gem_lines end |
#gemfile_path ⇒ Object
Returns the value of attribute gemfile_path.
6 7 8 |
# File 'lib/annotate_gem/gemfile.rb', line 6 def gemfile_path @gemfile_path end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/annotate_gem/gemfile.rb', line 6 def @options end |
#source ⇒ Object
Returns the value of attribute source.
6 7 8 |
# File 'lib/annotate_gem/gemfile.rb', line 6 def source @source end |
Instance Method Details
#parse ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/annotate_gem/gemfile.rb', line 15 def parse self.source = File.readlines(gemfile_path, :encoding => 'UTF-8') source.each_with_index do |line, i| match = GEM_LINE_REGEX.match(line) if match prev_line = source[i - 1] if i > 0 prev_line_comment = prev_line if is_line_a_comment?(prev_line) self.gem_lines << GemLine.new( name: match[:name], original_line: line, location: i, prev_line_comment: prev_line_comment, options: ) end end end |
#write_comments ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/annotate_gem/gemfile.rb', line 33 def write_comments gem_lines.reverse.each do |gem_line| next unless gem_line.should_insert? if [:inline] source[gem_line.location] = gem_line.inline_comment else source.insert(gem_line.location, gem_line.comment) end end File.write(gemfile_path, source.join) end |