platypus 1.0.2 → 1.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a5a8cc9e4c9edf9bb0f2103eb75986e4db58a520ad16989883411fa55be0fde3
4
+ data.tar.gz: bd9f3a59f53981bd6434b17aad52750a3c52509bc1bd227fadd804e4f0d6117b
5
+ SHA512:
6
+ metadata.gz: 2c2b06fb3ab9056d95638a03e86d1e208c58384958fd18385fa54248c2f25566d27b8dde8714c5fef8845078f95cc8e95a8c95be3c85e41bf50d84c559fe88c6
7
+ data.tar.gz: 833a810527e7421a3ec6536ab6ec2856169580048ed812d8ca32f58b268c19f0e1aeb84eefea4db491add05d6f1e31176d18efe1a2d5e59a9d7ab6641dca11ba
@@ -1,6 +1,19 @@
1
- = Release History
1
+ # Release History
2
2
 
3
- == 1.0.2 / 2011-10-22
3
+ ## 1.1.0 / 2026-04-03
4
+
5
+ Maintenance release. Modernized project tooling.
6
+
7
+ Changes:
8
+
9
+ * Replace custom Indexer system with standard gemspec.
10
+ * Replace Travis CI with GitHub Actions.
11
+ * Add Rakefile.
12
+ * Update URLs to HTTPS.
13
+ * Clean up obsolete files and .gitignore.
14
+
15
+
16
+ ## 1.0.2 / 2011-10-22
4
17
 
5
18
  This release fixes a bug that preventd Kernel#case? from being defined.
6
19
 
@@ -9,7 +22,7 @@ Changes:
9
22
  * Remove Kernel#case? def condition [bug].
10
23
 
11
24
 
12
- == 1.0.1 / 2011-10-19
25
+ ## 1.0.1 / 2011-10-19
13
26
 
14
27
  This release simply refreshes the project build script.
15
28
  Functionality hasn't changed at all.
@@ -19,7 +32,7 @@ Changes:
19
32
  * Administrative details only.
20
33
 
21
34
 
22
- == 1.0.0 / 2009-07-07
35
+ ## 1.0.0 / 2009-07-07
23
36
 
24
37
  This is the initial stand-alone release of Platypus.
25
38
 
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ License:: BSD-2-Clause
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice,
7
+ this list of conditions and the following disclaimer.
8
+
9
+ 2. Redistributions in binary form must reproduce the above copyright
10
+ notice, this list of conditions and the following disclaimer in the
11
+ documentation and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
14
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
15
+ AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
16
+ COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
17
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
20
+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
22
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # Platypus
2
+
3
+ [Source Code](https://github.com/rubyworks/platypus) |
4
+ [Report Issue](https://github.com/rubyworks/platypus/issues)
5
+
6
+ [![Gem Version](https://img.shields.io/gem/v/platypus.svg?style=flat)](https://rubygems.org/gems/platypus)
7
+ [![Build Status](https://github.com/rubyworks/platypus/actions/workflows/test.yml/badge.svg)](https://github.com/rubyworks/platypus/actions/workflows/test.yml)
8
+
9
+ <b>Platypus provides a generalized type conversion system,
10
+ method overloading and psuedo-classes for the Ruby programming
11
+ language.</b>
12
+
13
+
14
+ ## Overview
15
+
16
+ Type conversion work like a rational duck might expect.
17
+
18
+ ```ruby
19
+ "1234".to(Float) => 1234.0 (Float)
20
+
21
+ Time.from("6:30") => 1234.0 (Time)
22
+ ```
23
+
24
+ You can of course define your own.
25
+
26
+ ```ruby
27
+ class X
28
+ typecast String do |x|
29
+ "#{x}"
30
+ end
31
+ end
32
+ ```
33
+
34
+ To overload a method, mixin the Overloadable module and use the #overload (or #sig)
35
+ method to define new functionality based on a specified type interface.
36
+
37
+ ```ruby
38
+ class X
39
+ include Overloadable
40
+
41
+ def f
42
+ "f"
43
+ end
44
+
45
+ sig Integer
46
+ def f(i)
47
+ "f#{i}"
48
+ end
49
+
50
+ sig String, String
51
+ def f(s1, s2)
52
+ [s1, s2].join('+')
53
+ end
54
+ end
55
+
56
+ x = X.new
57
+
58
+ x.f #=> "f"
59
+ x.f(1) #=> "f1"
60
+ x.f("A","B") #=> "A+B"
61
+ ```
62
+
63
+ Finally, the Platypus gives you the Type superclass (aka pseudo-classes).
64
+
65
+ ```ruby
66
+ class KiloType < Type
67
+ x % 1000 == 0
68
+ end
69
+
70
+ KiloType === 1000
71
+ KiloType === 2000
72
+ ```
73
+
74
+ To learn more about using Platypus see the [Demonstrundum](http://rubyworks.github.com/platypus/docs/demo).
75
+
76
+
77
+ ## Installation
78
+
79
+ To install with RubyGems simply open a console and type:
80
+
81
+ $ gem install platypus
82
+
83
+ Or add it as a dependency to your Gemfile.
84
+
85
+ gem "platypus"
86
+
87
+ Old school site installation can be achieved with Setup.rb (gem install setup),
88
+ then download the tarball package and type:
89
+
90
+ $ tar -xvzf platypus-1.0.0.tgz
91
+ $ cd platypus-1.0.0
92
+ $ setup.rb all
93
+
94
+ Windows users use 'ruby setup.rb all'.
95
+
96
+
97
+ ## Authors
98
+
99
+ * Thomas Sawyer (trans)
100
+ * Jonas Pfenniger
101
+
102
+
103
+ ## Copying
104
+
105
+ Copyright (c) 2010 Rubyworks
106
+
107
+ This program is ditributed unser the terms of the *FreeBSD* license.
108
+
109
+ See LICENSE.txt file for details.
110
+
111
+
112
+ <br/><br/>
113
+
114
+ _ ___
115
+ / \ / \
116
+ \. |: cc| .---------.
117
+ (.|:,---, < Feel the \
118
+ (.|: \ c| \ POWER!!! /
119
+ (. y-' '--------'
120
+ \ _ /
121
+ m m
122
+
data/demo/01_intro.md ADDED
@@ -0,0 +1,2 @@
1
+ # Platypus
2
+
@@ -0,0 +1,29 @@
1
+ ## Type Casting
2
+
3
+ Require the casting library.
4
+
5
+ require 'platypus/typecast'
6
+
7
+ Define a couple of typecasts.
8
+
9
+ class ::String
10
+ typecast ::Regexp do |string|
11
+ /#{string}/
12
+ end
13
+ typecast ::Regexp, :multiline do |string|
14
+ /#{string}/m
15
+ end
16
+ end
17
+
18
+ ::String.typecast ::Integer do |string|
19
+ Integer(string)
20
+ end
21
+
22
+ See that they work.
23
+
24
+ ::Regexp.from("ABC").assert == /ABC/
25
+
26
+ And again.
27
+
28
+ "123".to(::Integer).assert == 123
29
+
data/demo/03_type.md ADDED
@@ -0,0 +1,33 @@
1
+ ## Pseudo-Types
2
+
3
+ Require the library.
4
+
5
+ require 'platypus/type'
6
+
7
+ Now we can create types which are psuedo-classes.
8
+
9
+ class KiloType < Type
10
+ condition do |x|
11
+ x.case? Integer
12
+ x.kind_of?(Integer)
13
+ x.respond_to?(:succ)
14
+ x > 1000
15
+ end
16
+ end
17
+
18
+ KiloType.assert === 2000
19
+ KiloType.refute === 999
20
+
21
+ Using the convenience #x method.
22
+
23
+ class MegaType < Type
24
+ x.case? Integer
25
+ x.kind_of?(Integer)
26
+ x.respond_to?(:succ)
27
+ x > 1000000
28
+ end
29
+
30
+ MegaType.refute === 999999
31
+ MegaType.assert === 20000000
32
+
33
+
@@ -0,0 +1,57 @@
1
+ ## Overloadable
2
+
3
+ The Overloadable mixin provides a means for overloading
4
+ methods based in method signature using an elegant syntax.
5
+ To demonstrate, we first need to load the library.
6
+
7
+ require 'platypus/overload'
8
+
9
+ Now we can define a class that utilizes it.
10
+
11
+ class X
12
+ include Overloadable
13
+
14
+ sig String, String
15
+
16
+ def x(str1, str2)
17
+ str1.assert.is_a?(String)
18
+ str2.assert.is_a?(String)
19
+ end
20
+
21
+ sig Integer, Integer
22
+
23
+ def x(int1, int2)
24
+ int1.assert.is_a?(Integer)
25
+ int2.assert.is_a?(Integer)
26
+ end
27
+ end
28
+
29
+ As you can see will placed assertions directly into our methods
30
+ definitions. We simply need to run an exmaple of each definition to
31
+ see that it worked.
32
+
33
+ x = X.new
34
+
35
+ x.x("Hello", "World")
36
+
37
+ x.x(100, 200)
38
+
39
+ But what happens if the signiture is not matched? Either an ArgumentError will
40
+ be raised.
41
+
42
+ expect ArgumentError do
43
+ x.x("Hello", 200)
44
+ end
45
+
46
+ Or it will fallback to a non-signiature definition, if one is defined.
47
+
48
+
49
+ class X
50
+ def x(obj1, obj2)
51
+ obj1.refute.is_a?(Integer)
52
+ obj2.refute.is_a?(String)
53
+ end
54
+ end
55
+
56
+ x.x("Hello", 200)
57
+
@@ -0,0 +1 @@
1
+ require 'ae'
@@ -49,7 +49,7 @@ end
49
49
 
50
50
  class Time #:nodoc:
51
51
  # This method will require the 'time.rb' Time extensions.
52
- typecast String do
52
+ typecast String do |string|
53
53
  require 'time'
54
54
  parse(string)
55
55
  end
@@ -1,3 +1,3 @@
1
1
  module Platypus
2
- VERSION = "1.0.2" #:erb: VERSION = "<%= version %>"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,87 +1,84 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: platypus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
5
- prerelease:
4
+ version: 1.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Thomas Sawyer
9
8
  - Jonas Pfenniger
10
- autorequire:
11
9
  bindir: bin
12
10
  cert_chain: []
13
- date: 2011-10-22 00:00:00.000000000 Z
11
+ date: 1980-01-02 00:00:00.000000000 Z
14
12
  dependencies:
15
13
  - !ruby/object:Gem::Dependency
16
- name: detroit
17
- requirement: &24356020 !ruby/object:Gem::Requirement
18
- none: false
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
19
16
  requirements:
20
- - - ! '>='
17
+ - - ">="
21
18
  - !ruby/object:Gem::Version
22
- version: '0'
19
+ version: '13'
23
20
  type: :development
24
21
  prerelease: false
25
- version_requirements: *24356020
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '13'
26
27
  - !ruby/object:Gem::Dependency
27
- name: qed
28
- requirement: &24385900 !ruby/object:Gem::Requirement
29
- none: false
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '5'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *24385900
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5'
37
41
  description: Provides a complete double-dispatch type conversion system, method overloadability
38
- and psuedo-classes.
39
- email:
42
+ and pseudo-classes.
43
+ email:
44
+ - transfire@gmail.com
40
45
  executables: []
41
46
  extensions: []
42
- extra_rdoc_files:
43
- - HISTORY.rdoc
44
- - SPEC.rdoc
45
- - README.rdoc
46
- - NOTES.rdoc
47
- - COPYING.rdoc
47
+ extra_rdoc_files: []
48
48
  files:
49
- - .ruby
50
- - .yardopts
49
+ - HISTORY.md
50
+ - LICENSE.txt
51
+ - README.md
52
+ - demo/01_intro.md
53
+ - demo/02_typecast.md
54
+ - demo/03_type.md
55
+ - demo/04_overload.md
56
+ - demo/applique/env.rb
57
+ - lib/platypus.rb
51
58
  - lib/platypus/core_ext.rb
52
59
  - lib/platypus/overload.rb
53
60
  - lib/platypus/type.rb
54
61
  - lib/platypus/typecast.rb
55
62
  - lib/platypus/version.rb
56
- - lib/platypus.rb
57
- - test/test_overload.rb
58
- - HISTORY.rdoc
59
- - SPEC.rdoc
60
- - README.rdoc
61
- - NOTES.rdoc
62
- - COPYING.rdoc
63
- homepage: http://rubyworks.github.com/platypus
64
- licenses: []
65
- post_install_message:
63
+ homepage: https://github.com/rubyworks/platypus
64
+ licenses:
65
+ - BSD-2-Clause
66
+ metadata: {}
66
67
  rdoc_options: []
67
68
  require_paths:
68
69
  - lib
69
70
  required_ruby_version: !ruby/object:Gem::Requirement
70
- none: false
71
71
  requirements:
72
- - - ! '>='
72
+ - - ">="
73
73
  - !ruby/object:Gem::Version
74
- version: '0'
74
+ version: '3.1'
75
75
  required_rubygems_version: !ruby/object:Gem::Requirement
76
- none: false
77
76
  requirements:
78
- - - ! '>='
77
+ - - ">="
79
78
  - !ruby/object:Gem::Version
80
79
  version: '0'
81
80
  requirements: []
82
- rubyforge_project:
83
- rubygems_version: 1.8.10
84
- signing_key:
85
- specification_version: 3
81
+ rubygems_version: 3.6.9
82
+ specification_version: 4
86
83
  summary: Riding on Types with Ruby
87
84
  test_files: []
data/.ruby DELETED
@@ -1,44 +0,0 @@
1
- ---
2
- source:
3
- - Profile
4
- authors:
5
- - name: Thomas Sawyer
6
- - name: Jonas Pfenniger
7
- copyrights:
8
- - holder: Thomas Sawyer
9
- year: '2004'
10
- license: BSD-2-Clause
11
- replacements: []
12
- alternatives: []
13
- requirements:
14
- - name: detroit
15
- groups:
16
- - build
17
- development: true
18
- - name: qed
19
- groups:
20
- - test
21
- development: true
22
- dependencies: []
23
- conflicts: []
24
- repositories:
25
- - uri: http://rubyworks.github.com/platypus.git
26
- scm: git
27
- name: upstream
28
- resources:
29
- home: http://rubyworks.github.com/platypus
30
- code: http://github.com/rubyworks/platypus
31
- mail: http://googlegroups/group/rubyworks-mailinglist
32
- extra: {}
33
- load_path:
34
- - lib
35
- revision: 0
36
- version: 1.0.2
37
- name: platypus
38
- title: Platypus
39
- summary: Riding on Types with Ruby
40
- created: '2004-01-01'
41
- description: Provides a complete double-dispatch type conversion system, method overloadability
42
- and psuedo-classes.
43
- organization: RubyWorks
44
- date: '2011-10-22'
data/.yardopts DELETED
@@ -1,5 +0,0 @@
1
- --private
2
- --readme README.rdoc
3
- lib
4
- -
5
- [A-Z]*.*
data/COPYING.rdoc DELETED
@@ -1,31 +0,0 @@
1
- = COPYRIGHT NOTICES
2
-
3
- == TPlatypus
4
-
5
- Copyright:: (c) 2004 Thomas Sawyer, Rubyworks
6
- License:: BSD-2-Clause
7
- Website:: http://rubyworks.github.com/platypus
8
-
9
- Copyright 2011 Thomas Sawyer. All rights reserved.
10
-
11
- Redistribution and use in source and binary forms, with or without
12
- modification, are permitted provided that the following conditions are met:
13
-
14
- 1. Redistributions of source code must retain the above copyright notice,
15
- this list of conditions and the following disclaimer.
16
-
17
- 2. Redistributions in binary form must reproduce the above copyright
18
- notice, this list of conditions and the following disclaimer in the
19
- documentation and/or other materials provided with the distribution.
20
-
21
- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
- COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28
- OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30
- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
-
data/NOTES.rdoc DELETED
@@ -1,14 +0,0 @@
1
- = Developer's Notes
2
-
3
-
4
- == 2011-10-19 | Name of Project
5
-
6
- This project used to be call Typecast. Maybe that was better than the "silly"
7
- Platypus name? But, then again, "typecast" doesn't quite cover everything this
8
- library does.
9
-
10
-
11
- == 2010-05-27 | Alias for +Overloadable#overload+
12
-
13
- I am not sure if I like +con+, +sig+ or +over+ better as a short alias for +overload+. I thought of +con+ becuase of it's double meaning as Latin for 'with' and as an abbreviation for 'conditional'. But it seems esoteric in practice, perhaps b/c it would be too general a term if we were programming in Spanish. While +over+ probably makes the most sense in terms of being an abbreviated form of +overload+, +sig+ is the same number of characters as +def+ and conveys some additional semantics which applies to method overloading --the *signature*.
14
-
data/README.rdoc DELETED
@@ -1,108 +0,0 @@
1
- = Platypus
2
- _ ___
3
- / \ / \
4
- \. |: cc|
5
- (.|:,---,
6
- (.|: \ c|
7
- (. y-'
8
- \ _ /
9
- m m
10
-
11
- * home: http://rubyworks.github.com/platypus
12
- * work: http://github.com/rubyworks/platypus
13
-
14
-
15
- == DESCRIPTION
16
-
17
- Platypus provides a generalized type conversion system,
18
- method overloading and psuedo-classes.
19
-
20
- == SYNOPSIS
21
-
22
- Type conversion work like a rational duck might expect.
23
-
24
- "1234".to(Float) => 1234.0 (Float)
25
-
26
- Time.from("6:30") => 1234.0 (Time)
27
-
28
- You can of course define your own.
29
-
30
- class X
31
- typecast String do |x|
32
- "#{x}"
33
- end
34
- end
35
-
36
- To overload a method, mixin the Overloadable module and use the #overload (or #sig)
37
- method to define new functionality based on a specified type interface.
38
-
39
- class X
40
- include Overloadable
41
-
42
- def f
43
- "f"
44
- end
45
-
46
- sig Integer
47
-
48
- def f(i)
49
- "f#{i}"
50
- end
51
-
52
- sig String, String
53
-
54
- def f(s1, s2)
55
- [s1, s2].join('+')
56
- end
57
- end
58
-
59
- x = X.new
60
-
61
- x.f #=> "f"
62
- x.f(1) #=> "f1"
63
- x.f("A","B") #=> "A+B"
64
-
65
- Finally, the Platypus gives you the Type superclass (aka pseudo-classes).
66
-
67
- class KiloType < Type
68
- x % 1000 == 0
69
- end
70
-
71
- KiloType === 1000
72
- KiloType === 2000
73
-
74
- To learn more about using Platypus see the Demonstrundum[http://rubyworks.github.com/platypus/docs/qed].
75
-
76
-
77
- == RELEASE NOTES
78
-
79
- Please see HISTORY file.
80
-
81
-
82
- == INSTALLATION
83
-
84
- To install with RubyGems simply open a console and type:
85
-
86
- $ gem install platypus
87
-
88
- Site installation can be achieved with Setup.rb (gem install setup),
89
- then download the tarball package and type:
90
-
91
- $ tar -xvzf platypus-1.0.0.tgz
92
- $ cd platypus-1.0.0
93
- $ setup.rb all
94
-
95
- Windows users use 'ruby setup.rb all'.
96
-
97
-
98
- == COPYING
99
-
100
- Copyright (c) 2010 Thomas Sawyer
101
-
102
- This program is ditributed unser the terms of the *FreeBSD* license.
103
-
104
- See COPYING.rdoc file for details.
105
-
106
-
107
- *-* mode: rdoc *-*
108
-
data/SPEC.rdoc DELETED
@@ -1,125 +0,0 @@
1
- = Platypus
2
-
3
-
4
- == Type Casting
5
-
6
- Require the casting library.
7
-
8
- require 'platypus/typecast'
9
-
10
- Define a couple of typecasts.
11
-
12
- class ::String
13
- typecast ::Regexp do |string|
14
- /#{string}/
15
- end
16
- typecast ::Regexp, :multiline do |string|
17
- /#{string}/m
18
- end
19
- end
20
-
21
- ::String.typecast ::Integer do |string|
22
- Integer(string)
23
- end
24
-
25
- See that they work.
26
-
27
- ::Regexp.from("ABC").assert == /ABC/
28
-
29
- And again.
30
-
31
- "123".to(::Integer).assert == 123
32
-
33
-
34
- == Pseudo-Types
35
-
36
- Require the library.
37
-
38
- require 'platypus/type'
39
-
40
- Now we can create types which are psuedo-classes.
41
-
42
- class KiloType < Type
43
- condition do |x|
44
- x.case? Integer
45
- x.kind_of?(Integer)
46
- x.respond_to?(:succ)
47
- x > 1000
48
- end
49
- end
50
-
51
- KiloType.assert === 2000
52
- KiloType.refute === 999
53
-
54
- Using the convenience #x method.
55
-
56
- class MegaType < Type
57
- x.case? Integer
58
- x.kind_of?(Integer)
59
- x.respond_to?(:succ)
60
- x > 1000000
61
- end
62
-
63
- MegaType.refute === 999999
64
- MegaType.assert === 20000000
65
-
66
-
67
-
68
- == Overloadable
69
-
70
- The Overloadable mixin provides a means for overloading
71
- methods based in method signature using an elegant syntax.
72
- To demonstrate, we first need to load the library.
73
-
74
- require 'platypus/overload'
75
-
76
- Now we can define a class that utilizes it.
77
-
78
- class X
79
- include Overloadable
80
-
81
- sig String, String
82
-
83
- def x(str1, str2)
84
- str1.assert.is_a?(String)
85
- str2.assert.is_a?(String)
86
- end
87
-
88
- sig Integer, Integer
89
-
90
- def x(int1, int2)
91
- int1.assert.is_a?(Integer)
92
- int2.assert.is_a?(Integer)
93
- end
94
- end
95
-
96
- As you can see will placed assertions directly into our methods
97
- definitions. We simply need to run an exmaple of each definition to
98
- see that it worked.
99
-
100
- x = X.new
101
-
102
- x.x("Hello", "World")
103
-
104
- x.x(100, 200)
105
-
106
- But what happens if the signiture is not matched? Either an ArgumentError will
107
- be raised.
108
-
109
- expect ArgumentError do
110
- x.x("Hello", 200)
111
- end
112
-
113
- Or it will fallback to a non-signiature definition, if one is defined.
114
-
115
-
116
- class X
117
- def x(obj1, obj2)
118
- obj1.refute.is_a?(Integer)
119
- obj2.refute.is_a?(String)
120
- end
121
- end
122
-
123
- x.x("Hello", 200)
124
-
125
-
@@ -1,173 +0,0 @@
1
- require 'platypus/overload'
2
-
3
- #
4
- class TC_Overload_01 < Test::Unit::TestCase
5
-
6
- class X
7
- include Overloadable
8
-
9
- def x
10
- "hello"
11
- end
12
-
13
- overload Array
14
-
15
- def x(x)
16
- [Array, x]
17
- end
18
-
19
- overload Symbol
20
-
21
- def x(x)
22
- [Symbol, x]
23
- end
24
- end
25
-
26
- def setup
27
- @x = X.new
28
- end
29
-
30
- def test_x
31
- assert_equal( "hello", @x.x )
32
- end
33
-
34
- def test_a
35
- assert_equal( [Array, [1]], @x.x([1]) )
36
- end
37
-
38
- def test_s
39
- assert_equal( [Symbol, :a], @x.x(:a) )
40
- end
41
-
42
- end
43
-
44
- #
45
- class TC_Overload_02 < Test::Unit::TestCase
46
-
47
- class X
48
- include Overloadable
49
-
50
- def x
51
- "hello"
52
- end
53
-
54
- overload Integer
55
-
56
- def x(i)
57
- i
58
- end
59
-
60
- overload String, String
61
-
62
- def x(s1, s2)
63
- [s1, s2]
64
- end
65
-
66
- end
67
-
68
- def setup
69
- @x = X.new
70
- end
71
-
72
- def test_x
73
- assert_equal( "hello", @x.x )
74
- end
75
-
76
- def test_i
77
- assert_equal( 1, @x.x(1) )
78
- end
79
-
80
- def test_s
81
- assert_equal( ["a","b"], @x.x("a","b") )
82
- end
83
-
84
- end
85
-
86
- #
87
- class TC_Overload_03 < Test::Unit::TestCase
88
-
89
- class SubArray < Array
90
- end
91
-
92
- class SubSubArray < SubArray
93
- end
94
-
95
- class X
96
- include Overloadable
97
-
98
- def x
99
- "hello"
100
- end
101
-
102
- overload Integer
103
-
104
- def x(i)
105
- i
106
- end
107
-
108
- overload Symbol
109
-
110
- def x(s)
111
- s
112
- end
113
-
114
- overload String, String
115
-
116
- def x(s1, s2)
117
- [s1, s2]
118
- end
119
-
120
- overload Symbol, String
121
-
122
- def x(s1, s2)
123
- [s1, s2]
124
- end
125
-
126
- overload Array
127
-
128
- def x(a)
129
- "array"
130
- end
131
-
132
- end
133
-
134
- def setup
135
- @x = X.new
136
- end
137
-
138
- def test_x
139
- assert_equal( "hello", @x.x )
140
- end
141
-
142
- def test_i
143
- assert_equal( 1, @x.x(1) )
144
- end
145
-
146
- def test_strings
147
- assert_equal( ["a","b"], @x.x("a","b") )
148
- end
149
-
150
- def test_symbol_string
151
- assert_equal( [:a,"b"], @x.x(:a,"b") )
152
- end
153
-
154
- def test_sym
155
- assert_equal( :sym, @x.x(:sym) )
156
- end
157
-
158
- def test_subarray
159
- assert_equal("array", @x.x([]))
160
- assert_equal("array", @x.x(SubArray.new))
161
- assert_equal("array", @x.x(SubSubArray.new))
162
- end
163
-
164
- #def test_raise
165
- # assert_raise ArgumentError do
166
- # X.module_eval do
167
- # overload 42
168
- # end
169
- # end
170
- #end
171
-
172
- end
173
-