mario 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  mario
2
2
  =====
3
3
 
4
- Mario is super small library with only two standard library dependencies to help you figure out what platform you're on.
5
-
4
+ Mario is super small library to help you with getting from platform to platform with ease!
6
5
 
7
6
  platform
8
7
  --------
@@ -22,6 +21,99 @@ If for some reason you opperating system isn't represented please alert me and/o
22
21
 
23
22
  Mario::Platform.forced = Mario::Platform::Linux
24
23
 
24
+
25
+ targeted methods
26
+ ----------------
27
+ In version 0.1 of Mario, the ability to define platform specific instance method implementations has been added via the class method `platform`. All you have to do is extend your class with `Mario::Tools`.
28
+
29
+ class MyClass
30
+ extend Mario::Tools
31
+
32
+ platform :windows do
33
+ def foo
34
+ "I will only work for Windows!"
35
+ end
36
+ end
37
+
38
+ platform :linux do
39
+ def foo
40
+ "I will only work for Linux!"
41
+ end
42
+ end
43
+ end
44
+
45
+ Mario::Platform.forced = Mario::Platform::Windows7
46
+ MyClass.new.foo # => "I will only work for Windows!"
47
+
48
+ Mario::Platform.forced = Mario::Platform::Linux
49
+ MyClass.new.foo # => "I will only work for Linux!"
50
+
51
+ Alternatively, if you don't need the flexibility of actual method definitions or multiple methods, you can do the following:
52
+
53
+ class MyClass
54
+ extend Mario::Tools
55
+
56
+ platform :windows, :foo do |param|
57
+ "This is my param on Windows: #{param}"
58
+ end
59
+
60
+ platform :linux, :foo do |param|
61
+ "This is my param on Linux: #{param}"
62
+ end
63
+ end
64
+
65
+
66
+ platform value maps
67
+ -------------------
68
+ Sometimes you don't need an actual method and you just want to define a platform spefic value for something. With Mario you can do the following:
69
+
70
+ class MyClass
71
+ extend Mario::Tools
72
+ attr_accessor :my_ivar
73
+
74
+ def initialize
75
+ @my_ivar = platform_value_map { :windows => 'Windows!', :linux => 'Linux!' }
76
+ end
77
+ end
78
+
79
+ Mario::Platform.forced = Mario::Platform::Windows7
80
+ MyClass.new.my_ivar # => 'Windows!'
81
+
82
+ Mario::Platform.forced = Mario::Platform::Linux
83
+ MyClass.new.my_ivar # => 'Linux!'
84
+
85
+ platform symbol values
86
+ ----------------------
87
+ Any class defined in Mario::Platform.targets can be used as symbol for `platform` and `platform_value_map` to target a given platform. The following are supported as of version 0.1
88
+
89
+ >> Mario::Platform.targets.map{ |t| Mario::Platform.klass_to_method(t).to_sym }
90
+ => [:cygwin, :linux, :bsd, :solaris, :tiger, :leopard, :snowleopard, :darwin, :windows7, :windowsnt]
91
+
92
+ testing
93
+ -------
94
+ When testing your app you'll want to force the platform that mario reports to test your platform specific methods. In order to accomplish this make sure to defer the method definition done by the `platform` class method in the following manner.
95
+
96
+ Mario::Tools.defer_method_definition!
97
+ require 'my_class'
98
+
99
+ This will allow you to force the platform and THEN define the methods on your classes manually so that you can test them for various platforms.
100
+
101
+ Mario::Platform.forced = Mario::Platform::Windows7
102
+ MyClass.define_platform_methods!
103
+ MyClass.new.foo # => "Windows!"
104
+
105
+ The blocks are stored so that `define_platform_methods!` can be called many times after forcing the platform to provide different results.
106
+
107
+ Mario::Platform.forced = Mario::Platform::Windows7
108
+ MyClass.define_platform_methods!
109
+ MyClass.new.foo # => "Windows!"
110
+
111
+ Mario::Platform.forced = Mario::Platform::Linux
112
+ MyClass.define_platform_methods!
113
+ MyClass.new.foo # => "Linux!"
114
+
115
+ *IMPORTANT* The above does NOT apply to `platform_value_map`, if you want the above functionality its best to assign the values from functions defined in the above manner
116
+
25
117
  hats
26
118
  ----
27
119
  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:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -1,6 +1,6 @@
1
1
  module Mario
2
2
  module Tools
3
- @@method_blocks = {}
3
+ @@class_method_blocks = {}
4
4
  @@defer_method_definition = false
5
5
 
6
6
  def defer_method_definition
@@ -18,8 +18,9 @@ module Mario
18
18
  module_function :defer_method_definition, :defer_method_definition=, :defer_method_definition!
19
19
 
20
20
  def platform(name, method=nil, &block)
21
- @@method_blocks[name] = method ? [ method , block ] : block
22
- define_platform_methods! if !defer_method_definition
21
+ @@class_method_blocks[self] ||= {}
22
+ @@class_method_blocks[self][name] = method ? [ method , block ] : block
23
+ define_platform_methods! unless defer_method_definition
23
24
  end
24
25
 
25
26
  def platform_value_map(map={})
@@ -29,7 +30,7 @@ module Mario
29
30
  end
30
31
 
31
32
  def define_platform_methods!
32
- @@method_blocks.each do |name, value|
33
+ @@class_method_blocks[self].each do |name, value|
33
34
  if Platform.check_symbol(name)
34
35
  if value.is_a?(Proc)
35
36
  self.class_eval &value
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mario}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
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-04-15}
12
+ s.date = %q{2010-04-16}
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 = [
@@ -59,6 +59,27 @@ class TestTools < Test::Unit::TestCase
59
59
  AlternateMockClass.new.fik
60
60
  end
61
61
  end
62
+
63
+ should "not define methods on seperate classes from different platforms" do
64
+ # Note requires new classes for testing this particular case
65
+ class MyClass
66
+ extend Mario::Tools
67
+ platform :windows, :foo do
68
+ puts "foo"
69
+ end
70
+ end
71
+
72
+ class MyClass2
73
+ extend Mario::Tools
74
+ platform :darwin, :foo do
75
+ puts "foo"
76
+ end
77
+ end
78
+
79
+ assert_raise NoMethodError do
80
+ MyClass2.new.foo
81
+ end
82
+ end
62
83
  end
63
84
 
64
85
  context "deferred addition of methods" do
@@ -78,6 +99,17 @@ class TestTools < Test::Unit::TestCase
78
99
  assert_method :faz
79
100
  end
80
101
 
102
+ should "overwrite previous methods via blocks" do
103
+ MockClass.platform(:windows, :fink) { true }
104
+ MockClass.platform(:linux, :fink) { false }
105
+ assert_overwritten(:fink)
106
+ end
107
+
108
+ should "overwrite previous methods" do
109
+ MockClass.platform(:windows) { def fizzle; true; end }
110
+ MockClass.platform(:linux) { def fizzle; false; end }
111
+ assert_overwritten(:fizzle)
112
+ end
81
113
  end
82
114
 
83
115
  context "platform value maps" do
@@ -100,4 +132,13 @@ class TestTools < Test::Unit::TestCase
100
132
  MockClass.new.send(method)
101
133
  end
102
134
  end
135
+
136
+ def assert_overwritten(method)
137
+ Mario::Platform.forced = Mario::Platform::Windows7
138
+ MockClass.define_platform_methods!
139
+ assert MockClass.new.send(method)
140
+ Mario::Platform.forced = Mario::Platform::Linux
141
+ MockClass.define_platform_methods!
142
+ assert !MockClass.new.send(method)
143
+ end
103
144
  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.1.0
4
+ version: 0.1.1
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-04-15 00:00:00 -07:00
12
+ date: 2010-04-16 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency