callbacks 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,3 +3,10 @@
3
3
  * First release
4
4
  * API not yet stable
5
5
  * Only supports adding hooks to classes not instances. Instances are planned for 0.2
6
+
7
+ == 0.0.2 / 2008-09-28
8
+
9
+ * Second release!
10
+ * API still not stable
11
+ * Callbacks now picks up "before#{callback}" and "after_#{callback}" instance methods automatically
12
+ * Some documentation added
@@ -3,9 +3,9 @@ Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
5
  Version.txt
6
- bin/callbacks
7
6
  ext/metaid.rb
8
7
  lib/callback.rb
8
+ lib/callbackchain.rb
9
9
  lib/callbacks.rb
10
10
  lib/classmethods.rb
11
11
  lib/instancemethods.rb
data/README.txt CHANGED
@@ -13,6 +13,7 @@ This package makes it simple to add callbacks a.k.a. "hooks" to ruby classes
13
13
  * It now only supports adding callbacks to classes
14
14
  * Different callbacks for instances comes with version 0.2
15
15
  * I haven't decided yet on the API, maybe I'll swap "callback_methods" with "hooks"
16
+ * When using add_callback_method before the method itself is defined, it won't work
16
17
 
17
18
  == SYNOPSIS:
18
19
 
@@ -27,6 +28,8 @@ This package makes it simple to add callbacks a.k.a. "hooks" to ruby classes
27
28
  o = Object.new
28
29
  o.inspect #will print "Going to inspect Object"
29
30
 
31
+ For more code examples see: http://callbacks.rubyforge.com
32
+
30
33
  == REQUIREMENTS:
31
34
 
32
35
  * ruby 1.8 (might work with ruby1.9)
data/Rakefile CHANGED
@@ -20,6 +20,8 @@ PROJ.exclude = %w(.git pkg/ nbproject/ doc/ ^test[0-9]*.rb$ website/ )
20
20
  PROJ.rdoc.exclude = %w(Version.txt)
21
21
  PROJ.rdoc.remote_dir = 'docs/'
22
22
 
23
+ PROJ.rcov.opts << "--exclude rcov.rb"
24
+
23
25
  PROJ.spec.opts << '--color'
24
26
 
25
27
  # EOF
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -0,0 +1,6 @@
1
+ #Just a test!
2
+ module Callbacks
3
+ class CallbackChain < Hash
4
+
5
+ end
6
+ end
@@ -2,26 +2,19 @@
2
2
  module Callbacks
3
3
  require 'observer'
4
4
  require 'ext/metaid.rb'
5
+ require 'callbackchain.rb'
5
6
  require 'instancemethods.rb'
6
7
  require 'classmethods.rb'
7
8
  require 'callback.rb'
8
-
9
- # CALLBACKS = [
10
- # 'song_halfway',
11
- # 'song_end',
12
- # 'boot',
13
- # 'shutdown',
14
- # ]
15
-
16
- CALLBACKS = []
17
9
 
18
10
  #Sets all things right
19
11
  def self.included(base) #:nodoc:
20
- base.extend Observable
12
+ base.extend Observable #why?
21
13
  base.extend Callbacks::ClassMethods
22
14
  base.send(:include, Callbacks::InstanceMethods)
23
15
  end
24
16
  end
25
17
 
26
18
  #TODO: add aliases like add_hook(), callbacks(), et cetera
27
- #TODO: feature to add hooks/callbacks to instances instead of only classes
19
+ #TODO: feature to add hooks/callbacks to instances instead of only classes
20
+ #TODO: make all of the activesupport features/tests work
@@ -8,13 +8,13 @@ module Callbacks
8
8
 
9
9
  def callback_actions
10
10
  #Use one @, else it gets stored in the module and then it will be avaiable in every class that uses callback
11
- @callback_actions ||= {}
11
+ @callback_actions ||= CallbackChain.new
12
12
  end
13
13
 
14
14
  def add_callback_methods(*callbacks)
15
15
  callbacks.each do |callback|
16
16
  self.callback_methods << callback.to_sym
17
- self.build_callback_method(callback)
17
+ self.build_callback_methods(callback)
18
18
  end
19
19
  end
20
20
 
@@ -30,21 +30,9 @@ module Callbacks
30
30
  self.callback_methods.delete(method.to_sym)
31
31
  end
32
32
 
33
- def build_callback_method(method)
34
-
35
- #instance_eval do
36
- instance_eval <<-"end_eval"
37
-
38
- def before_#{method}(*callbacks, &block)
39
- add_callback_action :before, :#{method}, *callbacks, &block
40
- end
41
-
42
- def after_#{method}(*callbacks, &block)
43
- add_callback_action :after, :#{method}, *callbacks, &block
44
- end
45
-
46
- end_eval
47
- #end
33
+ def build_callback_methods(method)
34
+ build_before_callback_method(method)
35
+ build_after_callback_method(method)
48
36
 
49
37
  class_eval <<-"end_eval"
50
38
  def #{method}_with_callbacks
@@ -66,12 +54,35 @@ module Callbacks
66
54
  ca[method][type] = [] if ca[method][type].nil?
67
55
 
68
56
  if block_given?
69
- ca[method][type] << Callback.new(method, nil, &block)
57
+ callback = Callback.new(method, nil, &block)
70
58
  else
71
59
  code.each do |c|
72
- ca[method][type] << Callback.new(method, c)
60
+ callback = Callback.new(method, c)
73
61
  end
74
62
  end
63
+ ca[method][type] << callback
64
+ return callback
65
+ end
66
+
67
+ private
68
+ def build_before_callback_method(method)
69
+ instance_eval <<-"end_eval"
70
+
71
+ def before_#{method}(*callbacks, &block)
72
+ add_callback_action :before, :#{method}, *callbacks, &block
73
+ end
74
+
75
+ end_eval
76
+ end
77
+
78
+ def build_after_callback_method(method)
79
+ instance_eval <<-"end_eval"
80
+
81
+ def after_#{method}(*callbacks, &block)
82
+ add_callback_action :after, :#{method}, *callbacks, &block
83
+ end
84
+
85
+ end_eval
75
86
  end
76
87
 
77
88
  end
@@ -2,20 +2,31 @@ module Callbacks
2
2
  module InstanceMethods
3
3
 
4
4
  def callback_actions(show_classvars = true)
5
- return self.class_callback_actions
5
+ callback_actions = self.class_callback_actions
6
6
  #return self.instance_callbacks if show_classvars == false
7
7
  #return self.class_callback_actions + self.instance_callback_actions if show_classvars == true
8
8
  end
9
9
 
10
- #Really don't know what this does :p But it works!
11
- #I'm really, really bad at metaprogramming
12
- def callback_actions_for(method, type)
10
+ def callback_actions_for(method, type)
11
+ #Do the rescue if [method] does not exist (nil), [type] will fail
13
12
  begin
14
- return self.callback_actions[method] if type.nil?
15
- return self.callback_actions[method][type] ||= []
13
+ callback_actions = self.callback_actions[method][type] ||= []
16
14
  rescue NoMethodError
17
- return []
15
+ callback_actions = []
18
16
  end
17
+
18
+ #If the before/after_ method exists, and it is not already added,
19
+ # add it!
20
+ #Dit kan mooier!
21
+ if self.respond_to?("#{type}_#{method}")
22
+ if not callback_actions.include? "#{type}_#{method}".to_sym
23
+ #callback_actions << Callback.new(method, "#{type}_#{method}".to_sym)
24
+ callback = self.class.add_callback_action(type, method, "#{type}_#{method}".to_sym)
25
+ callback_actions << callback
26
+ end
27
+ end
28
+
29
+ return callback_actions
19
30
  end
20
31
 
21
32
  def class_callback_actions
@@ -35,4 +46,4 @@ module Callbacks
35
46
  #Should I return something?
36
47
  end
37
48
  end
38
- end
49
+ end
File without changes
File without changes
@@ -91,7 +91,7 @@ namespace :gem do
91
91
  end
92
92
 
93
93
  desc 'Install the gem'
94
- task :install => [:clobber, :package] do
94
+ task :install => [:clobber, 'gem:package'] do
95
95
  sh "#{SUDO} #{GEM} install --local pkg/#{PROJ.gem._spec.full_name}"
96
96
 
97
97
  # use this version of the command for rubygems > 1.0.0
@@ -109,6 +109,11 @@ namespace :gem do
109
109
  desc 'Reinstall the gem'
110
110
  task :reinstall => [:uninstall, :install]
111
111
 
112
+ desc 'Cleanup the gem'
113
+ task :cleanup do
114
+ sh "#{SUDO} #{GEM} cleanup #{PROJ.gem._spec.name}"
115
+ end
116
+
112
117
  end # namespace :gem
113
118
 
114
119
  desc 'Alias to gem:package'
@@ -15,9 +15,9 @@ namespace :manifest do
15
15
  lines.map! do |line|
16
16
  case line
17
17
  when %r/^(-{3}|\+{3})/; nil
18
- when %r/^@/; Console::ANSICode.blue line
19
- when %r/^\+/; Console::ANSICode.green line
20
- when %r/^\-/; Console::ANSICode.red line
18
+ when %r/^@/; ANSICode.blue line
19
+ when %r/^\+/; ANSICode.green line
20
+ when %r/^\-/; ANSICode.red line
21
21
  else line end
22
22
  end
23
23
  end
File without changes
File without changes
File without changes
@@ -1,4 +1,3 @@
1
- # $Id$
2
1
 
3
2
  if PROJ.rubyforge.name.valid? && HAVE_RUBYFORGE
4
3
 
@@ -7,7 +6,7 @@ require 'rake/contrib/sshpublisher'
7
6
 
8
7
  namespace :gem do
9
8
  desc 'Package and upload to RubyForge'
10
- task :release => [:clobber, :package] do |t|
9
+ task :release => [:clobber, 'gem:package'] do |t|
11
10
  v = ENV['VERSION'] or abort 'Must supply VERSION=x.y.z'
12
11
  abort "Versions don't match #{v} vs #{PROJ.version}" if v != PROJ.version
13
12
  pkg = "pkg/#{PROJ.gem._spec.full_name}"
@@ -18,6 +17,7 @@ namespace :gem do
18
17
  end
19
18
 
20
19
  rf = RubyForge.new
20
+ rf.configure rescue nil
21
21
  puts 'Logging in'
22
22
  rf.login
23
23
 
@@ -18,7 +18,7 @@ PROJ = OpenStruct.new(
18
18
  :email => nil,
19
19
  :url => "\000",
20
20
  :version => ENV['VERSION'] || '0.0.0',
21
- :exclude => %w(tmp$ bak$ ~$ CVS .svn/ ^pkg/),
21
+ :exclude => %w(tmp$ bak$ ~$ CVS \.svn/ \.git/ ^pkg/),
22
22
  :release_name => ENV['RELEASE'],
23
23
 
24
24
  # System Defaults
@@ -58,7 +58,7 @@ PROJ = OpenStruct.new(
58
58
 
59
59
  # File Annotations
60
60
  :notes => OpenStruct.new(
61
- :exclude => %w(^tasks/setup.rb$),
61
+ :exclude => %w(^tasks/setup\.rb$),
62
62
  :extensions => %w(.txt .rb .erb) << '',
63
63
  :tags => %w(FIXME OPTIMIZE TODO)
64
64
  ),
@@ -74,8 +74,8 @@ PROJ = OpenStruct.new(
74
74
  # Rdoc
75
75
  :rdoc => OpenStruct.new(
76
76
  :opts => [],
77
- :include => %w(^lib/ ^bin/ ^ext/ .txt$),
78
- :exclude => %w(extconf.rb$),
77
+ :include => %w(^lib/ ^bin/ ^ext/ \.txt$),
78
+ :exclude => %w(extconf\.rb$),
79
79
  :main => nil,
80
80
  :dir => 'doc',
81
81
  :remote_dir => nil
@@ -159,6 +159,8 @@ GEM = WIN32 ? 'gem.bat' : 'gem'
159
159
  end
160
160
  HAVE_SVN = (Dir.entries(Dir.pwd).include?('.svn') and
161
161
  system("svn --version 2>&1 > #{DEV_NULL}"))
162
+ HAVE_GIT = (Dir.entries(Dir.pwd).include?('.git') and
163
+ system("git --version 2>&1 > #{DEV_NULL}"))
162
164
 
163
165
  # Reads a file at +path+ and spits out an array of the +paragraphs+
164
166
  # specified.
File without changes
File without changes
File without changes
@@ -153,6 +153,38 @@ class CallbacksTest < Test::Unit::TestCase
153
153
  assert_equal ['before_exit'], $tests_run
154
154
  end
155
155
 
156
+ # def test_should_not_raise_error_when_method_not_exist
157
+ # assert_nothing_raised do
158
+ # Tester.add_callback_methods :doesnotexist
159
+ # end
160
+ # end
161
+
162
+ # def test_no_method_callback_should_not_add_method
163
+ # Tester.add_callback_methods :nomethodcallback
164
+ # t = Tester.new
165
+ #
166
+ # assert_raise NameError do
167
+ # t.nomethodcallback
168
+ # end
169
+ # end
170
+
171
+ def test_should_use_defined_methods_in_class_as_callback
172
+ Tester.add_callback_methods :callback_with_defined_method
173
+ t = Tester.new
174
+ t.callback_with_defined_method
175
+ assert $tests_run.length > 0
176
+ end
177
+
178
+ def test_callback_should_only_be_certain_types
179
+ assert_nothing_raised do
180
+ Callback.new(:inspect, 'print "This is a test"')
181
+ end
182
+
183
+ assert_raise RuntimeError do
184
+ Callback.new(:inspect, [])
185
+ end
186
+ end
187
+
156
188
  # def test_callbacks_created_in_class_should_be_vissible_in_instance
157
189
  # Tester.add_callback_methods :boot, :exit
158
190
  # t = Tester.new
@@ -15,6 +15,15 @@ class Tester
15
15
  return "testing"
16
16
  end
17
17
 
18
+ def before_callback_with_defined_method
19
+ text = "This is before the method ran and is generated by a defined method in the class"
20
+ $tests_run << text
21
+ return text
22
+ end
23
+
24
+ def callback_with_defined_method
25
+ end
26
+
18
27
  def fill_globalvar_tests_run
19
28
  $tests_run << 'fill_globalvar_tests_run'
20
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: callbacks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leon Bogaert
@@ -9,29 +9,28 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-05-20 00:00:00 +02:00
12
+ date: 2008-09-28 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
16
  description: This package makes it simple to add callbacks a.k.a. "hooks" to ruby classes
17
17
  email: leon@vanutsteen.nl
18
- executables:
19
- - callbacks
18
+ executables: []
19
+
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
23
  - History.txt
24
24
  - README.txt
25
- - bin/callbacks
26
25
  files:
27
26
  - History.txt
28
27
  - Manifest.txt
29
28
  - README.txt
30
29
  - Rakefile
31
30
  - Version.txt
32
- - bin/callbacks
33
31
  - ext/metaid.rb
34
32
  - lib/callback.rb
33
+ - lib/callbackchain.rb
35
34
  - lib/callbacks.rb
36
35
  - lib/classmethods.rb
37
36
  - lib/instancemethods.rb
@@ -77,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
76
  requirements: []
78
77
 
79
78
  rubyforge_project: callbacks
80
- rubygems_version: 1.1.1
79
+ rubygems_version: 1.2.0
81
80
  signing_key:
82
81
  specification_version: 2
83
82
  summary: This package makes it simple to add callbacks a
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require File.expand_path(
4
- File.join(File.dirname(__FILE__), '..', 'lib', 'callbacks'))
5
-
6
- # Put your code here
7
-
8
- # EOF