Class: Gemfiler::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/gemfiler/output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filer, options = {}) ⇒ Output

Returns a new instance of Output.



9
10
11
12
# File 'lib/gemfiler/output.rb', line 9

def initialize(filer, options={})
  @filer   = filer
  @options = options
end

Instance Attribute Details

#filerObject (readonly)

Returns the value of attribute filer.



7
8
9
# File 'lib/gemfiler/output.rb', line 7

def filer
  @filer
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/gemfiler/output.rb', line 7

def options
  @options
end

Instance Method Details

#contentObject



20
21
22
23
# File 'lib/gemfiler/output.rb', line 20

def content
  erb = ERB.new(File.read(File.expand_path("../templates/gemfile.erb", __FILE__)), nil, "<>")
  erb.result(binding)
end

#gem_line(g, groups = nil) ⇒ Object

It’s a short parameter name because my syntax highlighter doesn’t like the word “gem”



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gemfiler/output.rb', line 88

def gem_line(g, groups=nil)
  gem_name = g[:name]
  line     = ["gem '#{gem_name}'"]

  space_between = @options[:nice_spaces] ? longest_gem_name(groups) - gem_name.length : 0

  if g[:version]
    line << (" " * space_between) + "'#{g[:version]}'"
  elsif (g.length - 1) > 0
    line << (" " * space_between) + g.inject([]) do |options, (key, value)|
      if key != :name
        options << hash_keyvalue(key, value)
      end

      options
    end.join(", ")
  end

  annotation = ""

  if @options[:annotate]
    puts "-- Annotating #{gem_name}"
    uri = URI.parse("https://rubygems.org/api/v1/gems/#{gem_name}.json")
    response = Net::HTTP.get_response(uri)

    if response.code == "200"
      parsed = JSON.load(response.body)
      info = parsed['info']
      info = info.gsub("\n", " ")

      annotation = " # #{info}"
    else
      puts "---- Gem #{gem_name} not a valid gem"
    end
  end

  line.join(", ") + annotation
end

#gemspecObject



32
33
34
# File 'lib/gemfiler/output.rb', line 32

def gemspec
  filer.shim.gemspec? ? "gemspec" : ""
end

#groupsObject



60
61
62
# File 'lib/gemfiler/output.rb', line 60

def groups
  filer.groups
end

#hash_keyvalue(key, value) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/gemfiler/output.rb', line 79

def hash_keyvalue(key, value)
  if @options[:ruby19_hashes]
    "#{key.to_s}: #{type_value(value)}"
  else
    "#{type_value(key)} => #{type_value(value)}"
  end
end

#longest_gem_name(group = nil) ⇒ Object



54
55
56
57
58
# File 'lib/gemfiler/output.rb', line 54

def longest_gem_name(group=nil)
  @longest ||= {}
  gems = group ? groups[group] : filer.uncategorized
  @longest[group] ||= gems.inject(0) {|max, gem| gem[:name].length > max ? gem[:name].length : max }
end

#rubyObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/gemfiler/output.rb', line 36

def ruby
  parts = []
  if version_info = filer.shim.ruby_version
    parts << "ruby '#{version_info[:version]}'"
    parts << hash_keyvalue(:engine, version_info[:engine]) if version_info[:engine]
    parts << hash_keyvalue(:engine_version, version_info[:engine_version]) if version_info[:engine_version]
  end

  parts.join(", ")
end

#sourcesObject



25
26
27
28
29
30
# File 'lib/gemfiler/output.rb', line 25

def sources
  filer.shim.sources.inject([]) do |sources, source|
    sources << "source #{type_value(source)}"
    sources
  end
end

#spacerObject



64
65
66
# File 'lib/gemfiler/output.rb', line 64

def spacer
  "  "
end

#type_value(value) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/gemfiler/output.rb', line 68

def type_value(value)
  case value
  when Symbol
    ":#{value.to_s}"
  when String
    "'#{value}'"
  when Fixnum, TrueClass, FalseClass
    value.to_s
  end
end

#uncategorized_gemsObject



47
48
49
50
51
52
# File 'lib/gemfiler/output.rb', line 47

def uncategorized_gems
  filer.uncategorized.inject([]) do |gems, gem|
    gems << self.gem_line(gem)
    gems
  end
end

#write(gemfile) ⇒ Object



14
15
16
17
18
# File 'lib/gemfiler/output.rb', line 14

def write(gemfile)
  File.open(gemfile, "w") do |file|
    file.write(content)
  end
end