acts_as_interface 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ gem "activesupport", "2.3.4"
14
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Philippe Huibonhoa
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ = acts_as_interface
2
+
3
+ == INSTALL
4
+
5
+ gem install timecop
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
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
+ == CONTRIBUTING
63
+
64
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
65
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
66
+ * Fork the project
67
+ * Start a feature/bugfix branch
68
+ * Commit and push until you are happy with your contribution
69
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
70
+ * 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.
71
+
72
+ == COPYRIGHT
73
+
74
+ Copyright (c) 2011 Philippe Huibonhoa. See LICENSE.txt for
75
+ further details.
76
+
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "acts_as_interface"
16
+ gem.homepage = "http://github.com/phuibonhoa/acts_as_interface"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Easily define abstract methods and callbacks for superclasses and modules}
19
+ gem.description = %Q{Easily define abstract methods and callbacks for superclasses and modules}
20
+ gem.email = "philippe.huibonhoa@bookrenter.com"
21
+ gem.authors = ["Philippe Huibonhoa"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "acts_as_interface #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{acts_as_interface}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Philippe Huibonhoa"]
12
+ s.date = %q{2011-01-27}
13
+ s.description = %q{Easily define abstract methods and callbacks for superclasses and modules}
14
+ s.email = %q{philippe.huibonhoa@bookrenter.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "LICENSE.txt",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "acts_as_interface.gemspec",
27
+ "lib/acts_as_interface.rb",
28
+ "test/helper.rb",
29
+ "test/test_acts_as_interface.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/phuibonhoa/acts_as_interface}
32
+ s.licenses = ["MIT"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.7}
35
+ s.summary = %q{Easily define abstract methods and callbacks for superclasses and modules}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/test_acts_as_interface.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
47
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
49
+ s.add_development_dependency(%q<rcov>, [">= 0"])
50
+ s.add_development_dependency(%q<activesupport>, ["= 2.3.4"])
51
+ else
52
+ s.add_dependency(%q<shoulda>, [">= 0"])
53
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
55
+ s.add_dependency(%q<rcov>, [">= 0"])
56
+ s.add_dependency(%q<activesupport>, ["= 2.3.4"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<shoulda>, [">= 0"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ s.add_dependency(%q<activesupport>, ["= 2.3.4"])
64
+ end
65
+ end
66
+
@@ -0,0 +1,76 @@
1
+ require 'active_support'
2
+
3
+ #Include ActsAsInterface to easily define abstract methods
4
+ #Example: Superclass
5
+ # class Person
6
+ # abstract_methods :dance, :eat
7
+ # abstract_methods :sleep, :play, :visibility => :private
8
+ # callbacks :prance
9
+ #
10
+ # abstract_methods :foo, :for => :class
11
+ # callbacks :france, :for => :class, :visibility => :protected
12
+ # end
13
+ #
14
+ # me = MyClass.new
15
+ # me.dance #raises NotImplementedError
16
+ # me.play #raises NoMethodError: private method `close_book` called
17
+ # me.send(:play) #calls empty method play
18
+ # me.prance #calls empty method prance
19
+ # Person.foo #raises NotImplementedError
20
+ #
21
+ #Example: ActiveRecord Module
22
+ # module ActsAsPerson
23
+ #
24
+ # # included in active record objects where abstracted methods can be either attributes of the model or defined by the model
25
+ # def self.included(base)
26
+ # base.instance_eval do
27
+ # include ActsAsInterface
28
+ # abstract_methods :name, :dance
29
+ # end
30
+ # end
31
+ #
32
+ # end
33
+ class Object
34
+ def define_abstract_method(abstract_method_name, options)
35
+ unless method_defined?(abstract_method_name) or (respond_to?(:columns) and columns.any? { |column| column.name.to_sym == abstract_method_name })
36
+ define_method(abstract_method_name) do |*abstract_method_args|
37
+ if options.has_key?(:default)
38
+ options[:default]
39
+ else
40
+ raise NotImplementedError, "#{abstract_method_name} not defined for #{self.class}"
41
+ end
42
+ end
43
+
44
+ send(options[:visibility], abstract_method_name) if options[:visibility]
45
+ end
46
+ end
47
+ end
48
+
49
+ module ActsAsInterface
50
+
51
+ def self.included(base)
52
+ base.extend(ClassMethods)
53
+ end
54
+
55
+ module ClassMethods
56
+ def abstract_methods(*args)
57
+ options = args.extract_options!
58
+ args.each do |abstract_method_name|
59
+ if options[:for] == :class
60
+ (class << self; self; end).instance_eval do
61
+ define_abstract_method(abstract_method_name, options)
62
+ end
63
+ else
64
+ define_abstract_method(abstract_method_name, options)
65
+ end
66
+ end
67
+ end
68
+
69
+ def callbacks(*args)
70
+ options = args.extract_options!
71
+ options = {:default => nil}.merge(options)
72
+ abstract_methods(*args.push(options))
73
+ end
74
+ end
75
+
76
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'acts_as_interface'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,104 @@
1
+ require "helper"
2
+
3
+ class AttributeActsAsInterfaceTest < Test::Unit::TestCase
4
+
5
+ class InterfacedClass
6
+ include ActsAsInterface
7
+
8
+ def existing_method
9
+ true
10
+ end
11
+
12
+ def self.existing_class_method
13
+ true
14
+ end
15
+
16
+ abstract_methods :im, :existing_method
17
+ abstract_methods :private_im, :visibility => :private
18
+ abstract_methods :im_with_default, :default => nil
19
+ callbacks :icb
20
+
21
+ abstract_methods :cm, :existing_class_method, :for => :class
22
+ abstract_methods :private_cm, :visibility => :private, :for => :class
23
+ abstract_methods :cm_with_default, :default => nil, :for => :class
24
+ callbacks :ccb, :for => :class
25
+ end
26
+
27
+ class ChildOfInterfacedClass < InterfacedClass
28
+ def im(word)
29
+ "hello #{word}"
30
+ end
31
+
32
+ def self.cm(word)
33
+ "#{word} world"
34
+ end
35
+ end
36
+
37
+ context "with instance methods" do
38
+ setup do
39
+ @ic = InterfacedClass.new
40
+ end
41
+
42
+ should "raise with no default" do
43
+ assert_raise(NotImplementedError) { @ic.im }
44
+ end
45
+
46
+ should "scope visibility if passed visibility option" do
47
+ assert_raise(NoMethodError) { @ic.private_im }
48
+ assert_raise(NotImplementedError) { @ic.send(:private_im) }
49
+ end
50
+
51
+ should "return default if passed default option" do
52
+ assert_equal nil, @ic.im_with_default
53
+ end
54
+
55
+ should "return nil if callback" do
56
+ assert_equal nil, @ic.icb
57
+ end
58
+
59
+ should "not override existing methods" do
60
+ assert_equal true, @ic.existing_method
61
+ end
62
+
63
+ context "child class" do
64
+ setup do
65
+ @child = ChildOfInterfacedClass.new
66
+ end
67
+
68
+ should "be allowed to override abstracted method" do
69
+ assert_equal "hello world", @child.im('world')
70
+ end
71
+ end
72
+ end
73
+
74
+ context "with class methods" do
75
+ should "raise with no default" do
76
+ assert_raise(NotImplementedError) { InterfacedClass.cm }
77
+ end
78
+
79
+ should "scope visibility if passed visibility option" do
80
+ assert_raise(NoMethodError) { InterfacedClass.private_cm }
81
+ assert_raise(NotImplementedError) { InterfacedClass.send(:private_cm) }
82
+ end
83
+
84
+ should "return default if passed default option" do
85
+ assert_equal nil, InterfacedClass.cm_with_default
86
+ end
87
+
88
+ should "return nil if callback" do
89
+ assert_equal nil, InterfacedClass.ccb
90
+ end
91
+
92
+ should "not override existing methods" do
93
+ assert_equal true, InterfacedClass.existing_class_method
94
+ end
95
+
96
+ context "child class" do
97
+ should "be allowed to override abstracted method" do
98
+ assert_equal "wayne's world", ChildOfInterfacedClass.cm("wayne's")
99
+ end
100
+ end
101
+ end
102
+
103
+
104
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_interface
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Philippe Huibonhoa
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-27 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ name: shoulda
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ hash: 23
42
+ segments:
43
+ - 1
44
+ - 0
45
+ - 0
46
+ version: 1.0.0
47
+ type: :development
48
+ name: bundler
49
+ prerelease: false
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ hash: 7
58
+ segments:
59
+ - 1
60
+ - 5
61
+ - 2
62
+ version: 1.5.2
63
+ type: :development
64
+ name: jeweler
65
+ prerelease: false
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ type: :development
78
+ name: rcov
79
+ prerelease: false
80
+ version_requirements: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - "="
86
+ - !ruby/object:Gem::Version
87
+ hash: 11
88
+ segments:
89
+ - 2
90
+ - 3
91
+ - 4
92
+ version: 2.3.4
93
+ type: :development
94
+ name: activesupport
95
+ prerelease: false
96
+ version_requirements: *id005
97
+ description: Easily define abstract methods and callbacks for superclasses and modules
98
+ email: philippe.huibonhoa@bookrenter.com
99
+ executables: []
100
+
101
+ extensions: []
102
+
103
+ extra_rdoc_files:
104
+ - LICENSE.txt
105
+ - README.rdoc
106
+ files:
107
+ - .document
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.rdoc
111
+ - Rakefile
112
+ - VERSION
113
+ - acts_as_interface.gemspec
114
+ - lib/acts_as_interface.rb
115
+ - test/helper.rb
116
+ - test/test_acts_as_interface.rb
117
+ has_rdoc: true
118
+ homepage: http://github.com/phuibonhoa/acts_as_interface
119
+ licenses:
120
+ - MIT
121
+ post_install_message:
122
+ rdoc_options: []
123
+
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ hash: 3
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ hash: 3
141
+ segments:
142
+ - 0
143
+ version: "0"
144
+ requirements: []
145
+
146
+ rubyforge_project:
147
+ rubygems_version: 1.3.7
148
+ signing_key:
149
+ specification_version: 3
150
+ summary: Easily define abstract methods and callbacks for superclasses and modules
151
+ test_files:
152
+ - test/helper.rb
153
+ - test/test_acts_as_interface.rb