mario 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ mario
2
+ =====
3
+
4
+ Mario is super small library with only two standard library dependencies to help you figure out what platform you're on.
5
+
6
+
7
+ platform
8
+ --------
9
+ Mario's main function is providing simple methods to match your current operating system or a group of operating systems. For example if you wanted to know if your code was excuting on some version of Windows
10
+
11
+ Mario::Platform.windows?
12
+
13
+ Or a Unix-like operating system
14
+
15
+ Mario::Platform.nix?
16
+
17
+ If you want to be more specific you can do that too
18
+
19
+ Mario::Platform.bsd? # or linux? or windows7? or darwin?
20
+
21
+ If for some reason you opperating system isn't represented please alert me and/or see the setup for hacking on Mario below, but in the meantime you can force which operating system you would like Mario to report
22
+
23
+ Mario::Platform.forced = Mario::Platform::Linux
24
+
25
+ hats
26
+ ----
27
+ Mario has different abilities with different hats, each hat is tied to the current operating system. To start Mario can only escape file paths properly for nix operating systems and there's still testing to be done on the windows version, but there will be more to come. To make use of Mario's version of his abilities on your platform you might do the following:
28
+
29
+ Mario::Platform.current.shell_escape_path('C:\Some Path\With Some Spaces.filext')
30
+
31
+ Its important to note that shell_escape_path is an actual method and not what Mario would do when confronted with an oncoming koopa shell.
32
+
33
+ hacking
34
+ -------
35
+
36
+ If you want to hack on mario, and I would grateful for any patches to for all those dark little corners that need light shone on them, just do the following from your project directory after forking and cloning:
37
+
38
+ $ bundle install
39
+ $ bundle lock
40
+ $ rake test
41
+
42
+ If all is well you're ready to roll.
43
+
44
+ license
45
+ -------
46
+
47
+ See the license file for more information.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.1.0
@@ -2,5 +2,8 @@
2
2
  require 'rbconfig'
3
3
  require 'logger'
4
4
  lib = File.dirname(__FILE__)
5
- %w{ mario/hats/nix mario/hats/windows mario/platform }.each { |file| require File.expand_path(file, lib) }
5
+ %w{
6
+ mario/util/object mario/hats/nix mario/hats/windows
7
+ mario/platform mario/tools
8
+ }.each { |file| require File.expand_path(file, lib) }
6
9
 
@@ -10,7 +10,7 @@ module Mario
10
10
  #
11
11
  # @return [Array[Class]]
12
12
  def nix_group
13
- [Cygwin, Linux, BSD, Solaris, Darwin]
13
+ [Cygwin, Linux, BSD, Solaris] + darwin_group
14
14
  end
15
15
 
16
16
  # A list of windows operating system classes
@@ -20,74 +20,42 @@ module Mario
20
20
  [Windows7, WindowsNT]
21
21
  end
22
22
 
23
- # The union of the {nix_group} and {windows_group} operating system class sets
24
- #
23
+ # A list of the different Darwin Versions
24
+ #
25
25
  # @return [Array[Class]]
26
- def targets
27
- nix_group | windows_group
28
- end
29
-
30
- # Checks if the current platform is linux
31
- #
32
- # @return [true, false]
33
- def linux?
34
- check Linux
35
- end
36
-
37
- # Checks if the current platform is osx
38
- #
39
- # @return [true, false]
40
- def darwin?
41
- check Darwin
26
+ def darwin_group
27
+ # NOTE ordering required, but removal of Darwin class constant
28
+ # procludes forcing os as Darwin
29
+ [Tiger, Leopard, SnowLeopard, Darwin]
42
30
  end
43
31
 
44
- # Checks if the current platform is solaris
32
+ # The union of the {nix_group} and {windows_group} operating system class sets, each operating system
33
+ # test method ( ie linux? ) is built from this set of class constants
45
34
  #
46
- # @return [true, false]
47
- def solaris?
48
- check Solaris
49
- end
50
-
51
- # Checks if the current platform is bsd
52
- #
53
- # @return [true, false]
54
- def bsd?
55
- check BSD
56
- end
57
-
58
- # Checks if the current platform is cygwin
59
- #
60
- # @return [true, false]
61
- def cygwin?
62
- check Cygwin
63
- end
64
-
65
- # Checks if the current platform is windows 7
66
- #
67
- # @return [true, false]
68
- def windows7?
69
- check Windows7
70
- end
71
-
72
- # Checks if the current platform is windows nt
73
- #
74
- # @return [true, false]
75
- def windowsnt?
76
- check WindowsNT
35
+ # @return [Array[Class]]
36
+ def targets
37
+ nix_group | windows_group
77
38
  end
78
39
 
79
40
  # Checks if the current platform is part of the {nix_group} and returns that class
80
41
  #
81
42
  # @return [Class]
82
43
  def nix?
83
- check_group nix_group
44
+ check_group(nix_group)
84
45
  end
85
46
 
86
47
  # Checks if the current platform is part of the {windows_group} and returns that class
87
48
  #
88
49
  # @return [Class]
89
50
  def windows?
90
- check_group windows_group
51
+ check_group(windows_group)
52
+ end
53
+
54
+ # Checks if the current platform is part of the {darwin_group} and returns that class
55
+ #
56
+ # @return [Class]
57
+ def darwin?
58
+ check_group(darwin_group)
91
59
  end
92
60
 
93
61
  # Checks a list of possible operating system classes to see if their target os strings match the {target_os} value
@@ -95,16 +63,14 @@ module Mario
95
63
  # @return [Class]
96
64
  def check_group(group)
97
65
  group.each do |klass|
98
- return klass if check klass
66
+ return klass if check(klass)
99
67
  end
100
68
  false
101
69
  end
102
70
 
103
- alias_method :osx?, :darwin?
104
-
105
71
  # Uses the forced class target os string if provided otherwise uses the target_os rbconfig hash element
106
72
  #
107
- # @return [String]
73
+ # @return [String]
108
74
  def target_os
109
75
  @@forced ? @@forced.target : Config::CONFIG['target_os']
110
76
  end
@@ -122,7 +88,7 @@ module Mario
122
88
  @@current=nil
123
89
  @@forced=klass
124
90
  logger.warn(<<-msg)
125
- Mario::Platform.target_os will now report as '#{target_os}' and #{klass} will be used for all functionality including operating system checks and hat based functionality (See Mario::Hats for more information)
91
+ Mario::Platform.target_os will now report as '#{target_os}' and #{klass} will be used for all functionality including operating system checks, platform blocks, and hat based functionality
126
92
  msg
127
93
  end
128
94
 
@@ -148,19 +114,33 @@ msg
148
114
  def current
149
115
  # Search for the current target os
150
116
  current_target_klass = check_group(targets)
117
+ unless current_target_klass
118
+ raise OperatingSystemNotRecognized.new(<<-msg)
119
+
120
+ The current target os #{target_os}, is not recognized by Mario, please use/change Mario::Platform.forced to emulate a supported operating system."
121
+
122
+ msg
123
+ end
151
124
  @@current ||= current_target_klass.new
152
125
  @@current
153
126
  end
127
+
128
+ def klass_to_method(klass)
129
+ klass.to_s.downcase.split('::').last
130
+ end
131
+
132
+ def check_symbol(name)
133
+ send(name.to_s + '?')
134
+ end
154
135
  end
155
136
 
156
- # Any additional fucniontality and they should be moved to a lib/platforms/<OS>.rb
137
+ # NOTE Any additional functionality and they should be moved to a lib/platforms/<OS>.rb
157
138
  class Cygwin
158
139
  include Hats::Nix
159
140
 
160
141
  def self.target
161
142
  'cygwin'
162
143
  end
163
-
164
144
  end
165
145
 
166
146
  class Darwin
@@ -171,6 +151,24 @@ msg
171
151
  end
172
152
  end
173
153
 
154
+ class Leopard < Darwin
155
+ def self.target
156
+ super + "9"
157
+ end
158
+ end
159
+
160
+ class Tiger < Darwin
161
+ def self.target
162
+ super + "8"
163
+ end
164
+ end
165
+
166
+ class SnowLeopard < Darwin
167
+ def self.target
168
+ super + "10"
169
+ end
170
+ end
171
+
174
172
  class Linux
175
173
  include Hats::Nix
176
174
 
@@ -195,7 +193,6 @@ msg
195
193
  end
196
194
  end
197
195
 
198
-
199
196
  class Windows7
200
197
  include Hats::Windows
201
198
 
@@ -211,5 +208,13 @@ msg
211
208
  'mswin'
212
209
  end
213
210
  end
211
+
212
+ targets.each do |klass|
213
+ define_class_method(klass_to_method(klass) + '?') do
214
+ check(klass)
215
+ end
216
+ end
217
+
218
+ class OperatingSystemNotRecognized < Exception; end
214
219
  end
215
220
  end
@@ -0,0 +1,43 @@
1
+ module Mario
2
+ module Tools
3
+ @@method_blocks = {}
4
+ @@defer_method_definition = false
5
+
6
+ def defer_method_definition
7
+ @@defer_method_definition
8
+ end
9
+
10
+ def defer_method_definition=(val)
11
+ @@defer_method_definition=val
12
+ end
13
+
14
+ def defer_method_definition!
15
+ @@defer_method_definition=true
16
+ end
17
+
18
+ module_function :defer_method_definition, :defer_method_definition=, :defer_method_definition!
19
+
20
+ def platform(name, method=nil, &block)
21
+ @@method_blocks[name] = method ? [ method , block ] : block
22
+ define_platform_methods! if !defer_method_definition
23
+ end
24
+
25
+ def platform_value_map(map={})
26
+ map.each do |key, value|
27
+ return value if Platform.check_symbol(key)
28
+ end
29
+ end
30
+
31
+ def define_platform_methods!
32
+ @@method_blocks.each do |name, value|
33
+ if Platform.check_symbol(name)
34
+ if value.is_a?(Proc)
35
+ self.class_eval &value
36
+ else
37
+ define_method value.first, &value.last
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ # Courtesy of Jay Fields and
2
+ # http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
3
+ class Object # :nodoc
4
+ def define_class_method(name, &blk)
5
+ (class << self; self; end).instance_eval { define_method name, &blk }
6
+ end
7
+ end
@@ -5,33 +5,36 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mario}
8
- s.version = "0.0.6"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Bender"]
12
- s.date = %q{2010-03-20}
12
+ s.date = %q{2010-04-15}
13
13
  s.description = %q{Mario is a collection of utilities for dealing with platform specific issues}
14
14
  s.email = %q{john.m.bender@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.rdoc"
17
+ "README.md"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
22
  "LICENSE",
23
- "README.rdoc",
23
+ "README.md",
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "lib/mario.rb",
27
27
  "lib/mario/hats/nix.rb",
28
28
  "lib/mario/hats/windows.rb",
29
29
  "lib/mario/platform.rb",
30
+ "lib/mario/tools.rb",
31
+ "lib/mario/util/object.rb",
30
32
  "mario.gemspec",
31
33
  "test/helper.rb",
32
34
  "test/mario/hats/test_nix.rb",
33
35
  "test/mario/hats/test_windows.rb",
34
- "test/mario/test_plaform.rb"
36
+ "test/mario/test_plaform.rb",
37
+ "test/mario/test_tools.rb"
35
38
  ]
36
39
  s.homepage = %q{http://github.com/johnbender/mario}
37
40
  s.rdoc_options = ["--charset=UTF-8"]
@@ -42,7 +45,8 @@ Gem::Specification.new do |s|
42
45
  "test/helper.rb",
43
46
  "test/mario/hats/test_nix.rb",
44
47
  "test/mario/hats/test_windows.rb",
45
- "test/mario/test_plaform.rb"
48
+ "test/mario/test_plaform.rb",
49
+ "test/mario/test_tools.rb"
46
50
  ]
47
51
 
48
52
  if s.respond_to? :specification_version then
@@ -22,5 +22,3 @@ class Test::Unit::TestCase
22
22
  logger.stubs(:warn)
23
23
  end
24
24
  end
25
-
26
-
@@ -1,3 +1,4 @@
1
+
1
2
  require 'helper'
2
3
 
3
4
  class TestPlatform < Test::Unit::TestCase
@@ -100,6 +101,40 @@ class TestPlatform < Test::Unit::TestCase
100
101
  assert !Mario::Platform.check_group([mock_os_klass, mock_os_klass])
101
102
  end
102
103
  end
104
+
105
+ context "darwin versions" do
106
+ should "return true for a given darwin version when the os target string is darwin plus a number" do
107
+ target_postfix_darwin(10.0)
108
+ assert Mario::Platform.snowleopard?
109
+ target_postfix_darwin(9.0)
110
+ assert Mario::Platform.leopard?
111
+ target_postfix_darwin(8.0)
112
+ assert Mario::Platform.tiger?
113
+ end
114
+
115
+ should "return true for darwin if any of the darwin group classes are the current platform" do
116
+ Mario::Platform.darwin_group.each do |klass|
117
+ Mario::Platform.forced = klass
118
+ assert Mario::Platform.darwin?
119
+ end
120
+ end
121
+ end
122
+
123
+ context "checking via symbols" do
124
+ should "call the symbol with a question mark prefix method" do
125
+ Mario::Platform.expects(:windows?)
126
+ Mario::Platform.check_symbol(:windows)
127
+ end
128
+ end
129
+
130
+ context "operating system is not reconized" do
131
+ should "raise an exception" do
132
+ Mario::Platform.expects(:check_group).returns(false)
133
+ assert_raise Mario::Platform::OperatingSystemNotRecognized do
134
+ Mario::Platform.current
135
+ end
136
+ end
137
+ end
103
138
  end
104
139
 
105
140
  def check_nix(force)
@@ -121,4 +156,8 @@ class TestPlatform < Test::Unit::TestCase
121
156
  mock.stubs(:target).returns(str)
122
157
  mock
123
158
  end
159
+
160
+ def target_postfix_darwin(with)
161
+ Mario::Platform.expects(:target_os).returns("darwin#{with}")
162
+ end
124
163
  end
@@ -0,0 +1,103 @@
1
+ require 'helper'
2
+
3
+ class MockClass
4
+ extend Mario::Tools
5
+ end
6
+
7
+ class AlternateMockClass
8
+ extend Mario::Tools
9
+ end
10
+
11
+ class TestTools < Test::Unit::TestCase
12
+ context "Mario tools" do
13
+ setup do
14
+ Mario::Platform.forced = Mario::Platform::Windows7
15
+ Mario::Tools.defer_method_definition = false
16
+ end
17
+
18
+ context "platform specific instance methods" do
19
+ setup do
20
+ MockClass.platform :windows do
21
+ def foo
22
+ true
23
+ end
24
+ end
25
+
26
+ MockClass.platform :nix do
27
+ def bar
28
+ true
29
+ end
30
+ end
31
+ end
32
+
33
+ should "be defined correctly for the platform" do
34
+ assert_method :foo
35
+ end
36
+
37
+ should "not be defined for other platforms" do
38
+ assert_no_method :bar
39
+ end
40
+ end
41
+
42
+ context "platform specific instance methods from blocks" do
43
+ setup do
44
+ MockClass.platform(:windows, :baz) { true }
45
+ MockClass.platform(:nix, :bak) {}
46
+ end
47
+
48
+ should "be defined correctly for the platform" do
49
+ assert_method :baz
50
+ end
51
+
52
+ should "not be defined for other platforms" do
53
+ assert_no_method :bak
54
+ end
55
+
56
+ should "not define methods on seperate classes" do
57
+ MockClass.platform(:windows, :fik) { true }
58
+ assert_raise NoMethodError do
59
+ AlternateMockClass.new.fik
60
+ end
61
+ end
62
+ end
63
+
64
+ context "deferred addition of methods" do
65
+ setup do
66
+ Mario::Tools.defer_method_definition!
67
+ end
68
+
69
+ should "prevent addition of methods until explicitly requested" do
70
+ MockClass.platform(:windows, :fiz) {}
71
+ assert_no_method :fiz
72
+ end
73
+
74
+ should "add methods when called explicitly" do
75
+ MockClass.platform(:windows, :faz) { true }
76
+ assert_no_method :faz
77
+ MockClass.define_platform_methods!
78
+ assert_method :faz
79
+ end
80
+
81
+ end
82
+
83
+ context "platform value maps" do
84
+ should "return the value associated with the given platform" do
85
+ map = { :windows => true, :nix => false }
86
+ Mario::Platform.forced = Mario::Platform::Windows7
87
+ assert MockClass.platform_value_map(map)
88
+ Mario::Platform.forced = Mario::Platform::Darwin
89
+ assert !MockClass.platform_value_map(map)
90
+ end
91
+ end
92
+ end
93
+
94
+ def assert_method(method)
95
+ assert MockClass.new.send(method)
96
+ end
97
+
98
+ def assert_no_method(method)
99
+ assert_raise NoMethodError do
100
+ MockClass.new.send(method)
101
+ end
102
+ end
103
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mario
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Bender
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-20 00:00:00 -07:00
12
+ date: 2010-04-15 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -40,23 +40,26 @@ extensions: []
40
40
 
41
41
  extra_rdoc_files:
42
42
  - LICENSE
43
- - README.rdoc
43
+ - README.md
44
44
  files:
45
45
  - .document
46
46
  - .gitignore
47
47
  - LICENSE
48
- - README.rdoc
48
+ - README.md
49
49
  - Rakefile
50
50
  - VERSION
51
51
  - lib/mario.rb
52
52
  - lib/mario/hats/nix.rb
53
53
  - lib/mario/hats/windows.rb
54
54
  - lib/mario/platform.rb
55
+ - lib/mario/tools.rb
56
+ - lib/mario/util/object.rb
55
57
  - mario.gemspec
56
58
  - test/helper.rb
57
59
  - test/mario/hats/test_nix.rb
58
60
  - test/mario/hats/test_windows.rb
59
61
  - test/mario/test_plaform.rb
62
+ - test/mario/test_tools.rb
60
63
  has_rdoc: true
61
64
  homepage: http://github.com/johnbender/mario
62
65
  licenses: []
@@ -90,3 +93,4 @@ test_files:
90
93
  - test/mario/hats/test_nix.rb
91
94
  - test/mario/hats/test_windows.rb
92
95
  - test/mario/test_plaform.rb
96
+ - test/mario/test_tools.rb
@@ -1,15 +0,0 @@
1
- = mario
2
-
3
- Mario provides some very simple tools for figuring out which platform you're on and, depending on your needs, some for helping you adapt.
4
-
5
- == Note on Patches/Pull Requests
6
-
7
- * Fork the project.
8
- * `bundle install`
9
- * `bundle lock`
10
- * `rake test`
11
- * Make your feature addition or bug fix.
12
-
13
- == Copyright
14
-
15
- Copyright (c) 2010 John Bender. See LICENSE for details.