toki 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (9) hide show
  1. data/.travis.yml +10 -0
  2. data/Gemfile +6 -0
  3. data/README.md +48 -16
  4. data/Rakefile +6 -8
  5. data/changelog +9 -0
  6. data/lib/toki.rb +40 -29
  7. data/test/test_toki.rb +78 -18
  8. data/version +1 -1
  9. metadata +4 -3
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.8.7"
4
+ - "1.9.2"
5
+ - "1.9.3"
6
+ - "2.0.0"
7
+ - jruby-18mode
8
+ - jruby-19mode
9
+ - rbx-18mode
10
+ - rbx-19mode
data/Gemfile CHANGED
@@ -2,3 +2,9 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in toki.gemspec
4
4
  gemspec
5
+
6
+ gem 'rake'
7
+
8
+ # if defined?(Rubinius)
9
+ # gem 'rubysl-test-unit'
10
+ # end
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Toki
1
+ # Toki [![Gem Version](https://badge.fury.io/rb/toki.png)](http://badge.fury.io/rb/toki) [![Build Status](https://travis-ci.org/juskoljo/toki.png?branch=master)](https://travis-ci.org/juskoljo/toki)
2
2
 
3
3
  Toki is a DSL that helps you to implement Ruby version, patchlevel, engine and platform specific code
4
4
 
@@ -21,7 +21,7 @@ Or install it yourself as:
21
21
  ```ruby
22
22
  require 'toki'
23
23
 
24
- class Example
24
+ class Example1
25
25
  extend Toki
26
26
 
27
27
  def self.ping
@@ -35,7 +35,7 @@ class Example
35
35
  end
36
36
 
37
37
  # any mri version of ruby that is 1.9 or later
38
- when_ruby :engine => 'ruby', :version => /^((?!1\.[0-8]).*?)$/ do
38
+ when_ruby :engine => :ruby, :version => /^(?!1\.[0-8]).*$/ do
39
39
  # also class methods can be defined
40
40
  def self.ping
41
41
  "gnop"
@@ -51,17 +51,29 @@ class Example
51
51
  when_ruby :version => '1.8.7', :patchlevel => '371' do; end
52
52
 
53
53
  # applied only if engine and version matches
54
- when_ruby :engine => 'ruby', :version => '1.8.7' do; end
55
- when_ruby :engine => 'jruby', :jruby_version => '1.7.6' do; end
56
- when_ruby :engine => 'macruby', :macruby_version => '0.12' do; end
57
- when_ruby :engine => 'rbx', :rbx_version => '2.1.1' do; end
58
- when_ruby :engine => 'maglev', :maglev_version => '1.1RC1' do; end
59
- when_ruby :engine => 'ironruby', :ironruby_version => '1.1.3' do; end
60
- when_ruby :engine => 'kiji', :kiji_version => '0.11' do; end
54
+ when_ruby :engine => :ruby, :version => '1.8.7' do; end
55
+ when_ruby :engine => "ruby", :version => '1.8.7' do; end
56
+
57
+ # notice that /ruby/ will match with both "ruby" and "jruby"
58
+ when_ruby :engine => /^ruby$/, :version => '1.8.7' do; end
59
+
60
+ when_ruby :engine => :jruby, :jruby_version => '1.7.6' do; end
61
+ when_ruby :engine => :macruby, :macruby_version => '0.12' do; end
62
+ when_ruby :engine => :rbx, :rbx_version => '2.1.1' do; end
63
+ when_ruby :engine => :maglev, :maglev_version => '1.1RC1' do; end
64
+ when_ruby :engine => :ironruby, :ironruby_version => '1.1.3' do; end
65
+ when_ruby :engine => :kiji, :kiji_version => '0.11' do; end
66
+
67
+ # apply only if matches with one of the engines
68
+ when_ruby :engine => [:ruby, :jruby] do; end
69
+
70
+ # apply only if matches with one of the engines
71
+ when_ruby :version => ['1.8.7', '2.0.0'] do; end
61
72
 
62
73
  # applied only if platform matches; :windows, :osx, :linux, :unix, :unknown
63
74
  when_ruby :platform => :linux do; end
64
75
  when_ruby :platform => /linux/ do; end
76
+ when_ruby :platform => "linux" do; end
65
77
 
66
78
  # alternative method to specify ruby version specific implementation
67
79
  when_ruby_version '2.0.0' do; end
@@ -71,19 +83,39 @@ class Example
71
83
  when_maglev_version '1.1RC1' do; end
72
84
  when_ironruby_version '1.1.3' do; end
73
85
  when_kiji_version '0.11' do; end
74
- when_ruby_patchlevel '371' do; end
75
- when_ruby_engine 'rbx' do; end
86
+ when_ruby_patchlevel 371 do; end
87
+ when_ruby_engine :rbx do; end
76
88
  when_platform :osx do; end
77
89
 
78
90
  end
79
91
 
92
+ class Example2
93
+ include Toki
94
+ def array_to_string
95
+ when_ruby_engine :ruby do
96
+ when_ruby :version => /^(?!1\.[0-8]).*$/ do
97
+ # MRI 1.9.x or later
98
+ return [1,2,3].join
99
+ end
100
+ # MRI 1.8.7
101
+ return [1,2,3].to_s
102
+ end
103
+ "321"
104
+ end
105
+ end
106
+
80
107
  # MRI 1.8.7
81
- puts Example.ping # => "pong"
82
- puts Example.new.array_to_string # => "123"
108
+ puts Example1.ping # => "pong"
109
+ puts Example1.new.array_to_string # => "123"
110
+ puts Example2.new.array_to_string # => "123"
83
111
 
84
112
  # MRI 2.0.0
85
- puts Example.ping # => "gnop"
86
- puts Example.new.array_to_string # => "123"
113
+ puts Example1.ping # => "gnop"
114
+ puts Example1.new.array_to_string # => "123"
115
+ puts Example2.new.array_to_string # => "123"
116
+
117
+ # e.g. JRuby or any other engine
118
+ puts Example2.new.array_to_string # => "321"
87
119
  ```
88
120
 
89
121
  ## Contributing
data/Rakefile CHANGED
@@ -4,12 +4,10 @@ require "rake/testtask"
4
4
 
5
5
  task :default => :test
6
6
 
7
- desc "Run unit tests"
8
- task :test do
9
- Rake::TestTask.new { | task |
10
- task.pattern = 'test/test_*.rb'
11
- task.verbose = true
12
- task.options = '-v'
13
- task.warning = false
14
- }
7
+ # :test
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "test"
10
+ t.test_files = FileList['test/test_*.rb']
11
+ t.options = '-v'
12
+ t.verbose = true
15
13
  end
data/changelog CHANGED
@@ -1,3 +1,12 @@
1
+ Sun Oct 27 22:29:21 +0200 2013 Jussi Koljonen <jussi.koljonen@gmail.com>
2
+
3
+ * Toki version 0.0.3 changes:
4
+
5
+ * Toki can be included to Class/Module and called from instance methods
6
+ * Added support for Array and Symbol arguments; e.g. :engine => [:ruby, 'jruby']
7
+ * Added more platforms to host_os detection; "msdos" and "djgpp" (windows)
8
+ * Minor internal refactoring
9
+
1
10
  Sun Oct 27 12:42:47 +0200 2013 Jussi Koljonen <jussi.koljonen@gmail.com>
2
11
 
3
12
  * Toki version 0.0.2 changes:
data/lib/toki.rb CHANGED
@@ -34,9 +34,9 @@ module Toki
34
34
  end
35
35
 
36
36
  # JRUBY_VERSION
37
- def when_jruby_version(pattern, &block)
37
+ def when_jruby_version(value, &block)
38
38
  if defined?(JRUBY_VERSION)
39
- if pattern_matches_with?(JRUBY_VERSION, pattern)
39
+ if value_matches_with?(value, JRUBY_VERSION)
40
40
  apply_behaviour(&block)
41
41
  else
42
42
  false
@@ -48,9 +48,9 @@ module Toki
48
48
  end
49
49
 
50
50
  # MACRUBY_VERSION
51
- def when_macruby_version(pattern, &block)
51
+ def when_macruby_version(value, &block)
52
52
  if defined?(MACRUBY_VERSION)
53
- if pattern_matches_with?(MACRUBY_VERSION, pattern)
53
+ if value_matches_with?(value, MACRUBY_VERSION)
54
54
  apply_behaviour(&block)
55
55
  else
56
56
  false
@@ -62,9 +62,9 @@ module Toki
62
62
  end
63
63
 
64
64
  # Rubinius::VERSION
65
- def when_rbx_version(pattern, &block)
65
+ def when_rbx_version(value, &block)
66
66
  if defined?(Rubinius)
67
- if pattern_matches_with?(Rubinius::VERSION, pattern)
67
+ if value_matches_with?(value, Rubinius::VERSION)
68
68
  apply_behaviour(&block)
69
69
  else
70
70
  false
@@ -76,9 +76,9 @@ module Toki
76
76
  end
77
77
 
78
78
  # MAGLEV_VERSION
79
- def when_maglev_version(pattern, &block)
79
+ def when_maglev_version(value, &block)
80
80
  if defined?(MAGLEV_VERSION)
81
- if pattern_matches_with?(MAGLEV_VERSION, pattern)
81
+ if value_matches_with?(value, MAGLEV_VERSION)
82
82
  apply_behaviour(&block)
83
83
  else
84
84
  false
@@ -90,9 +90,9 @@ module Toki
90
90
  end
91
91
 
92
92
  # IRONRUBY_VERSION
93
- def when_ironruby_version(pattern, &block)
93
+ def when_ironruby_version(value, &block)
94
94
  if defined?(IRONRUBY_VERSION)
95
- if pattern_matches_with?(IRONRUBY_VERSION, pattern)
95
+ if value_matches_with?(value, IRONRUBY_VERSION)
96
96
  apply_behaviour(&block)
97
97
  else
98
98
  false
@@ -104,9 +104,9 @@ module Toki
104
104
  end
105
105
 
106
106
  # KIJI_VERSION
107
- def when_kiji_version(pattern, &block)
107
+ def when_kiji_version(value, &block)
108
108
  if defined?(KIJI_VERSION)
109
- if pattern_matches_with?(KIJI_VERSION, pattern)
109
+ if value_matches_with?(value, KIJI_VERSION)
110
110
  apply_behaviour(&block)
111
111
  else
112
112
  false
@@ -118,8 +118,8 @@ module Toki
118
118
  end
119
119
 
120
120
  # RUBY_VERSION
121
- def when_ruby_version(pattern, &block)
122
- if pattern_matches_with?(RUBY_VERSION, pattern)
121
+ def when_ruby_version(value, &block)
122
+ if value_matches_with?(value, RUBY_VERSION)
123
123
  apply_behaviour(&block)
124
124
  else
125
125
  false
@@ -127,8 +127,8 @@ module Toki
127
127
  end
128
128
 
129
129
  # RUBY_PATCHLEVEL
130
- def when_ruby_patchlevel(pattern, &block)
131
- if pattern_matches_with?(RUBY_PATCHLEVEL, pattern)
130
+ def when_ruby_patchlevel(value, &block)
131
+ if value_matches_with?(value, RUBY_PATCHLEVEL)
132
132
  apply_behaviour(&block)
133
133
  else
134
134
  false
@@ -136,10 +136,10 @@ module Toki
136
136
  end
137
137
 
138
138
  # RUBY_ENGINE
139
- def when_ruby_engine(pattern, &block)
139
+ def when_ruby_engine(value, &block)
140
140
  # ruby 1.8.7 does not have RUBY_ENGINE constant
141
141
  ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
142
- if pattern_matches_with?(ruby_engine, pattern)
142
+ if value_matches_with?(value, ruby_engine)
143
143
  apply_behaviour(&block)
144
144
  else
145
145
  false
@@ -147,16 +147,16 @@ module Toki
147
147
  end
148
148
 
149
149
  # RUBY_PLATFORM
150
- def when_platform(pattern, &block)
151
- case pattern
150
+ def when_platform(value, &block)
151
+ case value
152
152
  when Symbol
153
- if pattern == detect_os
153
+ if value == detect_os
154
154
  apply_behaviour(&block)
155
155
  else
156
156
  false
157
157
  end
158
158
  else
159
- if pattern_matches_with?(RbConfig::CONFIG['host_os'], pattern)
159
+ if value_matches_with?(value, RbConfig::CONFIG['host_os'])
160
160
  apply_behaviour(&block)
161
161
  else
162
162
  false
@@ -168,7 +168,7 @@ module Toki
168
168
 
169
169
  def detect_os
170
170
  case RbConfig::CONFIG['host_os']
171
- when /mswin|msys|mingw|windows|win32|cygwin|bccwin|wince|emc/i
171
+ when /msdos|mswin|msys|djgpp|mingw|windows|win32|cygwin|bccwin|wince|emc/i
172
172
  :windows
173
173
  when /darwin|mac os/i
174
174
  :osx
@@ -181,20 +181,31 @@ module Toki
181
181
  end
182
182
  end
183
183
 
184
- def pattern_matches_with?(constant, pattern)
185
- constant.to_s =~ case pattern
184
+ def value_matches_with?(value, other)
185
+ case value
186
186
  when String, Integer
187
- Regexp.union(pattern.to_s)
187
+ value.to_s == other.to_s
188
188
  when Regexp
189
- pattern
189
+ value =~ other
190
+ when Array
191
+ value.any?{|item| value_matches_with?(item, other)}
192
+ when Symbol
193
+ value == other.to_sym
190
194
  else
191
- raise ArgumentError, "wrong argument type #{pattern.class} (expected String or Regexp)"
195
+ raise ArgumentError, "wrong argument type #{value.class} (expected String, Regexp, Array or Symbol)"
192
196
  end
193
197
  end
194
198
 
195
199
  # apply behaviour to target class
196
200
  def apply_behaviour(&block)
197
- class_eval(&block) if block_given?
201
+ if block_given?
202
+ case self
203
+ when Class, Module
204
+ class_eval(&block)
205
+ else
206
+ instance_eval(&block)
207
+ end
208
+ end
198
209
  true
199
210
  end
200
211
 
data/test/test_toki.rb CHANGED
@@ -28,15 +28,17 @@ class TestToki < Test::Unit::TestCase
28
28
  end
29
29
 
30
30
  def test_jruby_version
31
- unless (jruby_version_defined = defined?(JRUBY_VERSION))
32
- Object.const_set(:JRUBY_VERSION, '1.7.4')
31
+ if (jruby_version_defined = defined?(JRUBY_VERSION))
32
+ version = JRUBY_VERSION
33
+ else
34
+ Object.const_set(:JRUBY_VERSION, (version = 'x.x.x'))
33
35
  end
34
36
  klass = Class.new do
35
37
  extend Toki
36
38
  def hello
37
39
  nil
38
40
  end
39
- when_ruby :jruby_version => '1.7.4' do
41
+ when_ruby :jruby_version => version do
40
42
  def hello
41
43
  "world"
42
44
  end
@@ -49,9 +51,11 @@ class TestToki < Test::Unit::TestCase
49
51
  end
50
52
 
51
53
  def test_rbx_version
52
- unless (rbx_defined = defined?(Rubinius))
54
+ if (rbx_defined = defined?(Rubinius))
55
+ version = Rubinius::VERSION
56
+ else
53
57
  klass = Class.new
54
- klass.const_set(:VERSION, '2.1.1')
58
+ klass.const_set(:VERSION, (version = 'x.x.x'))
55
59
  Object.const_set(:Rubinius, klass)
56
60
  end
57
61
  klass = Class.new do
@@ -59,7 +63,7 @@ class TestToki < Test::Unit::TestCase
59
63
  def hello
60
64
  nil
61
65
  end
62
- when_ruby :rbx_version => '2.1.1' do
66
+ when_ruby :rbx_version => version do
63
67
  def hello
64
68
  "world"
65
69
  end
@@ -72,15 +76,17 @@ class TestToki < Test::Unit::TestCase
72
76
  end
73
77
 
74
78
  def test_macruby_version
75
- unless (macruby_version_defined = defined?(MACRUBY_VERSION))
76
- Object.const_set(:MACRUBY_VERSION, '0.12')
79
+ if (macruby_version_defined = defined?(MACRUBY_VERSION))
80
+ version = MACRUBY_VERSION
81
+ else
82
+ Object.const_set(:MACRUBY_VERSION, (version = 'x.x.x'))
77
83
  end
78
84
  klass = Class.new do
79
85
  extend Toki
80
86
  def hello
81
87
  nil
82
88
  end
83
- when_ruby :macruby_version => '0.12' do
89
+ when_ruby :macruby_version => version do
84
90
  def hello
85
91
  "world"
86
92
  end
@@ -93,15 +99,17 @@ class TestToki < Test::Unit::TestCase
93
99
  end
94
100
 
95
101
  def test_maglev_version
96
- unless (maglev_version_defined = defined?(MAGLEV_VERSION))
97
- Object.const_set(:MAGLEV_VERSION, '1.1RC1')
102
+ if (maglev_version_defined = defined?(MAGLEV_VERSION))
103
+ version = MAGLEV_VERSION
104
+ else
105
+ Object.const_set(:MAGLEV_VERSION, (version = 'x.x.x'))
98
106
  end
99
107
  klass = Class.new do
100
108
  extend Toki
101
109
  def hello
102
110
  nil
103
111
  end
104
- when_ruby :maglev_version => '1.1RC1' do
112
+ when_ruby :maglev_version => version do
105
113
  def hello
106
114
  "world"
107
115
  end
@@ -114,15 +122,17 @@ class TestToki < Test::Unit::TestCase
114
122
  end
115
123
 
116
124
  def test_ironruby_version
117
- unless (ironruby_version_defined = defined?(IRONRUBY_VERSION))
118
- Object.const_set(:IRONRUBY_VERSION, '1.1.3')
125
+ if (ironruby_version_defined = defined?(IRONRUBY_VERSION))
126
+ version = IRONRUBY_VERSION
127
+ else
128
+ Object.const_set(:IRONRUBY_VERSION, (version = 'x.x.x'))
119
129
  end
120
130
  klass = Class.new do
121
131
  extend Toki
122
132
  def hello
123
133
  nil
124
134
  end
125
- when_ruby :ironruby_version => '1.1.3' do
135
+ when_ruby :ironruby_version => version do
126
136
  def hello
127
137
  "world"
128
138
  end
@@ -135,15 +145,17 @@ class TestToki < Test::Unit::TestCase
135
145
  end
136
146
 
137
147
  def test_kiji_version
138
- unless (kiji_version_defined = defined?(KIJI_VERSION))
139
- Object.const_set(:KIJI_VERSION, '0.11')
148
+ if (kiji_version_defined = defined?(KIJI_VERSION))
149
+ version = KIJI_VERSION
150
+ else
151
+ Object.const_set(:KIJI_VERSION, (version = 'x.x.x'))
140
152
  end
141
153
  klass = Class.new do
142
154
  extend Toki
143
155
  def hello
144
156
  nil
145
157
  end
146
- when_ruby :kiji_version => '0.11' do
158
+ when_ruby :kiji_version => version do
147
159
  def hello
148
160
  "world"
149
161
  end
@@ -188,6 +200,40 @@ class TestToki < Test::Unit::TestCase
188
200
  assert_equal "world", instance.hello
189
201
  end
190
202
 
203
+ def test_ruby_engine_with_symbol_argument
204
+ ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
205
+ ruby_engine = ruby_engine.to_sym
206
+ klass = Class.new do
207
+ extend Toki
208
+ def hello
209
+ nil
210
+ end
211
+ when_ruby :engine => ruby_engine do
212
+ def hello
213
+ "world"
214
+ end
215
+ end
216
+ end
217
+ instance = klass.new
218
+ assert_equal "world", instance.hello
219
+ end
220
+
221
+ def test_ruby_version_with_array_argument
222
+ klass = Class.new do
223
+ extend Toki
224
+ def hello
225
+ nil
226
+ end
227
+ when_ruby :version => ['x.x.x', RUBY_VERSION] do
228
+ def hello
229
+ "world"
230
+ end
231
+ end
232
+ end
233
+ instance = klass.new
234
+ assert_equal "world", instance.hello
235
+ end
236
+
191
237
  def test_platform
192
238
  klass = Class.new do
193
239
  extend Toki
@@ -221,4 +267,18 @@ class TestToki < Test::Unit::TestCase
221
267
  assert_equal "world", klass.hello
222
268
  end
223
269
 
270
+ def test_include_toki_to_instance
271
+ klass = Class.new do
272
+ include Toki
273
+ def raise_exception
274
+ when_ruby_version RUBY_VERSION do
275
+ raise RuntimeError
276
+ end
277
+ end
278
+ end
279
+ assert_raises RuntimeError do
280
+ klass.new.raise_exception
281
+ end
282
+ end
283
+
224
284
  end
data/version CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toki
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jussi Koljonen
@@ -29,6 +29,7 @@ extra_rdoc_files: []
29
29
 
30
30
  files:
31
31
  - .gitignore
32
+ - .travis.yml
32
33
  - Gemfile
33
34
  - LICENSE
34
35
  - README.md