abstract_method 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +83 -0
  3. data/VERSION +1 -0
  4. data/lib/abstract_method.rb +43 -0
  5. metadata +111 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 SUSE
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,83 @@
1
+ Abstract Method
2
+ ===============
3
+
4
+ Abstract Method is a tiny library enabling you to define abstract methods in Ruby classes and modules.
5
+
6
+ Example
7
+ -------
8
+
9
+ ```ruby
10
+ class AbstractClass
11
+ abstract_method :foo
12
+ end
13
+
14
+ class ConcreteClass < AbstractClass
15
+ def foo
16
+ 42
17
+ end
18
+ end
19
+
20
+ AbstractClass.new.foo # raises AbstractMethodCalled
21
+ ConcreteClass.new.foo # => 42
22
+ ```
23
+
24
+ Installation
25
+ ------------
26
+
27
+ $ gem install abstract_method
28
+
29
+ Usage
30
+ -----
31
+
32
+ First, require the library:
33
+
34
+ ```ruby
35
+ require "abstract_method"
36
+ ```
37
+
38
+ You can now use the `abstract_method` method to define abstract methods in classes and modules.
39
+
40
+ ```ruby
41
+ class AbstractClass
42
+ abstract_method :foo
43
+ end
44
+
45
+ class AbstractModule
46
+ abstract_method :bar
47
+ end
48
+ ```
49
+
50
+ When called, the abstract method will raise an `AbstractMethodCalled` exception with a helpful message:
51
+
52
+ ```ruby
53
+ AbstractClass.new.foo # => raises AbstractMethodCalled with the following message:
54
+ # Called unimplemented abstract method AbstractClass#foo
55
+ # (defined in class AbstractClass).
56
+ ```
57
+
58
+ Abstract methods can be overridden as usual:
59
+
60
+ ```ruby
61
+ class ConcreteClass < AbstractClass
62
+ def foo
63
+ 42
64
+ end
65
+ end
66
+
67
+ ConcreteClass.new.foo # => 42
68
+ ```
69
+
70
+ For more information, see the [API documentation](http://rubydoc.info/github/openSUSE/abstract_method/frames).
71
+
72
+ FAQ
73
+ ---
74
+
75
+ **Why define abstract methods? In a dynamic language like Ruby this isn't needed.**
76
+
77
+ Mainly for documentation purposes.
78
+
79
+ Imagine writing a plugin system where the plugins are classes derived from one superclass. For a plugin author it would be great to see what methods plugins need to implement directly from the superclass code. The abstract method definition is also a great place to put documentation describing what the method should do.
80
+
81
+ **Why create a new library? There is already the [abstract](http://rubygems.org/gems/abstract) gem.**
82
+
83
+ With the abstract gem you have to specify the abstract method parameter list in a string when you define it. This is ugly and in Ruby the parameter list often does not say much about the method anyway. The parameters should should rather be described using a code documentation system (such as [YARD](http://yardoc.org/)).
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,43 @@
1
+ # Tiny library enabling you to define abstract methods in Ruby classes and
2
+ # modules.
3
+ module AbstractMethod
4
+ # Abstract Method version (uses [semantic versioning](http://semver.org/)).
5
+ VERSION = File.read(File.dirname(__FILE__) + "/../VERSION").strip
6
+ end
7
+
8
+ # Exception raised when an abstract method is called.
9
+ class AbstractMethodCalled < StandardError
10
+ end
11
+
12
+ class Module
13
+ # Defines one or more abstract methods with given names in a class or module.
14
+ # When called, the abstract method will raise an `AbstractMethodCalled`
15
+ # exception with a helpful message.
16
+ #
17
+ # @example
18
+ # class AbstractClass
19
+ # abstract_method :foo
20
+ # end
21
+ #
22
+ # class ConcreteClass < AbstractClass
23
+ # def foo
24
+ # 42
25
+ # end
26
+ # end
27
+ #
28
+ # AbstractClass.new.foo # raises AbstractMethodCalled
29
+ # ConcreteClass.new.foo # => 42
30
+ #
31
+ # @param [Array<Symbol>] names the names of defined abstract methods
32
+ def abstract_method(*names)
33
+ definitor = self
34
+
35
+ names.each do |name|
36
+ define_method name do
37
+ raise AbstractMethodCalled,
38
+ "Called unimplemented abstract method #{self.class}##{name} " +
39
+ "(defined in #{definitor.class.name.downcase} #{definitor})."
40
+ end
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: abstract_method
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - David Majda
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-04-25 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: redcarpet
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: yard
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ description: Abstract Method is a tiny library enabling you to define abstract methods in Ruby classes and modules.
64
+ email: dmajda@suse.de
65
+ executables: []
66
+
67
+ extensions: []
68
+
69
+ extra_rdoc_files: []
70
+
71
+ files:
72
+ - LICENSE
73
+ - README.md
74
+ - VERSION
75
+ - lib/abstract_method.rb
76
+ has_rdoc: true
77
+ homepage: https://github.com/openSUSE/abstract_method
78
+ licenses:
79
+ - MIT
80
+ post_install_message:
81
+ rdoc_options: []
82
+
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ requirements: []
104
+
105
+ rubyforge_project:
106
+ rubygems_version: 1.6.2
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Tiny library enabling you to define abstract methods in Ruby classes and modules
110
+ test_files: []
111
+