specstar-models 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/specstar/models.rb +107 -0
  2. metadata +45 -0
@@ -0,0 +1,107 @@
1
+ require 'rspec'
2
+
3
+ module Specstar
4
+ module Models
5
+ module Matchers
6
+ def properties_to_sentence(hash)
7
+ " of " + hash.map { |key, value| "#{key} #{value}" }.to_sentence if hash.present?
8
+ end
9
+
10
+ RSpec::Matchers.define :have_attribute do |attr|
11
+ attr = attr.to_s
12
+
13
+ @extras = nil
14
+ chain :with do |extras|
15
+ @extras = extras
16
+ end
17
+
18
+ match do |target|
19
+ result = target.attributes.include? attr
20
+
21
+ if result && @extras
22
+ properties = target.class.columns_hash[attr]
23
+ @extras.each_pair do |property, value|
24
+ result = false && break unless properties.send(property) == value
25
+ end
26
+ end
27
+
28
+ result
29
+ end
30
+
31
+ failure_message_for_should do |target|
32
+ "Expected #{target.class} to have an attribute '#{attr}'#{properties_to_sentence @extras}."
33
+ end
34
+
35
+ description do
36
+ "have an attribute '#{attr}'#{properties_to_sentence @extras}."
37
+ end
38
+ end
39
+
40
+ def has_attribute?(model, attr, extras={})
41
+ attr = attr.to_s
42
+
43
+ result = model.attributes.include? attr
44
+
45
+ if result && extras.any?
46
+ properties = model.class.columns_hash[attr]
47
+ extras.each_pair do |property, value|
48
+ result = false && break unless properties.send(property) == value
49
+ end
50
+ end
51
+
52
+ result
53
+ end
54
+
55
+ def has_association?(model, association)
56
+ model.class.reflect_on_all_associations.map { |a| a.name }.include? association.to_sym
57
+ end
58
+
59
+ RSpec::Matchers.define :validate_presence_of do |attr|
60
+ match do |model|
61
+ (has_attribute?(model, attr) || has_association?(model, attr)) &&
62
+ model._validators[attr].select { |validator| validator.instance_of? ActiveModel::Validations::PresenceValidator }.size > 0
63
+ end
64
+
65
+ failure_message_for_should do |model|
66
+ if has_attribute?(model, attr) || has_association?(model, attr)
67
+ "expected #{model.class} to validate presence of #{attr}."
68
+ else
69
+ "expected #{model.class} to have an attribute or association #{attr}."
70
+ end
71
+ end
72
+ end
73
+
74
+ RSpec::Matchers.define :validate_uniqueness_of do |attr|
75
+ match do |model|
76
+ (has_attribute?(model, attr) || has_association?(model, attr)) &&
77
+ model._validators[attr].select { |validator| validator.instance_of? ActiveRecord::Validations::UniquenessValidator }.size > 0
78
+ end
79
+
80
+ failure_message_for_should do |model|
81
+ if has_attribute?(model, attr) || has_association?(model, attr)
82
+ "expected #{model.class} to validate uniqueness of #{attr}."
83
+ else
84
+ "expected #{model.class} to have an attribute or association #{attr}."
85
+ end
86
+ end
87
+ end
88
+
89
+ RSpec::Matchers.define :validate_inclusion_of do |attr, options|
90
+ match do |model|
91
+ (has_attribute?(model, attr) || has_association?(model, attr)) &&
92
+ model._validators[attr].select do |validator|
93
+ validator.instance_of?(ActiveModel::Validations::InclusionValidator) && validator.options.merge(options) == validator.options
94
+ end.size > 0
95
+ end
96
+
97
+ failure_message_for_should do |model|
98
+ if has_attribute?(model, attr) || has_association?(model, attr)
99
+ "expected #{model.class} to validate inclusion of #{attr} in [#{options.delete(:in).map(&:to_s).join(', ')}]."
100
+ else
101
+ "expected #{model.class} to have an attribute or association #{attr}."
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: specstar-models
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sujoy Gupta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: sujoyg@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/specstar/models.rb
21
+ homepage: http://github.com/sujoyg/specstar-models
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: RSpec helpers for models.
45
+ test_files: []