monkey-lib 0.3.3 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/monkey/engine.rb CHANGED
@@ -14,7 +14,7 @@ module Monkey
14
14
  alias rubinius? rbx?
15
15
 
16
16
  def ree?
17
- !!(RUBY_DESCRIPTION =~ /Ruby Enterprise Edition/)
17
+ !!(RUBY_DESCRIPTION =~ /Ruby Enterprise Edition/) || RUBY_ENGINE == "ree"
18
18
  end
19
19
 
20
20
  module_function :jruby?
@@ -25,11 +25,11 @@ module Monkey
25
25
  module_function :macruby?
26
26
  module_function :maglev?
27
27
  module_function :ree?
28
-
28
+
29
29
  def ruby_core?
30
30
  maglev? or rubinius?
31
31
  end
32
-
32
+
33
33
  module_function :ruby_core?
34
34
 
35
35
  include Rubinius if defined? Rubinius
@@ -74,17 +74,66 @@ module Monkey
74
74
  ::RUBY_DESCRIPTION << "[#{RUBY_PLATFORM}]"
75
75
  end
76
76
 
77
- def ruby_engine(pretty = true)
78
- return RUBY_ENGINE unless pretty
79
- case RUBY_ENGINE
77
+ if ree?
78
+ ::REAL_RUBY_ENGINE_VERSION = ::REE_VERSION = RUBY_DESCRIPTION[/[^ ]+$/]
79
+ ::REAL_RUBY_ENGINE = "ree"
80
+ else
81
+ ::REAL_RUBY_ENGINE, ::REAL_RUBY_ENGINE_VERSION = ::RUBY_ENGINE, ::RUBY_ENGINE_VERSION
82
+ end
83
+
84
+ def ruby_engine(engine = RUBY_ENGINE, pretty = true)
85
+ return engine unless pretty
86
+ case engine
80
87
  when "ruby" then ree? ? "Ruby Enterprise Edition" : "Ruby"
88
+ when "ree" then "Ruby Enterprise Edition"
81
89
  when "rbx" then "Rubinius"
82
90
  when "maglev" then "MagLev"
83
- else RUBY_ENGINE.capitalize.gsub("ruby", "Ruby")
91
+ else engine.capitalize.gsub("ruby", "Ruby")
84
92
  end
85
93
  end
86
94
 
87
95
  module_function :ruby_engine
96
+
97
+ def self.set_engine(engine, engine_version = nil)
98
+ Object.send :remove_const, "RUBY_ENGINE"
99
+ Object.const_set "RUBY_ENGINE", engine
100
+ if engine_version
101
+ Object.send :remove_const, "RUBY_ENGINE_VERSION"
102
+ Object.const_set "RUBY_ENGINE_VERSION", engine_version
103
+ end
104
+ end
105
+
106
+ def with_ruby_engine(engine, engine_version)
107
+ engine_was, engine_version_was = ::RUBY_ENGINE, ::RUBY_ENGINE_VERSION
108
+ unless defined? ::OLD_RUBY_ENGINE
109
+ Object.const_set("OLD_RUBY_ENGINE", ::RUBY_ENGINE)
110
+ Object.const_set("OLD_RUBY_ENGINE_VERSION", ::RUBY_ENGINE_VERSION)
111
+ end
112
+ Monkey::Engine.set_engine engine, engine_version
113
+ if block_given?
114
+ result = yield
115
+ Monkey::Engine.set_engine engine_was, engine_version_was
116
+ result
117
+ else
118
+ [engine_was, engine_version_was]
119
+ end
120
+ end
121
+
122
+ module_function :with_ruby_engine
123
+
124
+ def use_real_ruby_engine(&block)
125
+ with_ruby_engine(::REAL_RUBY_ENGINE, ::REAL_RUBY_ENGINE_VERSION, &block)
126
+ end
127
+
128
+ module_function :use_real_ruby_engine
129
+
130
+ def use_original_ruby_engine(&block)
131
+ if defined? ::OLD_RUBY_ENGINE then with_ruby_engine(::OLD_RUBY_ENGINE, ::OLD_RUBY_ENGINE_VERSION, &block)
132
+ else with_ruby_engine(::RUBY_ENGINE, ::RUBY_ENGINE_VERSION, &block)
133
+ end
134
+ end
135
+
136
+ module_function :use_original_ruby_engine
88
137
 
89
138
  end
90
139
  end
@@ -14,4 +14,42 @@ describe Monkey::Engine do
14
14
  defined?(RUBY_DESCRIPTION).should == "constant"
15
15
  end
16
16
 
17
+ it "defines REAL_RUBY_VERSION" do
18
+ defined?(RUBY_VERSION).should == "constant"
19
+ end
20
+
21
+ it "defines REAL_RUBY_ENGINE_VERSION" do
22
+ defined?(RUBY_ENGINE_VERSION).should == "constant"
23
+ end
24
+
25
+ describe "with_ruby_engine" do
26
+
27
+ it "sets RUBY_ENGINE and RUBY_ENGINE_VERSION" do
28
+ Monkey::Engine.with_ruby_engine("jruby", "1.4.0") do
29
+ RUBY_ENGINE.should == "jruby"
30
+ RUBY_ENGINE_VERSION.should == "1.4.0"
31
+ Monkey::Engine.should be_jruby
32
+ end
33
+ Monkey::Engine.with_ruby_engine("ruby", "1.8.7") do
34
+ RUBY_ENGINE.should == "ruby"
35
+ RUBY_ENGINE_VERSION.should == "1.8.7"
36
+ Monkey::Engine.should be_mri
37
+ end
38
+ end
39
+
40
+ it "restores old RUBY_ENGINE if block given" do
41
+ old = RUBY_ENGINE
42
+ Monkey::Engine.with_ruby_engine("foobar", "1.0") { }
43
+ RUBY_ENGINE.should_not == "foobar"
44
+ RUBY_ENGINE.should == old
45
+ end
46
+
47
+ it "restores old RUBY_ENGINE_VERSION if block given" do
48
+ old = RUBY_ENGINE_VERSION
49
+ Monkey::Engine.with_ruby_engine("foobar", "1.0") { }
50
+ RUBY_ENGINE_VERSION.should == old
51
+ end
52
+
53
+ end
54
+
17
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monkey-lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Haase