Sha256: cdb4480e9deb54519a5de04f2ff5540d8f3eb4dcc78060e0c33c02c5ed7c0699

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

module AboutPage
  class Dependencies < AboutPage::Configuration::Node
    attr_reader :max_depth

    def initialize(max_depth=100)
      @max_depth = max_depth
    end

    def to_h
      @dependencies ||= spec_list.inject({}) { |h,data|
        spec = { :name => data[0], :version => data[1] }
        if data[2].nil?
          h[:implied] ||= []
          h[:implied] << spec
        else
          data[2].each { |g| 
            h[g] ||= [] 
            h[g] << spec
          }
        end
        h        
      }
    end

    def spec_list
      required = environment.current_dependencies.inject({}) { |h,d| h[d.name] = d.groups; h }
      list = environment.specs.sort { |a,b| a.name <=> b.name }.collect { |s| [
        s.name, 
        [s.version.to_s, s.git_version.to_s].join,
        required[s.name]
      ]}
    end

    private
    def environment
      @environment ||= Bundler.load
    end

    def groups
      @groups ||= environment.current_dependencies.group_by { |d| d.groups.first.to_s }
    end

    def specs
      @specs ||= environment.specs
    end

    def dependency_version(key)
      spec = environment.specs.find { |s| s.name == key }
      rev = spec.git_version
      rev.strip! unless rev.nil?
      location = [spec.source.options.values_at('path','uri').compact.first,rev].compact.join('@')
      [spec.version.to_s,location].compact.join(' ').strip
    end
    
    def dependency_hash(key, graph=nil, depth=0)
      graph = Bundler::Graph.new(environment,'/dev/null') if graph.nil?
      result = { :version => dependency_version(key) }
      if depth < @max_depth
        deps = Hash[graph.relations[key].collect { |dep| [dep, dependency_hash(dep, graph, depth+1)]}]
        result[:dependencies] = deps unless deps.empty?
      end
      result
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
about_page-0.2.4 lib/about_page/dependencies.rb
about_page-0.2.3 lib/about_page/dependencies.rb
about_page-0.2.2 lib/about_page/dependencies.rb
about_page-0.2.1 lib/about_page/dependencies.rb
about_page-0.2.0 lib/about_page/dependencies.rb