Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
146 lines (112 sloc) 3.228 kB
class Dependency < ActiveRecord::Base
belongs_to :rubygem
belongs_to :version
before_validation :use_gem_dependency,
:use_existing_rubygem,
:parse_gem_dependency
after_create :push_on_to_list
validates :requirements, :presence => true
validates :scope, :inclusion => {:in => %w(development runtime)}
attr_accessor :gem_dependency
def self.unresolved(rubygem)
where(unresolved_name: nil, rubygem_id: rubygem.id)
end
def self.mark_unresolved_for(rubygem)
unresolved(rubygem).update_all(unresolved_name: rubygem.name,
rubygem_id: nil)
end
def self.development
where(:scope => 'development')
end
def self.runtime
where(:scope => 'runtime')
end
def self.runtime_key(full_name)
"rd:#{full_name}"
end
# rails,rack,bundler
def self.for(gem_list)
versions = Redis.current.pipelined do
gem_list.each do |rubygem_name|
Redis.current.lrange(Rubygem.versions_key(rubygem_name), 0, -1)
end
end || []
versions.flatten!
return [] if versions.blank?
data = Redis.current.pipelined do
versions.each do |version|
Redis.current.hvals(Version.info_key(version))
Redis.current.lrange(Dependency.runtime_key(version), 0, -1)
end
end
data.in_groups_of(2).map do |(name, number, platform), deps|
{
:name => name,
:number => number,
:platform => platform,
:dependencies => deps.map { |dep| dep.split(" ", 2) }
}
end
end
def name
unresolved_name || rubygem.try(:name)
end
def payload
{
'name' => name,
'requirements' => clean_requirements
}
end
def as_json(options={})
payload
end
def to_xml(options={})
payload.to_xml(options.merge(:root => 'dependency'))
end
def to_yaml(*args)
payload.to_yaml(*args)
end
def encode_with(coder)
coder.tag, coder.implicit, coder.map = nil, true, payload
end
def to_s
"#{name} #{clean_requirements}"
end
def clean_requirements(reqs = requirements)
reqs.gsub(/#<YAML::Syck::DefaultKey[^>]*>/, "=")
end
def update_resolved(rubygem)
self.rubygem = rubygem
self.unresolved_name = nil
save!
end
private
def use_gem_dependency
return if self.rubygem
if gem_dependency.class != Gem::Dependency
errors.add :rubygem, "Please use Gem::Dependency to specify dependencies."
return false
end
if gem_dependency.name.empty?
errors.add :rubygem, "Blank is not a valid dependency name"
return false
end
true
end
def use_existing_rubygem
return if self.rubygem
unless self.rubygem = Rubygem.find_by_name(gem_dependency.name)
self.unresolved_name = gem_dependency.name
end
true
end
def parse_gem_dependency
return if self.requirements
reqs = gem_dependency.requirements_list.join(', ')
self.requirements = clean_requirements(reqs)
self.scope = gem_dependency.type.to_s
end
def push_on_to_list
Redis.current.lpush(Dependency.runtime_key(self.version.full_name), self.to_s) if self.scope == 'runtime'
end
end
Jump to Line
Something went wrong with that request. Please try again.