Skip to content

Commit

Permalink
Push formatting output through formatter class
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimartino committed Jun 3, 2015
1 parent f0535e5 commit c7a85ed
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 126 deletions.
104 changes: 3 additions & 101 deletions lib/gemtastic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,108 +5,10 @@ module Gemtastic
require 'gemtastic/gemfile'
require 'gemtastic/source'

require 'gemtastic/formatters/annotation_formatter.rb'
require 'gemtastic/formatters/colorized_annotation_formatter.rb'

def self.ate file
Gemfile.new(File.read(file))
end
end

# class
# def initialize
# process("Gemfile.test")
# end

# def process(gemfile)
# # Assign gemfile path relative to location of execution
# local_gemfile = File.dirname(__FILE__) + "/../Gemfile.test"

# # Exit if file doesn't exist
# abort("Could not find the file, exiting") unless File.file?(local_gemfile)

# # Create new empty array, we'll store the list of gems here
# gemlist ||= []

# # Load the gems in the gemfile, stash each gem and it's version
# gemfile = Gemnasium::Parser::Gemfile.new(File.open(local_gemfile).read)
# gemfile.dependencies.each_with_index.map {|x, i| gemlist << [x.name, x.requirement.as_list, x.groups]}

# # Sort by groups and group by uniques
# gemlist = gemlist.sort_by {|gem| gem[2].to_s }.group_by {|gem| gem[2].uniq}

# # TODO: Fetch gem info from API server

# # iterate through server and fill in gem info
# temp_file = Tempfile.new(File.dirname(__FILE__) + "Gemfile.test.tmp")
# File.open(local_gemfile, 'r') do |f|
# f.each_line do |line|
# line = line.gsub("\t", ' ')
# temp_file.puts line.include?("gem") ? gem_comment(line) : line
# end
# end
# temp_file.close
# FileUtils.mv(temp_file.path, local_gemfile)

# end

# def gem_comment(line)
# comment = "\n"

# # Add prerequisite tabs
# line[/\A */].size.times { comment += " " }

# # Add some arbitrary comment
# comment += "# This is the best comment evar."
# comment += "\n"
# return comment+line
# end

# def process_old(gemfile)
# # Assign gemfile path relative to location of execution
# local_gemfile = File.dirname(__FILE__) + "/../Gemfile.test"

# # Exit if file doesn't exist
# abort("Could not find the file, exiting") unless File.file?(local_gemfile)

# # Create new empty array, we'll store the list of gems here
# gemlist ||= []

# # Load the gems in the gemfile, stash each gem and it's version
# gemfile = Gemnasium::Parser::Gemfile.new(File.open(local_gemfile).read)
# gemfile.dependencies.each_with_index.map {|x, i| gemlist << [x.name, x.requirement.as_list, x.groups]}

# # Sort by groups and group by uniques
# gemlist = gemlist.sort_by {|gem| gem[2].to_s }.group_by {|gem| gem[2].uniq}

# # TODO: Fetch gem info from API server

# # Find the source(s)
# source = ""
# File.open(local_gemfile, 'r') do |f|
# f.each_line do |line|
# if line.include?("source")
# source += line
# end
# end
# end

# temp_file = Tempfile.new(File.dirname(__FILE__) + "Gemfile.test.tmp")
# temp_file.puts source

# gemlist.each do |group, value|
# unless group.include?(:default)
# group_names = group.map{|x| ":"+x.to_s}.join(',')
# puts "group #{group_names} do\n"
# end
# end

# # temp_file.close
# # FileUtils.mv(temp_file.path, local_gemfile)

# # File.open(local_gemfile, "w+") do |file|
# # file.each_line do |line|
# # file.puts "#comment here\n" + line
# # end
# # end

# end
# end
# end
28 changes: 3 additions & 25 deletions lib/gemtastic/annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,14 @@ class Annotation

API = 'https://rubygems.org/api/v1/gems/'

def initialize gem, indent=nil, annotations=nil
def initialize gem, indent=nil, formatter=Gemtastic::AnnotationFormatter
@gem = gem
@indent = indent
@annotations = annotations || {
'homepage_uri' => 'Homepage',
'source_code_uri' => 'Source',
'documentation_uri' => 'Documentation'
}
@formatter = formatter.new(self)
end

def to_s
<<-EOL.chomp
#{indent}#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~
#{indent}#~#
#{indent}#~# #{get['name']}:
#{indent}#~# #{"-" * get['name'].length}
#{indent}#~#
#{indent}#~# #{get['info'].gsub(/\n/, "\n#{indent}#~# ")}
#{indent}#~#
#{annotate}
#{indent}#~#
#{indent}#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~
#{indent}#~#
EOL
@formatter.to_s
end

def get
Expand All @@ -49,11 +33,5 @@ def gem_api_url
def self.source_string? str
/\A#~#/.match str
end

def annotate
annotations.each_pair.map { |uri, header|
"#{indent}#~# #{'%-14s' % (header<<':')} #{get[uri]}"
}.join("\n")
end
end
end
68 changes: 68 additions & 0 deletions lib/gemtastic/formatters/annotation_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module Gemtastic
class AnnotationFormatter
private
attr_reader :annotation, :annotations

public
def initialize annotation, annotations=nil
@annotation = annotation
@annotations = annotations || {
'homepage_uri' => 'Homepage',
'source_code_uri' => 'Source',
'documentation_uri' => 'Documentation'
}
end

def to_s
to_s_lines.map { |line| "#{indent}#{line}" }.join("\n")
end

private

def to_s_lines
[
header,
name,
underline,
spacer,
description,
spacer,
annotate,
spacer,
header,
spacer
].flatten
end

def spacer
"#~#"
end

def header
"#~" * 40
end

def description
"#{spacer} #{annotation.get['info'].gsub(/\n/, "\n#{indent}#{spacer} ")}"
end

def name
"#{spacer} #{annotation.get['name']}"
end

def underline
"#{spacer} " + ('-' * annotation.get['name'].length)
end

def annotate
@annotations.each_pair.map { |uri, header|
"#{spacer} #{'%-14s' % (header<<':')} #{annotation.get[uri]}"
}
end
def indent
@annotation.indent
end
end
end
4 changes: 4 additions & 0 deletions lib/gemtastic/formatters/colorized_annotation_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Gemtastic
class ColorizedAnnotationFormatter < AnnotationFormatter
end
end

0 comments on commit c7a85ed

Please sign in to comment.