giraffesoft-timeline_fu 0.1.1 → 0.2.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.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
+ :minor: 2
3
+ :patch: 0
2
4
  :major: 0
3
- :minor: 1
4
- :patch: 1
@@ -7,14 +7,19 @@ module TimelineFu
7
7
  module ClassMethods
8
8
  def fires(event_type, opts)
9
9
  raise ArgumentError, "Argument :on is mandatory" unless opts.has_key?(:on)
10
+ opts[:subject] = :self unless opts.has_key?(:subject)
10
11
 
11
12
  method_name = :"fire_#{event_type}_after_#{opts[:on]}"
12
13
  define_method(method_name) do
13
14
  create_options = [:actor, :subject, :secondary_subject].inject({}) do |memo, sym|
14
- memo[sym] = send(opts[sym]) if opts[sym]
15
+ case opts[sym]
16
+ when :self
17
+ memo[sym] = self
18
+ else
19
+ memo[sym] = send(opts[sym]) if opts[sym]
20
+ end
15
21
  memo
16
22
  end
17
- create_options[:subject] ||= self
18
23
  create_options[:event_type] = event_type.to_s
19
24
 
20
25
  TimelineEvent.create!(create_options)
@@ -0,0 +1,20 @@
1
+ module TimelineFu
2
+ module Macros
3
+ def should_fire_event(event_type, opts = {})
4
+ should "fire #{event_type} on #{opts[:on]}" do
5
+ matcher = fire_event(event_type, opts)
6
+
7
+ assert_accepts matcher, self.class.name.gsub(/Test$/, '').constantize
8
+ end
9
+ end
10
+
11
+ def should_not_fire_event(event_type, opts = {})
12
+ should "fire #{event_type} on #{opts[:on]}" do
13
+ matcher = fire_event(event_type, opts)
14
+
15
+ assert_rejects matcher, self.class.name.gsub(/Test$/, '').constantize
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,58 @@
1
+ module TimelineFu
2
+ module Matchers
3
+ class FireEvent
4
+ def initialize(event_type, opts = {})
5
+ @event_type = event_type
6
+ @opts = opts
7
+ @method = :"fire_#{@event_type}_after_#{@opts[:on]}"
8
+ end
9
+
10
+ def matches?(subject)
11
+ @subject = subject
12
+
13
+ defines_callback_method? && setups_up_callback?
14
+ end
15
+
16
+ def defines_callback_method?
17
+ if @subject.instance_methods.include?(@method.to_s)
18
+ true
19
+ else
20
+ @missing = "#{@subject.name} does not respond to #{@method}"
21
+ false
22
+ end
23
+ end
24
+
25
+ def setups_up_callback?
26
+ callback_chain_name = "after_#{@opts[:on]}_callback_chain"
27
+ callback_chain = @subject.send(callback_chain_name)
28
+ if callback_chain.any? {|chain| chain.method == @method }
29
+ true
30
+ else
31
+ @missing = "does setup after #{@opts[:on]} callback for #{@method}"
32
+ false
33
+ end
34
+ end
35
+
36
+ def description
37
+ "fire a #{@event_type} event"
38
+ end
39
+
40
+ def expectation
41
+ expected = "#{@subject.name} to #{description}"
42
+ end
43
+
44
+ def failure_message
45
+ "Expected #{expectation} (#{@missing})"
46
+ end
47
+
48
+ def negative_failure_message
49
+ "Did not expect #{expectation}"
50
+ end
51
+
52
+ end
53
+
54
+ def fire_event(event_type, opts)
55
+ FireEvent.new(event_type, opts)
56
+ end
57
+ end
58
+ end
data/test/fires_test.rb CHANGED
@@ -58,4 +58,18 @@ class FiresTest < Test::Unit::TestCase
58
58
  TimelineEvent.expects(:create!).never
59
59
  @james.save
60
60
  end
61
+
62
+ def test_should_set_secondary_subject_to_self_when_requested
63
+ @list = List.new(hash_for_list(:author => @james));
64
+ TimelineEvent.stubs(:create!).with(has_entry(:event_type, "list_created"))
65
+ @list.save
66
+ @comment = Comment.new(:body => 'cool list!', :author => @mat, :list => @list)
67
+ TimelineEvent.stubs(:create!).with(has_entry(:event_type, "comment_created"))
68
+ @comment.save
69
+ TimelineEvent.expects(:create!).with(:actor => @mat,
70
+ :subject => @list,
71
+ :secondary_subject => @comment,
72
+ :event_type => 'comment_deleted')
73
+ @comment.destroy
74
+ end
61
75
  end
data/test/test_helper.rb CHANGED
@@ -55,9 +55,13 @@ class Comment < ActiveRecord::Base
55
55
  belongs_to :list
56
56
  belongs_to :author, :class_name => "Person"
57
57
 
58
- fires :comment_created, :actor => :author,
59
- :on => :create,
58
+ fires :comment_created, :actor => :author,
59
+ :on => :create,
60
60
  :secondary_subject => :list
61
+ fires :comment_deleted, :actor => :author,
62
+ :on => :destroy,
63
+ :subject => :list,
64
+ :secondary_subject => :self
61
65
  end
62
66
 
63
67
  TimelineEvent = Class.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: giraffesoft-timeline_fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Golick
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2009-02-20 00:00:00 -08:00
14
+ date: 2009-05-26 00:00:00 -07:00
15
15
  default_executable:
16
16
  dependencies: []
17
17
 
@@ -21,8 +21,8 @@ executables: []
21
21
 
22
22
  extensions: []
23
23
 
24
- extra_rdoc_files: []
25
-
24
+ extra_rdoc_files:
25
+ - README.rdoc
26
26
  files:
27
27
  - README.rdoc
28
28
  - VERSION.yml
@@ -34,14 +34,17 @@ files:
34
34
  - generators/timeline_fu/USAGE
35
35
  - lib/timeline_fu
36
36
  - lib/timeline_fu/fires.rb
37
+ - lib/timeline_fu/macros.rb
38
+ - lib/timeline_fu/matchers.rb
37
39
  - lib/timeline_fu.rb
38
40
  - test/fires_test.rb
39
41
  - test/test_helper.rb
40
- has_rdoc: false
42
+ has_rdoc: true
41
43
  homepage: http://github.com/giraffesoft/timeline_fu
42
44
  post_install_message:
43
- rdoc_options: []
44
-
45
+ rdoc_options:
46
+ - --inline-source
47
+ - --charset=UTF-8
45
48
  require_paths:
46
49
  - lib
47
50
  required_ruby_version: !ruby/object:Gem::Requirement