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) ⇒ Output

Returns a new instance of Output.



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

def initialize(filer)
  @filer = filer
end

Instance Attribute Details

#filerObject (readonly)

Returns the value of attribute filer.



5
6
7
# File 'lib/Gemfiler/output.rb', line 5

def filer
  @filer
end

Instance Method Details

#contentObject



17
18
19
20
# File 'lib/Gemfiler/output.rb', line 17

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”



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/Gemfiler/output.rb', line 76

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

  space_between = longest_gem_name(groups) - gem_name.length

  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 << ":#{key} => #{type_value(value)}"
      end

      options
    end.join(', ')
  end

  line.join(', ')
end

#gemspecObject



29
30
31
# File 'lib/Gemfiler/output.rb', line 29

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

#groupsObject



56
57
58
# File 'lib/Gemfiler/output.rb', line 56

def groups
  filer.groups
end

#longest_gem_name(group = nil) ⇒ Object



51
52
53
54
# File 'lib/Gemfiler/output.rb', line 51

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

#rubyObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/Gemfiler/output.rb', line 33

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

  parts.join(', ')
end

#sourcesObject



22
23
24
25
26
27
# File 'lib/Gemfiler/output.rb', line 22

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

#spacerObject



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

def spacer
  '  '
end

#type_value(value) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/Gemfiler/output.rb', line 64

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



44
45
46
47
48
49
# File 'lib/Gemfiler/output.rb', line 44

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

#write(gemfile) ⇒ Object



11
12
13
14
15
# File 'lib/Gemfiler/output.rb', line 11

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