Class: VersionGuard::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/version_guard.rb

Instance Method Summary collapse

Constructor Details

#initialize(target, requirement, options = {}) ⇒ Checker

Returns a new instance of Checker.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/version_guard.rb', line 15

def initialize(target, requirement, options = {})
  if target.nil? or requirement.nil? or target.empty? or requirement.empty?
    raise Error, 'target and requirement cannot be nil'
  end

  @requirement = requirement
  @options = options

  @version = begin
    Gem::Version.new(target) # raise ArgumentError unless target is like '1.2.3'
    target
  rescue ArgumentError
    # constantize string like 'ActiveRecord::VERSION::STRING'
    names = target.split('::')
    names.shift if names.empty? || names.first.empty? # Normalize ::TOPLEVEL
    @options[:name] ||= names.first
    constant = Object
    names.each do |name|
      constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
    end
    constant
  end
end

Instance Method Details

#abort_if_failObject



44
45
46
47
48
49
50
51
52
# File 'lib/version_guard.rb', line 44

def abort_if_fail
  if check
    puts "#{message_base} passed" if @options[:verbose]
  else
    trace = caller.find{|i| i !~ /version_guard\.rb/ }
    filename, lineno = trace.split(':')[0..1]
    abort "#{message_base} failed - check line #{lineno} in #{filename}"
  end
end

#checkObject



39
40
41
42
# File 'lib/version_guard.rb', line 39

def check
  requirements = @requirement.is_a?(Array) ? @requirement : [@requirement]
  Gem::Version::Requirement.new(requirements).satisfied_by?(Gem::Version.new(@version))
end

#message_baseObject



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

def message_base
  if @options[:name]
    "Version check for #{@options[:name]} (#{@version.inspect} with #{@requirement.inspect})"
  else
    "Version check #{@version.inspect} with #{@requirement.inspect}"
  end
end