mongoid-rspec-callbacks 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 465a27718060c18e511a3325a334d7f6f6b2069f
4
+ data.tar.gz: 460b30c060683b4cfd25ad4d8a9a777151395ce3
5
+ SHA512:
6
+ metadata.gz: 8a0b7b93b7a11f9fdd0f2959c0d9ef8219bc627bdfdfe4f9332f11791e565e7901c9f84ee13d79379f5da2092e4b37b18831496e63dee072c0634deb371ff60a
7
+ data.tar.gz: 8a661367cb27819cb28c70d34d9480d4a142280badde2c2920b1c2e546a098ba99ded2f7111705bbb3cbe2559407964e7cac5e820a74ff4812077b1a99992020
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in mongoid-rspec-callbacks.gemspec
4
- gemspec
3
+ gem 'activesupport', '~> 4.2'
4
+ gem 'jeweler'
5
+ gem 'rspec'
6
+ gem 'mongoid'
data/README.md CHANGED
@@ -3,7 +3,7 @@ mongoid-rspec-callbacks
3
3
 
4
4
  http://rubygems.org/gems/mongoid-rspec-callbacks
5
5
 
6
- RSpec Callbacks matchers for Mongoid 3.x.
6
+ RSpec Callbacks matchers for Mongoid 5.x and ActiveSupport 4.2.
7
7
 
8
8
  This gem is meant to be use with [mongoid-rpsec](http://rubygems.org/gems/mongoid-rspec), altought it works by itself.
9
9
 
data/Rakefile CHANGED
@@ -1,3 +1,17 @@
1
+ require 'jeweler'
2
+ Jeweler::Tasks.new do |gem|
3
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
4
+ gem.name = "mongoid-rspec-callbacks"
5
+ gem.homepage = "http://github.com/nanocity/mongoid-rspec-callbacks"
6
+ gem.license = "GPLv3"
7
+ gem.summary = %Q{RSpec Callbacks matchers for Mongoid 3.X}
8
+ gem.description = %Q{This gem is meant to be use with mongoid-rpsec, altought it works by itself. Syntax is the same as shoulda-callback-matchers.}
9
+ gem.email = "lciugar@gmail.com"
10
+ gem.authors = ["Luis Ciudad"]
11
+ # dependencies defined in Gemfile
12
+ end
13
+ Jeweler::RubygemsDotOrgTasks.new
14
+
1
15
  require 'rspec/core/rake_task'
2
16
 
3
17
  RSpec::Core::RakeTask.new(:spec)
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -1,28 +1,18 @@
1
1
  module Mongoid
2
2
  module Matchers
3
3
  class HaveCallbackMatcher
4
+ # Ensure that the given model has a callback defined for the given method/s
5
+ # callback(:method1, :method2).after(:validation).on(:create)
6
+ # @methods @kind @operation @context
7
+
4
8
  KINDS = %w[ before around after ]
5
9
 
10
+ # Set methods to look for
6
11
  def initialize( *args )
7
12
  @methods = args || []
8
13
  end
9
14
 
10
- def matches?( klass )
11
- return false unless @kind
12
- return false if @no_op = !klass.class.respond_to?( :"_#{@operation}_callbacks" )
13
-
14
- @guess = nil
15
- @methods.each do |method|
16
- filter = klass.class.send( :"_#{@operation}_callbacks" ).detect do |c|
17
- @guess = c if c.filter == method
18
-
19
- c.filter == method and c.kind == @kind and c.options[:on] == @on
20
- end
21
-
22
- return false unless filter
23
- end
24
- end
25
-
15
+ # Set when callback is fired using @kind and @operation
26
16
  KINDS.each do |kind|
27
17
  define_method( kind.to_sym ) do |op|
28
18
  @operation = op
@@ -31,48 +21,87 @@ module Mongoid
31
21
  end
32
22
  end
33
23
 
24
+ # Set on condition
34
25
  def on( action )
35
- @on = action
26
+ @context = action
36
27
  self
37
28
  end
38
29
 
39
- def failure_message_for_should
40
- failure_message( true )
30
+ def matches?( klass )
31
+ return false unless @kind
32
+ return false if @no_op = !klass.class.respond_to?( :"_#{@operation}_callbacks" )
33
+
34
+ @guess = nil
35
+ @methods.each do |method|
36
+ filter = klass.class.send( :"_#{@operation}_callbacks" ).detect do |callback|
37
+ # Save callback instance in order to print information
38
+ # about it in case of failure
39
+ @guess = callback if callback.filter == method
40
+ check_filter?(callback, method) and check_kind?(callback, @kind) and check_context?(callback, @context)
41
+ end
42
+
43
+ return false unless filter
44
+ end
45
+ end
46
+
47
+ def failure_message
48
+ message( true )
41
49
  end
42
50
 
43
- def failure_message_for_should_not
44
- failure_message( false )
51
+ def failure_message_when_negated
52
+ message( false )
45
53
  end
46
54
 
47
55
  def description
48
56
  msg = "call #{@methods.join(", ")}"
49
57
  msg << " #{@kind} #{@operation}" if @operation
50
- msg << " on #{@on}" if @on
58
+ msg << " on #{@context}" if @context
51
59
  msg
52
60
  end
53
61
 
54
62
  protected
55
- def failure_message( should )
56
- return "Invalid operation. Use :initialize, :build, :validation,"\
57
- ":create, :find, :update, :upsert, :save or :destroy" if @no_op
58
-
59
- if @kind
60
- msg = "Expected method#{@methods.size > 1 ? 's' : ''} #{@methods.join(", ")} #{should ? '' : 'not ' }to be called"
61
- msg << " #{@kind} #{@operation}" if @operation
62
- msg << " on #{@on}" if @on
63
- msg << ( @guess ? ", but got method #{@guess.filter} called" : ", but no callback found" )
64
- msg << " #{@guess.kind} #{@operation}" if @guess
65
- msg << " on #{@guess.options[:on]}" if @guess and @guess.options[:on]
66
-
67
- msg
68
- else
69
- "Callback#{@methods.size > 1 ? 's' : '' } #{@methods.join(", ")} can"\
70
- "not be tested against undefined lifecycle. Use .before, .after or .around"
63
+ def message( should )
64
+ return "Invalid operation. Use :initialize, :build, :validation,"\
65
+ ":create, :find, :update, :upsert, :save or :destroy" if @no_op
66
+
67
+ if @kind
68
+ msg = "Expected method#{@methods.size > 1 ? 's' : ''} #{@methods.join(", ")} #{should ? '' : 'not ' }to be called"
69
+ msg << " #{@kind} #{@operation}" if @operation
70
+ msg << " on #{@context}" if @context
71
+ msg << ( @guess ? ", but got method #{@guess.filter} called" : ", but no callback found" )
72
+ msg << " #{@guess.kind} #{@operation}" if @guess
73
+ msg << " on another context" if @guess and !@context_match
74
+
75
+ msg
76
+ else
77
+ "Callback#{@methods.size > 1 ? 's' : '' } #{@methods.join(", ")} can"\
78
+ "not be tested against undefined lifecycle. Use .before, .after or .around"
79
+ end
80
+ end
81
+
82
+ private
83
+ def check_filter?(callback, method)
84
+ callback.filter == method
85
+ end
86
+
87
+ def check_kind?(callback, kind)
88
+ callback.kind == kind
71
89
  end
72
- end
73
90
 
91
+ def check_context?(callback, context)
92
+ return true if !context
93
+
94
+ options = callback.instance_variable_get(:@if)
95
+ @context_match = options.select do |o|
96
+ o.is_a?(Proc)
97
+ end.detect do |o|
98
+ o.call(ValidationContext.new(context))
99
+ end
100
+ end
74
101
  end
75
102
 
103
+ ValidationContext = Struct.new :validation_context
104
+
76
105
  def callback( *args )
77
106
  HaveCallbackMatcher.new( *args )
78
107
  end
@@ -1,7 +1,7 @@
1
1
  module Mongoid
2
2
  module Rspec
3
3
  module Callbacks
4
- VERSION = "0.0.1"
4
+ VERSION = "1.0.0"
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,62 @@
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
+ # stub: mongoid-rspec-callbacks 1.0.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "mongoid-rspec-callbacks"
9
+ s.version = "1.0.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Luis Ciudad"]
14
+ s.date = "2016-03-09"
15
+ s.description = "This gem is meant to be use with mongoid-rpsec, altought it works by itself. Syntax is the same as shoulda-callback-matchers."
16
+ s.email = "lciugar@gmail.com"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ "COPYING",
23
+ "Gemfile",
24
+ "LICENSE",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/matchers/callbacks.rb",
29
+ "lib/mongoid-rspec-callbacks.rb",
30
+ "lib/mongoid-rspec-callbacks/version.rb",
31
+ "mongoid-rspec-callbacks.gemspec",
32
+ "spec/callbacks_spec.rb",
33
+ "spec/models/user.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = "http://github.com/nanocity/mongoid-rspec-callbacks"
37
+ s.licenses = ["GPLv3"]
38
+ s.rubygems_version = "2.4.8"
39
+ s.summary = "RSpec Callbacks matchers for Mongoid 3.X"
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 4
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<activesupport>, ["~> 4.2"])
46
+ s.add_runtime_dependency(%q<jeweler>, [">= 0"])
47
+ s.add_runtime_dependency(%q<rspec>, [">= 0"])
48
+ s.add_runtime_dependency(%q<mongoid>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<activesupport>, ["~> 4.2"])
51
+ s.add_dependency(%q<jeweler>, [">= 0"])
52
+ s.add_dependency(%q<rspec>, [">= 0"])
53
+ s.add_dependency(%q<mongoid>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<activesupport>, ["~> 4.2"])
57
+ s.add_dependency(%q<jeweler>, [">= 0"])
58
+ s.add_dependency(%q<rspec>, [">= 0"])
59
+ s.add_dependency(%q<mongoid>, [">= 0"])
60
+ end
61
+ end
62
+
@@ -2,9 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe "Callbacks" do
4
4
  describe User do
5
- it { should callback(:callback1).before(:save) }
6
- it { should callback(:callback2).after(:save) }
7
- it { should callback(:callback1, :callback2).before(:validation) }
8
- it { should callback(:callback3).after(:validation).on(:create) }
5
+ it { is_expected.to callback(:callback1).before(:save) }
6
+ it { is_expected.to callback(:callback2).after(:save) }
7
+ it { is_expected.to callback(:callback1, :callback2).before(:validation) }
8
+ it { is_expected.to callback(:callback3).after(:validation).on(:create) }
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,105 +1,115 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-rspec-callbacks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Luis Ciudad
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-16 00:00:00.000000000 Z
11
+ date: 2016-03-09 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- name: rake
14
+ name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jeweler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
20
32
  - !ruby/object:Gem::Version
21
33
  version: '0'
22
34
  type: :runtime
23
35
  prerelease: false
24
36
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
37
  requirements:
27
- - - ! '>='
38
+ - - ">="
28
39
  - !ruby/object:Gem::Version
29
40
  version: '0'
30
41
  - !ruby/object:Gem::Dependency
31
- name: mongoid
42
+ name: rspec
32
43
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
44
  requirements:
35
- - - ! '>='
45
+ - - ">="
36
46
  - !ruby/object:Gem::Version
37
- version: 3.0.1
47
+ version: '0'
38
48
  type: :runtime
39
49
  prerelease: false
40
50
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
51
  requirements:
43
- - - ! '>='
52
+ - - ">="
44
53
  - !ruby/object:Gem::Version
45
- version: 3.0.1
54
+ version: '0'
46
55
  - !ruby/object:Gem::Dependency
47
- name: rspec
56
+ name: mongoid
48
57
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
58
  requirements:
51
- - - ! '>='
59
+ - - ">="
52
60
  - !ruby/object:Gem::Version
53
- version: '2.9'
61
+ version: '0'
54
62
  type: :runtime
55
63
  prerelease: false
56
64
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
65
  requirements:
59
- - - ! '>='
66
+ - - ">="
60
67
  - !ruby/object:Gem::Version
61
- version: '2.9'
62
- description: Callbacks RSpec matches for Mongoid models
68
+ version: '0'
69
+ description: This gem is meant to be use with mongoid-rpsec, altought it works by
70
+ itself. Syntax is the same as shoulda-callback-matchers.
63
71
  email: lciugar@gmail.com
64
72
  executables: []
65
73
  extensions: []
66
- extra_rdoc_files: []
74
+ extra_rdoc_files:
75
+ - LICENSE
76
+ - README.md
67
77
  files:
68
- - .gitignore
69
- - .rvmrc
70
78
  - COPYING
71
79
  - Gemfile
72
80
  - LICENSE
73
81
  - README.md
74
82
  - Rakefile
83
+ - VERSION
75
84
  - lib/matchers/callbacks.rb
76
85
  - lib/mongoid-rspec-callbacks.rb
77
86
  - lib/mongoid-rspec-callbacks/version.rb
87
+ - mongoid-rspec-callbacks.gemspec
78
88
  - spec/callbacks_spec.rb
79
89
  - spec/models/user.rb
80
90
  - spec/spec_helper.rb
81
91
  homepage: http://github.com/nanocity/mongoid-rspec-callbacks
82
- licenses: []
92
+ licenses:
93
+ - GPLv3
94
+ metadata: {}
83
95
  post_install_message:
84
96
  rdoc_options: []
85
97
  require_paths:
86
98
  - lib
87
99
  required_ruby_version: !ruby/object:Gem::Requirement
88
- none: false
89
100
  requirements:
90
- - - ! '>='
101
+ - - ">="
91
102
  - !ruby/object:Gem::Version
92
103
  version: '0'
93
104
  required_rubygems_version: !ruby/object:Gem::Requirement
94
- none: false
95
105
  requirements:
96
- - - ! '>='
106
+ - - ">="
97
107
  - !ruby/object:Gem::Version
98
108
  version: '0'
99
109
  requirements: []
100
110
  rubyforge_project:
101
- rubygems_version: 1.8.25
111
+ rubygems_version: 2.4.8
102
112
  signing_key:
103
- specification_version: 3
104
- summary: Callbacks RSpec matchers for Mongoid
113
+ specification_version: 4
114
+ summary: RSpec Callbacks matchers for Mongoid 3.X
105
115
  test_files: []
data/.gitignore DELETED
@@ -1,6 +0,0 @@
1
- *.swp
2
- *.gem
3
- *.gemspec
4
- .rmvrc
5
- Gemfile.lock
6
- pkg/*
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm use --create 1.9.3@mongoid-rspec-callbacks