Class: Diligent::List

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/diligent/list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeList

Returns a new instance of List.



29
30
31
32
33
# File 'lib/diligent/list.rb', line 29

def initialize
  # TODO Optionally point at a different path?
  @project_name = File.split(FileUtils.getwd).last
  @specs = Bundler.load.specs
end

Instance Attribute Details

#project_nameString

Returns the name of the project we're reading, currently assumed to be the directory name.

Returns:

  • (String)

    the name of the project we're reading, currently assumed to be the directory name.



12
13
14
# File 'lib/diligent/list.rb', line 12

def project_name
  @project_name
end

#specsBundler::SpecSet

Returns Dependency info as generated by Bundler.load.specs.

Returns:

  • (Bundler::SpecSet)

    Dependency info as generated by Bundler.load.specs



17
18
19
# File 'lib/diligent/list.rb', line 17

def specs
  @specs
end

Class Method Details

.loadHash

Initialize and return gem info in a single step. May be deprecated as it seems to be of limited use.

Returns:

  • (Hash)

    a hash containing dependency info



24
25
26
# File 'lib/diligent/list.rb', line 24

def load
  self.new.as_hash
end

Instance Method Details

#as_csv(filename = nil) ⇒ String

Output dependency info as CSV.

Parameters:

  • filename (String) (defaults to: nil)

    optional name of output file

Returns:

  • (String)

    a CSV string containing dependency info, unless a filename is given



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/diligent/list.rb', line 64

def as_csv(filename = nil)
  as_array = as_hash.inject([]) do |arr, gem_info|
    row = []
    row << gem_info.first
    gem_info.last.inject(row) do |r, i|
      r << i.last
    end
    arr << row
  end

  csv = CSV.generate do |csv|
    csv << %w{ Gem Version Author Summary Description License Homepage }
    as_array.each { |row| csv << row }
  end

  write_to_file filename, csv
end

#as_hashHash

Output dependency info as a hash.

Returns:

  • (Hash)

    a hash containing dependency info



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/diligent/list.rb', line 37

def as_hash
  @specs.inject({}) do |hash, spec|
    hash[spec.name] = {
      'version'      => spec.version.to_s,
      'author'       => spec.author,
      'summary'      => spec.summary,
      'description'  => spec.description,
      'license'      => spec.license,
      'homepage'     => spec.homepage
    }

    hash
  end
end

#as_html(filename = nil) ⇒ String

Output dependency info as HTML.

Parameters:

  • filename (String) (defaults to: nil)

    optional name of output file

Returns:

  • (String)

    a CSV string containing dependency info, unless a filename is given



85
86
87
88
89
90
91
# File 'lib/diligent/list.rb', line 85

def as_html(filename = nil)
  list = as_hash
  template_path = File.join(File.dirname(__FILE__), '../../templates/list.html.erb')
  html = ERB.new(File.read(template_path)).result(binding)

  write_to_file filename, html
end

#as_json(filename = nil) ⇒ String

Output dependency info as JSON. A JSON string is returned by the method even if a filename is specified.

Parameters:

  • filename (String) (defaults to: nil)

    optional name of output file

Returns:

  • (String)

    a JSON string containing dependency info



56
57
58
59
# File 'lib/diligent/list.rb', line 56

def as_json(filename = nil)
  json = as_hash.to_json
  write_to_file filename, json
end