Class: CapistranoSentinel::GemFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano_sentinel/classes/gem_finder.rb

Overview

helper used to determine gem versions

Class Method Summary collapse

Class Method Details

.capistrano_version_2?Boolean

Returns:

  • (Boolean)

22
23
24
25
# File 'lib/capistrano_sentinel/classes/gem_finder.rb', line 22

def capistrano_version_2?
  cap_version = fetch_gem_version('capistrano')
  value_blank?(cap_version) ? false : verify_gem_version(cap_version, '3.0', operator: '<')
end

.fetch_gem_version(gem_name) ⇒ Object


45
46
47
48
# File 'lib/capistrano_sentinel/classes/gem_finder.rb', line 45

def fetch_gem_version(gem_name)
  version = find_loaded_gem_property(gem_name)
  value_blank?(version) ? nil : get_parsed_version(version)
end

.find_loaded_gem(name, property = nil) ⇒ Object


35
36
37
38
39
# File 'lib/capistrano_sentinel/classes/gem_finder.rb', line 35

def find_loaded_gem(name, property = nil)
  gem_spec = Gem.loaded_specs.values.find { |repo| repo.name == name }
  return if value_blank?(gem_spec)
  value_blank?(property) ?  gem_spec :  gem_spec.send(property)
end

.find_loaded_gem_property(gem_name, property = 'version') ⇒ Object


41
42
43
# File 'lib/capistrano_sentinel/classes/gem_finder.rb', line 41

def find_loaded_gem_property(gem_name, property = 'version')
  find_loaded_gem(gem_name, property)
end

.format_gem_version(version) ⇒ Object


57
58
59
60
61
# File 'lib/capistrano_sentinel/classes/gem_finder.rb', line 57

def format_gem_version(version)
  return version if version.size <= 2
  version.pop until version.size == 2
  version
end

.get_current_gem_nameObject


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/capistrano_sentinel/classes/gem_finder.rb', line 6

def get_current_gem_name
  searcher = if Gem::Specification.respond_to? :find
    # ruby 2.0
    Gem::Specification
  elsif Gem.respond_to? :searcher
    # ruby 1.8/1.9
    Gem.searcher.init_gemspecs
  end
  spec = unless searcher.nil?
    searcher.find do |spec|
      File.fnmatch(File.join(spec.full_gem_path,'*'), __FILE__)
    end
  end
  spec.name unless value_blank?(spec)
end

.get_parsed_version(version) ⇒ Object


50
51
52
53
54
55
# File 'lib/capistrano_sentinel/classes/gem_finder.rb', line 50

def get_parsed_version(version)
  return 0 if value_blank?(version)
  version = version.to_s.split('.')
  version = format_gem_version(version)
  version.join('.').to_f
end

.value_blank?(value) ⇒ Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/capistrano_sentinel/classes/gem_finder.rb', line 27

def value_blank?(value)
  value.nil? || value_empty?(value) || (value.is_a?(String) &&  /\A[[:space:]]*\z/ === value)
end

.value_empty?(value) ⇒ Boolean

Returns:

  • (Boolean)

31
32
33
# File 'lib/capistrano_sentinel/classes/gem_finder.rb', line 31

def value_empty?(value)
  value.respond_to?(:empty?) ? !!value.empty? : !value
end

.verify_gem_version(gem_version, version, options = {}) ⇒ Object


63
64
65
66
# File 'lib/capistrano_sentinel/classes/gem_finder.rb', line 63

def verify_gem_version(gem_version, version, options = {})
  version = get_parsed_version(version)
  get_parsed_version(gem_version).send(options.fetch('operator', '<='), version)
end