Class: RedCard

Inherits:
Object
  • Object
show all
Defined in:
lib/redcard.rb,
lib/redcard/engine.rb,
lib/redcard/version.rb

Defined Under Namespace

Classes: Engine, InvalidRubyEngineError, InvalidRubyError, InvalidRubyVersionError, UnknownRubyEngineError, Version

Constant Summary collapse

VERSION =
"1.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*requirements) ⇒ RedCard

Returns a new instance of RedCard.



51
52
53
54
55
# File 'lib/redcard.rb', line 51

def initialize(*requirements)
  @engine_versions = requirements.last.kind_of?(Hash) ? requirements.pop.to_a : []
  @engines = requirements.select { |x| x.kind_of? Symbol }
  @versions = requirements.select { |x| x.kind_of? String or x.kind_of? Range }
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



49
50
51
# File 'lib/redcard.rb', line 49

def error
  @error
end

#messageObject (readonly)

Returns the value of attribute message.



49
50
51
# File 'lib/redcard.rb', line 49

def message
  @message
end

Class Method Details

.check(*requirements) ⇒ Object



10
11
12
# File 'lib/redcard.rb', line 10

def self.check(*requirements)
  new(*requirements).check
end

.engineObject



21
22
23
# File 'lib/redcard.rb', line 21

def self.engine
  (defined?(RUBY_ENGINE) && RUBY_ENGINE) || "ruby"
end

.engine_versionObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/redcard.rb', line 25

def self.engine_version
  case engine
  # when "ironruby"
    # TODO
  when "jruby"
    Object.const_get :JRUBY_VERSION
  when "maglev"
    Object.const_get :MAGLEV_VERSION
  when "rbx"
    if defined?(::Rubinius)
      Object.const_get(:Rubinius).const_get(:VERSION)
    end
  when "ruby"
    RUBY_VERSION
  when "topaz"
    if defined?(::Topaz)
      Object.const_get(:Topaz).const_get(:VERSION)
    end
  else
    raise UnknownRubyEngineError, "#{engine} is not recognized"
  end
end

.verify(*requirements) ⇒ Object



14
15
16
17
18
19
# File 'lib/redcard.rb', line 14

def self.verify(*requirements)
  card = new(*requirements)
  unless card.check
    raise card.error, card.message
  end
end

Instance Method Details

#checkObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/redcard.rb', line 57

def check
  unless @engines.empty? or
         @engines.any? { |x| Engine.new(RedCard.engine, x).valid? }
    invalid_engine
    return false
  end

  unless @versions.empty? or
         @versions.any? { |x| Version.new(RUBY_VERSION, x).valid? }
    invalid_ruby_version
    return false
  end

  return true if @engine_versions.empty?

  @engine_versions.map! do |e, v|
    [Engine.new(RedCard.engine, e), Version.new(RedCard.engine_version, v)]
  end

  return true if @engine_versions.any? do |engine, version|
    engine.valid? and version.valid?
  end

  invalid_engine_version
  return false
end