acts_as_interface 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +110 -0
- data/VERSION +1 -1
- data/acts_as_interface.gemspec +4 -4
- data/lib/acts_as_interface.rb +5 -4
- data/test/test_acts_as_interface.rb +9 -5
- metadata +6 -6
- data/README.rdoc +0 -104
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# acts\_as\_interface
|
2
|
+
|
3
|
+
## INSTALL
|
4
|
+
|
5
|
+
gem install acts_as_interface
|
6
|
+
|
7
|
+
## DESCRIPTION
|
8
|
+
|
9
|
+
In the spirit of self documenting code, `acts_as_interface` allows you to easily define `abstract_methods` and callbacks just by listing them at the top of a superclass or module. Child classes or classes that include the module must implement abstract_methods or a `NotImplementedError` is raised when the method is called.
|
10
|
+
|
11
|
+
## EXAMPLES
|
12
|
+
|
13
|
+
### SUPERCLASS
|
14
|
+
|
15
|
+
class Person
|
16
|
+
include acts_as_interface
|
17
|
+
|
18
|
+
abstract_methods :dance, :eat
|
19
|
+
abstract_methods :sleep, :play, :visibility => :private
|
20
|
+
callbacks :prance
|
21
|
+
|
22
|
+
abstract_methods :foo, :for => :class
|
23
|
+
callbacks :france, :for => :class, :visibility => :protected
|
24
|
+
end
|
25
|
+
|
26
|
+
class Student < Person
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
me = Student.new
|
31
|
+
me.dance ## => raises NotImplementedError
|
32
|
+
|
33
|
+
me.play ## => raises NoMethodError: private method `play` called
|
34
|
+
me.send(:play) ## => raises NotImplementedError
|
35
|
+
|
36
|
+
me.prance ## calls empty method prance
|
37
|
+
|
38
|
+
Student.foo ## => raises NotImplementedError
|
39
|
+
|
40
|
+
|
41
|
+
### MODULE (on include)
|
42
|
+
|
43
|
+
module ActsAsPerson
|
44
|
+
def self.included(base)
|
45
|
+
base.instance_eval do
|
46
|
+
include ActsAsInterface
|
47
|
+
abstract_methods :name, :dance
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class Student < ActiveRecord::Base
|
53
|
+
include acts_as_person
|
54
|
+
|
55
|
+
#name is an attribute (ie t.string :name)
|
56
|
+
end
|
57
|
+
|
58
|
+
student = Student.first
|
59
|
+
student.name ## works as usual
|
60
|
+
student.dance ## => raises NotImplementedError
|
61
|
+
|
62
|
+
### MODULE (from method call)
|
63
|
+
|
64
|
+
module ActsAsPerson
|
65
|
+
module ClassMethods
|
66
|
+
def acts_as_person
|
67
|
+
include ActsAsInterface
|
68
|
+
abstract_methods :name, :dance
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.included(base)
|
73
|
+
base.extend(ClassMethods)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class Student < ActiveRecord::Base
|
78
|
+
include acts_as_person
|
79
|
+
|
80
|
+
#include or invoke modules that defines name but not dance
|
81
|
+
|
82
|
+
acts_as_person ## apply interface
|
83
|
+
end
|
84
|
+
|
85
|
+
student = Student.first
|
86
|
+
student.name ## works as usual
|
87
|
+
student.dance ## => raises NotImplementedError
|
88
|
+
|
89
|
+
|
90
|
+
## Credits
|
91
|
+
|
92
|
+
![BookRenter.com Logo](http://assets0.bookrenter.com/images/header/bookrenter_logo.gif "BookRenter.com")
|
93
|
+
|
94
|
+
ActsAsInterface is currently maintained by [Philippe Huibonhoa](http://github.com/phuibonhoa) and funded by [BookRenter.com](http://www.bookrenter.com "BookRenter.com").
|
95
|
+
|
96
|
+
## CONTRIBUTING
|
97
|
+
|
98
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
99
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
100
|
+
* Fork the project
|
101
|
+
* Start a feature/bugfix branch
|
102
|
+
* Commit and push until you are happy with your contribution
|
103
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
104
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
105
|
+
|
106
|
+
## COPYRIGHT
|
107
|
+
|
108
|
+
Copyright (c) 2011 Philippe Huibonhoa. See LICENSE.txt for
|
109
|
+
further details.
|
110
|
+
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/acts_as_interface.gemspec
CHANGED
@@ -5,22 +5,22 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{acts_as_interface}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Philippe Huibonhoa"]
|
12
|
-
s.date = %q{2011-02
|
12
|
+
s.date = %q{2011-03-02}
|
13
13
|
s.description = %q{Easily define abstract methods and callbacks for superclasses and modules}
|
14
14
|
s.email = %q{philippe.huibonhoa@bookrenter.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
17
|
-
"README.
|
17
|
+
"README.md"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
"Gemfile",
|
22
22
|
"LICENSE.txt",
|
23
|
-
"README.
|
23
|
+
"README.md",
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"acts_as_interface.gemspec",
|
data/lib/acts_as_interface.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
1
3
|
#Include ActsAsInterface to easily define abstract methods
|
2
4
|
#Example: Superclass
|
3
5
|
# class Person
|
@@ -35,7 +37,8 @@ class Object
|
|
35
37
|
if options.has_key?(:default)
|
36
38
|
options[:default]
|
37
39
|
else
|
38
|
-
|
40
|
+
class_name = options[:for] == :class ? self : self.class
|
41
|
+
raise NotImplementedError, "#{abstract_method_name} not defined for #{class_name}"
|
39
42
|
end
|
40
43
|
end
|
41
44
|
|
@@ -55,9 +58,7 @@ module ActsAsInterface
|
|
55
58
|
options = args.extract_options!
|
56
59
|
args.each do |abstract_method_name|
|
57
60
|
if options[:for] == :class
|
58
|
-
(class << self; self; end).
|
59
|
-
define_abstract_method(abstract_method_name, options)
|
60
|
-
end
|
61
|
+
(class << self; self; end).define_abstract_method(abstract_method_name, options)
|
61
62
|
else
|
62
63
|
define_abstract_method(abstract_method_name, options)
|
63
64
|
end
|
@@ -8,9 +8,11 @@ class AttributeActsAsInterfaceTest < Test::Unit::TestCase
|
|
8
8
|
def existing_method
|
9
9
|
true
|
10
10
|
end
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def existing_class_method
|
14
|
+
true
|
15
|
+
end
|
14
16
|
end
|
15
17
|
|
16
18
|
abstract_methods :im, :existing_method
|
@@ -29,8 +31,10 @@ class AttributeActsAsInterfaceTest < Test::Unit::TestCase
|
|
29
31
|
"hello #{word}"
|
30
32
|
end
|
31
33
|
|
32
|
-
|
33
|
-
|
34
|
+
class << self
|
35
|
+
def cm(word)
|
36
|
+
"#{word} world"
|
37
|
+
end
|
34
38
|
end
|
35
39
|
end
|
36
40
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_interface
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Philippe Huibonhoa
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02
|
18
|
+
date: 2011-03-02 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -118,12 +118,12 @@ extensions: []
|
|
118
118
|
|
119
119
|
extra_rdoc_files:
|
120
120
|
- LICENSE.txt
|
121
|
-
- README.
|
121
|
+
- README.md
|
122
122
|
files:
|
123
123
|
- .document
|
124
124
|
- Gemfile
|
125
125
|
- LICENSE.txt
|
126
|
-
- README.
|
126
|
+
- README.md
|
127
127
|
- Rakefile
|
128
128
|
- VERSION
|
129
129
|
- acts_as_interface.gemspec
|
data/README.rdoc
DELETED
@@ -1,104 +0,0 @@
|
|
1
|
-
= acts_as_interface
|
2
|
-
|
3
|
-
== INSTALL
|
4
|
-
|
5
|
-
gem install acts_as_interface
|
6
|
-
|
7
|
-
== DESCRIPTION
|
8
|
-
|
9
|
-
In the spirit of self documenting code, acts_as_interface allows you to easily define abstract_methods and callbacks just by listing them at the top of a superclass or module. Child classes or classes that include the module must implement abstract_methods or a NotImplementedError is raised when the method is called.
|
10
|
-
|
11
|
-
== EXAMPLES
|
12
|
-
|
13
|
-
=== SUPERCLASS
|
14
|
-
|
15
|
-
class Person
|
16
|
-
include acts_as_interface
|
17
|
-
|
18
|
-
abstract_methods :dance, :eat
|
19
|
-
abstract_methods :sleep, :play, :visibility => :private
|
20
|
-
callbacks :prance
|
21
|
-
|
22
|
-
abstract_methods :foo, :for => :class
|
23
|
-
callbacks :france, :for => :class, :visibility => :protected
|
24
|
-
end
|
25
|
-
|
26
|
-
class Student < Person
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
me = Student.new
|
31
|
-
me.dance #raises NotImplementedError
|
32
|
-
|
33
|
-
me.play #raises NoMethodError: private method `play` called
|
34
|
-
me.send(:play) #raises NotImplementedError
|
35
|
-
|
36
|
-
me.prance #calls empty method prance
|
37
|
-
|
38
|
-
Student.foo #raises NotImplementedError
|
39
|
-
|
40
|
-
|
41
|
-
=== MODULE (on include)
|
42
|
-
|
43
|
-
module ActsAsPerson
|
44
|
-
def self.included(base)
|
45
|
-
base.instance_eval do
|
46
|
-
include ActsAsInterface
|
47
|
-
abstract_methods :name, :dance
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
class Student < ActiveRecord::Base
|
53
|
-
include acts_as_person
|
54
|
-
|
55
|
-
#name is an attribute (ie t.string :name)
|
56
|
-
end
|
57
|
-
|
58
|
-
student = Student.first
|
59
|
-
student.name #works as usual
|
60
|
-
student.dance #raises NotImplementedError
|
61
|
-
|
62
|
-
=== MODULE (from method call)
|
63
|
-
|
64
|
-
module ActsAsPerson
|
65
|
-
module ClassMethods
|
66
|
-
def acts_as_person
|
67
|
-
include ActsAsInterface
|
68
|
-
abstract_methods :name, :dance
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def self.included(base)
|
73
|
-
base.extend(ClassMethods)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
class Student < ActiveRecord::Base
|
78
|
-
include acts_as_person
|
79
|
-
|
80
|
-
#include or invoke modules that defines name but not dance
|
81
|
-
|
82
|
-
acts_as_person #apply interface
|
83
|
-
end
|
84
|
-
|
85
|
-
student = Student.first
|
86
|
-
student.name #works as usual
|
87
|
-
student.dance #raises NotImplementedError
|
88
|
-
|
89
|
-
|
90
|
-
== CONTRIBUTING
|
91
|
-
|
92
|
-
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
93
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
94
|
-
* Fork the project
|
95
|
-
* Start a feature/bugfix branch
|
96
|
-
* Commit and push until you are happy with your contribution
|
97
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
98
|
-
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
99
|
-
|
100
|
-
== COPYRIGHT
|
101
|
-
|
102
|
-
Copyright (c) 2011 Philippe Huibonhoa. See LICENSE.txt for
|
103
|
-
further details.
|
104
|
-
|