Class: TenYearsRails::GemInfo

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::DateHelper
Defined in:
lib/ten_years_rails/gem_info.rb

Direct Known Subclasses

NullGemInfo

Defined Under Namespace

Classes: NullGemInfo

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gem_specification) ⇒ GemInfo

Returns a new instance of GemInfo.



48
49
50
51
52
# File 'lib/ten_years_rails/gem_info.rb', line 48

def initialize(gem_specification)
  @gem_specification = gem_specification
  @version = gem_specification.version
  @name = gem_specification.name
end

Instance Attribute Details

#gem_specificationObject (readonly)

Returns the value of attribute gem_specification.



47
48
49
# File 'lib/ten_years_rails/gem_info.rb', line 47

def gem_specification
  @gem_specification
end

#nameObject (readonly)

Returns the value of attribute name.



47
48
49
# File 'lib/ten_years_rails/gem_info.rb', line 47

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



47
48
49
# File 'lib/ten_years_rails/gem_info.rb', line 47

def version
  @version
end

Class Method Details

.allObject



41
42
43
44
45
# File 'lib/ten_years_rails/gem_info.rb', line 41

def self.all
  Gem::Specification.each.map do |gem_specification|
    new(gem_specification)
  end
end

Instance Method Details

#ageObject



54
55
56
57
58
59
60
# File 'lib/ten_years_rails/gem_info.rb', line 54

def age
  if respond_to?(:time_ago_in_words)
    "#{time_ago_in_words(created_at)} ago"
  else
    created_at.strftime("%b %e, %Y")
  end
end

#compatible_with_rails?(rails_version: Gem::Version.new("5.0")) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/ten_years_rails/gem_info.rb', line 97

def compatible_with_rails?(rails_version: Gem::Version.new("5.0"))
  unsatisfied_rails_dependencies(rails_version: rails_version).empty?
end

#created_atObject



66
67
68
# File 'lib/ten_years_rails/gem_info.rb', line 66

def created_at
  @created_at ||= gem_specification.date
end

#from_rails?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/ten_years_rails/gem_info.rb', line 109

def from_rails?
  rails_gems.include?(name)
end

#latest_versionObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/ten_years_rails/gem_info.rb', line 86

def latest_version
  @latest_version ||= begin
    latest_gem_specification = Gem.latest_spec_for(name)
    if latest_gem_specification
      GemInfo.new(latest_gem_specification)
    else
      NullGemInfo.new
    end
  end
end

#sourced_from_git?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/ten_years_rails/gem_info.rb', line 62

def sourced_from_git?
  !!gem_specification.git_version
end

#state(rails_version) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ten_years_rails/gem_info.rb', line 74

def state(rails_version)
  if compatible_with_rails?(rails_version: rails_version)
    :compatible
  elsif latest_version.compatible_with_rails?(rails_version: rails_version)
    :latest_compatible
  elsif latest_version.version == "NOT FOUND"
    :no_new_version
  else
    :incompatible
  end
end

#unsatisfied_rails_dependencies(rails_version:) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/ten_years_rails/gem_info.rb', line 101

def unsatisfied_rails_dependencies(rails_version:)
  rails_dependencies = gem_specification.runtime_dependencies.select {|dependency| rails_gems.include?(dependency.name) }

  rails_dependencies.reject do |rails_dependency|
    rails_dependency.requirement.satisfied_by?(Gem::Version.new(rails_version))
  end
end

#up_to_date?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/ten_years_rails/gem_info.rb', line 70

def up_to_date?
  version == latest_version.version
end