toki 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +89 -0
- data/Rakefile +15 -0
- data/lib/toki.rb +153 -0
- data/lib/toki/version.rb +3 -0
- data/test/test_toki.rb +161 -0
- data/toki.gemspec +17 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jussi Koljonen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# Toki
|
2
|
+
|
3
|
+
Toki is a DSL that helps you to implement Ruby version, patchlevel, engine and platform specific code
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'toki'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install toki
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'toki'
|
23
|
+
|
24
|
+
class Example
|
25
|
+
extend Toki
|
26
|
+
|
27
|
+
def self.ping
|
28
|
+
"pong"
|
29
|
+
end
|
30
|
+
|
31
|
+
# ruby 1.8.7 # => "123"
|
32
|
+
# ruby 2.0.0 # => "[1,2,3]"
|
33
|
+
def array_to_string
|
34
|
+
[1,2,3].to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
# any mri version of ruby that is 1.9 or later
|
38
|
+
when_ruby :version => /^((?!1\.[0-8]).*?)$/, :engine => 'ruby' do
|
39
|
+
# also class methods can be defined
|
40
|
+
def self.ping
|
41
|
+
"PONG"
|
42
|
+
end
|
43
|
+
# applied only if ruby 1.9.x or later; overwrites array_to_string method
|
44
|
+
# ruby 2.0.0 # => "123"
|
45
|
+
def array_to_string
|
46
|
+
[1,2,3].join
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# applied only if version and patchlevel matches
|
51
|
+
when_ruby :version => '1.8.7', :patchlevel => '371' do; end
|
52
|
+
|
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
|
+
|
59
|
+
# applied only if platform matches; :windows, :osx, :linux, :unix, :unknown
|
60
|
+
when_ruby :platform => :linux do; end
|
61
|
+
when_ruby :platform => /linux/ do; end
|
62
|
+
|
63
|
+
# alternative method to specify ruby version specific implementation
|
64
|
+
when_ruby_version '2.0.0' do; end
|
65
|
+
when_jruby_version '1.7.6' do; end
|
66
|
+
when_macruby_version '0.12' do; end
|
67
|
+
when_rbx_version '2.1.1' do; end
|
68
|
+
when_ruby_patchlevel '371' do; end
|
69
|
+
when_ruby_engine 'rbx' do; end
|
70
|
+
when_platform :osx do; end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
# 1.8.7
|
75
|
+
puts Example.ping # => 'pong'
|
76
|
+
puts Example.new.array_to_string # => "123"
|
77
|
+
|
78
|
+
# 2.0.0
|
79
|
+
puts Example.ping # => 'gnop'
|
80
|
+
puts Example.new.array_to_string # => "123"
|
81
|
+
```
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
1. Fork it
|
86
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
87
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
88
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
89
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require "rake/testtask"
|
4
|
+
|
5
|
+
task :default => :test
|
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
|
+
}
|
15
|
+
end
|
data/lib/toki.rb
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'rbconfig'
|
2
|
+
require File.expand_path('../toki/version', __FILE__)
|
3
|
+
|
4
|
+
module Toki
|
5
|
+
|
6
|
+
def when_ruby(options, &block)
|
7
|
+
raise ArgumentError, "wrong argument type #{options.class} (expected Hash)" unless options.kind_of?(Hash)
|
8
|
+
apply_behaviour(&block) if options.all? do | key, value |
|
9
|
+
case key
|
10
|
+
when :version
|
11
|
+
when_ruby_version(value)
|
12
|
+
when :jruby_version
|
13
|
+
when_jruby_version(value)
|
14
|
+
when :macruby_version
|
15
|
+
when_macruby_version(value)
|
16
|
+
when :rbx_version
|
17
|
+
when_rbx_version(value)
|
18
|
+
when :engine
|
19
|
+
when_ruby_engine(value)
|
20
|
+
when :patchlevel
|
21
|
+
when_ruby_patchlevel(value)
|
22
|
+
when :platform
|
23
|
+
when_platform(value)
|
24
|
+
else
|
25
|
+
raise ArgumentError, "unsupported key #{key} for when_ruby options"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# JRUBY_VERSION
|
31
|
+
def when_jruby_version(pattern, &block)
|
32
|
+
if defined?(JRUBY_VERSION)
|
33
|
+
if pattern_matches_with?(JRUBY_VERSION, pattern)
|
34
|
+
apply_behaviour(&block)
|
35
|
+
else
|
36
|
+
false
|
37
|
+
end
|
38
|
+
else
|
39
|
+
# JRUBY_VERSION not defined
|
40
|
+
false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# MACRUBY_VERSION
|
45
|
+
def when_macruby_version(pattern, &block)
|
46
|
+
if defined?(MACRUBY_VERSION)
|
47
|
+
if pattern_matches_with?(MACRUBY_VERSION, pattern)
|
48
|
+
apply_behaviour(&block)
|
49
|
+
else
|
50
|
+
false
|
51
|
+
end
|
52
|
+
else
|
53
|
+
# MACRUBY_VERSION not defined
|
54
|
+
false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Rubinius::VERSION
|
59
|
+
def when_rbx_version(pattern, &block)
|
60
|
+
if defined?(Rubinius)
|
61
|
+
if pattern_matches_with?(Rubinius::VERSION, pattern)
|
62
|
+
apply_behaviour(&block)
|
63
|
+
else
|
64
|
+
false
|
65
|
+
end
|
66
|
+
else
|
67
|
+
# Rubinius not defined
|
68
|
+
false
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# RUBY_VERSION
|
73
|
+
def when_ruby_version(pattern, &block)
|
74
|
+
if pattern_matches_with?(RUBY_VERSION, pattern)
|
75
|
+
apply_behaviour(&block)
|
76
|
+
else
|
77
|
+
false
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# RUBY_PATCHLEVEL
|
82
|
+
def when_ruby_patchlevel(pattern, &block)
|
83
|
+
if pattern_matches_with?(RUBY_PATCHLEVEL, pattern)
|
84
|
+
apply_behaviour(&block)
|
85
|
+
else
|
86
|
+
false
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# RUBY_ENGINE
|
91
|
+
def when_ruby_engine(pattern, &block)
|
92
|
+
# ruby 1.8.7 does not have RUBY_ENGINE constant
|
93
|
+
ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
|
94
|
+
if pattern_matches_with?(ruby_engine, pattern)
|
95
|
+
apply_behaviour(&block)
|
96
|
+
else
|
97
|
+
false
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# RUBY_PLATFORM
|
102
|
+
def when_platform(pattern, &block)
|
103
|
+
case pattern
|
104
|
+
when Symbol
|
105
|
+
if pattern == detect_os
|
106
|
+
apply_behaviour(&block)
|
107
|
+
else
|
108
|
+
false
|
109
|
+
end
|
110
|
+
else
|
111
|
+
if pattern_matches_with?(RbConfig::CONFIG['host_os'], pattern)
|
112
|
+
apply_behaviour(&block)
|
113
|
+
else
|
114
|
+
false
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
def detect_os
|
122
|
+
case RbConfig::CONFIG['host_os']
|
123
|
+
when /mswin|msys|mingw|windows|win32|cygwin|bccwin|wince|emc/i
|
124
|
+
:windows
|
125
|
+
when /darwin|mac os/i
|
126
|
+
:osx
|
127
|
+
when /linux/i
|
128
|
+
:linux
|
129
|
+
when /solaris|sunos|bsd/i
|
130
|
+
:unix
|
131
|
+
else
|
132
|
+
:unknown
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def pattern_matches_with?(constant, pattern)
|
137
|
+
constant.to_s =~ case pattern
|
138
|
+
when String, Integer
|
139
|
+
Regexp.union(pattern.to_s)
|
140
|
+
when Regexp
|
141
|
+
pattern
|
142
|
+
else
|
143
|
+
raise ArgumentError, "wrong argument type #{pattern.class} (expected String or Regexp)"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# apply behaviour to target class
|
148
|
+
def apply_behaviour(&block)
|
149
|
+
class_eval(&block) if block_given?
|
150
|
+
true
|
151
|
+
end
|
152
|
+
|
153
|
+
end # Toki
|
data/lib/toki/version.rb
ADDED
data/test/test_toki.rb
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.expand_path('../../lib/toki', __FILE__)
|
3
|
+
|
4
|
+
class TestToki < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
# do nothing
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
# do nothing
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_ruby_version
|
15
|
+
klass = Class.new do
|
16
|
+
extend Toki
|
17
|
+
def hello
|
18
|
+
nil
|
19
|
+
end
|
20
|
+
when_ruby :version => RUBY_VERSION do
|
21
|
+
def hello
|
22
|
+
"world"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
instance = klass.new
|
27
|
+
assert_equal "world", instance.hello
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_jruby_version
|
31
|
+
unless (jruby_version_defined = defined?(JRUBY_VERSION))
|
32
|
+
Object.const_set(:JRUBY_VERSION, '1.7.4')
|
33
|
+
end
|
34
|
+
klass = Class.new do
|
35
|
+
extend Toki
|
36
|
+
def hello
|
37
|
+
nil
|
38
|
+
end
|
39
|
+
when_ruby :jruby_version => '1.7.4' do
|
40
|
+
def hello
|
41
|
+
"world"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
instance = klass.new
|
46
|
+
assert_equal "world", instance.hello
|
47
|
+
ensure
|
48
|
+
Object.send(:remove_const, :JRUBY_VERSION) unless jruby_version_defined
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_rbx_version
|
52
|
+
unless (rbx_defined = defined?(Rubinius))
|
53
|
+
klass = Class.new
|
54
|
+
klass.const_set(:VERSION, '2.1.1')
|
55
|
+
Object.const_set(:Rubinius, klass)
|
56
|
+
end
|
57
|
+
klass = Class.new do
|
58
|
+
extend Toki
|
59
|
+
def hello
|
60
|
+
nil
|
61
|
+
end
|
62
|
+
when_ruby :rbx_version => '2.1.1' do
|
63
|
+
def hello
|
64
|
+
"world"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
instance = klass.new
|
69
|
+
assert_equal "world", instance.hello
|
70
|
+
ensure
|
71
|
+
Object.send(:remove_const, :Rubinius) unless rbx_defined
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_macruby_version
|
75
|
+
unless (macruby_version_defined = defined?(MACRUBY_VERSION))
|
76
|
+
Object.const_set(:MACRUBY_VERSION, '0.12')
|
77
|
+
end
|
78
|
+
klass = Class.new do
|
79
|
+
extend Toki
|
80
|
+
def hello
|
81
|
+
nil
|
82
|
+
end
|
83
|
+
when_ruby :macruby_version => '0.12' do
|
84
|
+
def hello
|
85
|
+
"world"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
instance = klass.new
|
90
|
+
assert_equal "world", instance.hello
|
91
|
+
ensure
|
92
|
+
Object.send(:remove_const, :MACRUBY_VERSION) unless macruby_version_defined
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_ruby_patchlevel
|
96
|
+
klass = Class.new do
|
97
|
+
extend Toki
|
98
|
+
def hello
|
99
|
+
nil
|
100
|
+
end
|
101
|
+
when_ruby :patchlevel => RUBY_PATCHLEVEL do
|
102
|
+
def hello
|
103
|
+
"world"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
instance = klass.new
|
108
|
+
assert_equal "world", instance.hello
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_ruby_engine
|
112
|
+
ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
|
113
|
+
klass = Class.new do
|
114
|
+
extend Toki
|
115
|
+
def hello
|
116
|
+
nil
|
117
|
+
end
|
118
|
+
when_ruby :engine => ruby_engine do
|
119
|
+
def hello
|
120
|
+
"world"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
instance = klass.new
|
125
|
+
assert_equal "world", instance.hello
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_platform
|
129
|
+
klass = Class.new do
|
130
|
+
extend Toki
|
131
|
+
def hello
|
132
|
+
nil
|
133
|
+
end
|
134
|
+
when_ruby :platform => RbConfig::CONFIG['host_os'] do
|
135
|
+
def hello
|
136
|
+
"world"
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
instance = klass.new
|
141
|
+
assert_equal "world", instance.hello
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_class_methods
|
145
|
+
klass = Class.new do
|
146
|
+
extend Toki
|
147
|
+
def self.hello
|
148
|
+
nil
|
149
|
+
end
|
150
|
+
when_ruby :version => Regexp.union(RUBY_VERSION) do
|
151
|
+
class << self
|
152
|
+
def hello
|
153
|
+
"world"
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
assert_equal "world", klass.hello
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
data/toki.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/toki/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jussi Koljonen"]
|
6
|
+
gem.email = ["jussi.koljonen@gmail.com"]
|
7
|
+
gem.description = %q{Toki is a DSL that helps you to implement Ruby version, patchlevel, engine and platform specific code}
|
8
|
+
gem.summary = %q{Toki is a DSL that helps you to implement Ruby version, patchlevel, engine and platform specific code}
|
9
|
+
gem.homepage = "https://github.com/juskoljo/toki"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "toki"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Toki::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: toki
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jussi Koljonen
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2013-10-26 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Toki is a DSL that helps you to implement Ruby version, patchlevel, engine and platform specific code
|
22
|
+
email:
|
23
|
+
- jussi.koljonen@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- LICENSE
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- lib/toki.rb
|
37
|
+
- lib/toki/version.rb
|
38
|
+
- test/test_toki.rb
|
39
|
+
- toki.gemspec
|
40
|
+
homepage: https://github.com/juskoljo/toki
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.8.25
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Toki is a DSL that helps you to implement Ruby version, patchlevel, engine and platform specific code
|
73
|
+
test_files:
|
74
|
+
- test/test_toki.rb
|