lib/grub/gem_line.rb in grub-0.0.1 vs lib/grub/gem_line.rb in grub-0.0.2
- old
+ new
@@ -1,35 +1,64 @@
module Grub
class GemLine
- attr_accessor :original_line, :location, :prev_line_comment, :name, :spec
+ attr_accessor :name, :original_line, :location, :prev_line_comment, :spec, :options
- def initialize(name:, original_line:, location:, prev_line_comment: nil)
+ def initialize(name:, original_line: nil, location: nil, prev_line_comment: nil, options: {})
@name = name
@original_line = original_line
@location = location
@prev_line_comment = prev_line_comment
+ @options = options
end
def comment
- leading_spaces_count = original_line.length - original_line.lstrip.length
leading_spaces = original_line[0..leading_spaces_count - 1] if leading_spaces_count > 0
- comment = "# #{description}"
- comment << " (#{website})" unless website.nil? || website.empty?
- comment << "\n"
- "#{leading_spaces}#{comment}"
+ comment = "#{leading_spaces}# #{info}"
end
+ def info
+ output = if options[:website_only]
+ website
+ elsif options[:description_only]
+ description
+ else
+ description_and_website
+ end
+ output << "\n"
+ end
+
def should_insert?
- prev_line_comment.nil? || !prev_line_comment.include?(comment)
+ !info.empty? && !already_added_comment && !existing_comment_option
end
+ private
+
+ def already_added_comment
+ prev_line_comment && prev_line_comment.include?(comment)
+ end
+
+ # if there exists a prev_line_comment and the user has specified new_comments_only
+ def existing_comment_option
+ prev_line_comment && options[:new_comments_only]
+ end
+
+ def leading_spaces_count
+ original_line.length - original_line.lstrip.length
+ end
+
def description
- spec.summary if spec
+ "#{spec.summary}" if spec
end
def website
- spec.homepage if spec
+ "#{spec.homepage.to_s}" if spec
+ end
+
+ def description_and_website
+ output = "#{description}"
+ output << " (#{website})" unless website.empty?
+ output
end
end
end