daily_affirmation 0.8.2 → 1.0.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.
- checksums.yaml +4 -4
- data/README.md +3 -6
- data/daily_affirmation.gemspec +1 -1
- data/lib/daily_affirmation.rb +1 -0
- data/lib/daily_affirmation/affirmations.rb +15 -0
- data/lib/daily_affirmation/validator.rb +30 -0
- data/lib/daily_affirmation/validators/absence_validator.rb +1 -0
- data/lib/daily_affirmation/validators/acceptance_validator.rb +1 -0
- data/lib/daily_affirmation/validators/confirmation_validator.rb +2 -0
- data/lib/daily_affirmation/validators/custom_validator.rb +4 -0
- data/lib/daily_affirmation/validators/date_validator.rb +8 -0
- data/lib/daily_affirmation/validators/equality_validator.rb +4 -0
- data/lib/daily_affirmation/validators/exclusion_validator.rb +5 -0
- data/lib/daily_affirmation/validators/format_validator.rb +3 -0
- data/lib/daily_affirmation/validators/inclusion_validator.rb +3 -0
- data/lib/daily_affirmation/validators/length_validator.rb +5 -0
- data/lib/daily_affirmation/validators/numericality_validator.rb +8 -0
- data/lib/daily_affirmation/validators/presence_validator.rb +1 -0
- data/lib/daily_affirmation/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1cc1e1ce0b58bfda7c7d73f8d28f03e1e5e74756
|
4
|
+
data.tar.gz: bbb527d49af075c5512336fbde9a1139f85c840f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 08ce5f23b0c2911bd124818fd1a71ab052db502027d8d72f146c5980b0c07ad2d9ceb42e0761488dc93674bd6df083d97a1a58dc4f16826d30a4736e2bfbb172
|
7
|
+
data.tar.gz: 02eff11856d22c91fc861630c34210f5b02e4b40d7adcf520853ff657f82971d3e8cb8ae303628e59d1a785ea13c957d9692898ad93c89554a1a276200a73e56
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# DailyAffirmation
|
2
2
|
|
3
|
-
[
|
3
|
+
[](https://www.codeship.io/projects/24759)
|
4
4
|
|
5
5
|
[](http://badge.fury.io/rb/daily_affirmation)
|
6
6
|
[](https://codeclimate.com/github/teamsnap/daily_affirmation)
|
@@ -8,6 +8,8 @@
|
|
8
8
|
[](https://gemnasium.com/teamsnap/daily_affirmation)
|
9
9
|
[](http://opensource.org/licenses/MIT)
|
10
10
|
|
11
|
+
[Documentation](http://www.rubydoc.info/github/teamsnap/daily_affirmation)
|
12
|
+
|
11
13
|
A simple library for external validations of POROs
|
12
14
|
|
13
15
|

|
@@ -49,11 +51,6 @@ end
|
|
49
51
|
team1 = OpenStruct.new(:name => "", :status_cd => 3)
|
50
52
|
TeamAffirmation.new(team1).valid? #=> false
|
51
53
|
```
|
52
|
-
|
53
|
-
## Roadmap
|
54
|
-
|
55
|
-
- Allow error messages to use i18n.
|
56
|
-
- Add a way to build custom validations (example validating a password).
|
57
54
|
|
58
55
|
## Contributing
|
59
56
|
|
data/daily_affirmation.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["oss@teamsnap.com"]
|
11
11
|
spec.description = "A simple library for external validations of POROs"
|
12
12
|
spec.summary = "A simple library for external validations of POROs"
|
13
|
-
spec.homepage = "
|
13
|
+
spec.homepage = "http://teamsnap.github.io/daily_affirmation"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
data/lib/daily_affirmation.rb
CHANGED
@@ -17,6 +17,7 @@ require_relative "daily_affirmation/version"
|
|
17
17
|
module DailyAffirmation
|
18
18
|
OptionError = Class.new(StandardError)
|
19
19
|
|
20
|
+
# Include DailyAffirmation.affirmations to make your object a validator.
|
20
21
|
def self.affirmations
|
21
22
|
Module.new do
|
22
23
|
def self.included(descendant)
|
@@ -4,14 +4,26 @@ module DailyAffirmation
|
|
4
4
|
descendant.extend(ClassMethods)
|
5
5
|
end
|
6
6
|
|
7
|
+
# Creates a new instance of the validator.
|
8
|
+
#
|
9
|
+
# @param object [Object] the object to validate.
|
10
|
+
# @return [self]
|
7
11
|
def initialize(object)
|
8
12
|
self.object = object
|
9
13
|
end
|
10
14
|
|
15
|
+
# Tells you if the object is valid based on the affirmations.
|
16
|
+
#
|
17
|
+
# @return [true, false]
|
11
18
|
def valid?
|
12
19
|
validate[0]
|
13
20
|
end
|
14
21
|
|
22
|
+
# Returns an array of length 2 telling you if the object is valid, along
|
23
|
+
# with any error messages.
|
24
|
+
#
|
25
|
+
# @return [Array(Boolean, Array<String>)] Array of length 2 containing
|
26
|
+
# validation results.
|
15
27
|
def validate
|
16
28
|
@validate ||= [
|
17
29
|
affirmations.map(&:first).all?,
|
@@ -19,6 +31,9 @@ module DailyAffirmation
|
|
19
31
|
]
|
20
32
|
end
|
21
33
|
|
34
|
+
# Returns an array of error message if any, otherwise an empty array.
|
35
|
+
#
|
36
|
+
# @return [Array<String>]
|
22
37
|
def error_messages
|
23
38
|
validate[1]
|
24
39
|
end
|
@@ -1,5 +1,18 @@
|
|
1
1
|
module DailyAffirmation
|
2
2
|
class Validator
|
3
|
+
# Initializes a new validator to validate the given object/attribute
|
4
|
+
# combination.
|
5
|
+
#
|
6
|
+
# @param object [Object] the object to validate.
|
7
|
+
# @param attribute [Symbol] the attribute to validate. The object must
|
8
|
+
# `respond_to?` a method with the name `attribute` with no arguments.
|
9
|
+
# @param opts [Hash] any special options related to the affirmation.
|
10
|
+
# @option opts [Proc] :if evaluated before affirmation to ensure we should
|
11
|
+
# run validation.
|
12
|
+
# @option opts [true, false] :allow_nil determines if we skip validation of
|
13
|
+
# `nil` valued attributes.
|
14
|
+
#
|
15
|
+
# @return [self]
|
3
16
|
def initialize(object, attribute, opts = {})
|
4
17
|
self.object = object
|
5
18
|
self.attribute = attribute
|
@@ -7,14 +20,31 @@ module DailyAffirmation
|
|
7
20
|
self.opts = opts
|
8
21
|
end
|
9
22
|
|
23
|
+
# Returns an array of length 2 telling you if the object passes this
|
24
|
+
# affirmation along with an error message if it doesn't.
|
25
|
+
#
|
26
|
+
# @return [Array(Boolean, [nil, String])] Array of length 2 containing
|
27
|
+
# validation results.
|
10
28
|
def affirm
|
11
29
|
@affirm ||= [valid?, valid? ? nil : error_message]
|
12
30
|
end
|
13
31
|
|
32
|
+
# Tells you if the object is valid based on this affirmation.
|
33
|
+
#
|
34
|
+
# @note Subclasses of DailyAffirmation::Validator must implement this
|
35
|
+
# method.
|
36
|
+
#
|
37
|
+
# @return [true, false]
|
14
38
|
def valid?
|
15
39
|
raise StandardError, "must implement #valid?"
|
16
40
|
end
|
17
41
|
|
42
|
+
# Returns the error message related to this validation.
|
43
|
+
#
|
44
|
+
# @note This method will always return the associated error message, even
|
45
|
+
# if the object passes validation.
|
46
|
+
#
|
47
|
+
# @return [String]
|
18
48
|
def error_message
|
19
49
|
i18n_error_message(:none)
|
20
50
|
end
|
@@ -2,6 +2,8 @@ require_relative "../validator"
|
|
2
2
|
|
3
3
|
module DailyAffirmation
|
4
4
|
module Validators
|
5
|
+
# This affirmation ensures a related attribute *_confirmation has the same
|
6
|
+
# value.
|
5
7
|
class ConfirmationValidator < Validator
|
6
8
|
def valid?
|
7
9
|
@valid ||= value == object.send("#{attribute}_confirmation")
|
@@ -2,6 +2,10 @@ require_relative "../validator"
|
|
2
2
|
|
3
3
|
module DailyAffirmation
|
4
4
|
module Validators
|
5
|
+
# This affirmation accepts a :proc option and evaluates it's results.
|
6
|
+
#
|
7
|
+
# @option opts [Proc] :proc evaluated to determine if attribute should be
|
8
|
+
# validated.
|
5
9
|
class CustomValidator < Validator
|
6
10
|
def valid?
|
7
11
|
@valid ||= opts[:proc].call(object)
|
@@ -2,6 +2,14 @@ require_relative "../validator"
|
|
2
2
|
|
3
3
|
module DailyAffirmation
|
4
4
|
module Validators
|
5
|
+
# This affirmation ensures an attribute is a date and if a :before or
|
6
|
+
# :after option is given, that the attribute is before or after the
|
7
|
+
# selected date.
|
8
|
+
#
|
9
|
+
# @option opts [Date] :before if present checked to ensure value is before
|
10
|
+
# provided date.
|
11
|
+
# @option opts [Date] :after if present checked to ensure value is after
|
12
|
+
# provided date.
|
5
13
|
class DateValidator < Validator
|
6
14
|
class NullDateLike
|
7
15
|
def <(val); true; end
|
@@ -2,6 +2,10 @@ require_relative "../validator"
|
|
2
2
|
|
3
3
|
module DailyAffirmation
|
4
4
|
module Validators
|
5
|
+
# This affirmation ensures an attribute is equal to the :value option
|
6
|
+
# given.
|
7
|
+
#
|
8
|
+
# @option opts [Object] :value used to determine if attribute is equal
|
5
9
|
class EqualityValidator < Validator
|
6
10
|
def valid?
|
7
11
|
@valid ||= (opts[:value] == value)
|
@@ -3,6 +3,11 @@ require_relative "inclusion_validator"
|
|
3
3
|
|
4
4
|
module DailyAffirmation
|
5
5
|
module Validators
|
6
|
+
# This affirmation ensures an attribute is not in the provided :list
|
7
|
+
# option.
|
8
|
+
#
|
9
|
+
# @option opts [#include?] :list list of values the attribute must not
|
10
|
+
# match.
|
6
11
|
class ExclusionValidator < Validator
|
7
12
|
def valid?
|
8
13
|
@valid ||= !InclusionValidator.new(object, attribute, opts).valid?
|
@@ -2,6 +2,9 @@ require_relative "../validator"
|
|
2
2
|
|
3
3
|
module DailyAffirmation
|
4
4
|
module Validators
|
5
|
+
# This affirmation ensures an attribute matches the provided :regex option.
|
6
|
+
#
|
7
|
+
# @option opts [Regex] :regex the patten the attributes value must match.
|
5
8
|
class FormatValidator < Validator
|
6
9
|
def valid?
|
7
10
|
@valid ||= !!opts[:regex].match(value)
|
@@ -2,6 +2,9 @@ require_relative "../validator"
|
|
2
2
|
|
3
3
|
module DailyAffirmation
|
4
4
|
module Validators
|
5
|
+
# This affirmation ensures an attribute is in the provided :list option.
|
6
|
+
#
|
7
|
+
# @option opts [#include?] :list list of values the attribute must match.
|
5
8
|
class InclusionValidator < Validator
|
6
9
|
def valid?
|
7
10
|
@valid ||= opts[:list].include?(value)
|
@@ -2,6 +2,11 @@ require_relative "../validator"
|
|
2
2
|
|
3
3
|
module DailyAffirmation
|
4
4
|
module Validators
|
5
|
+
# This affirmation ensures an attribute's size is within the provided
|
6
|
+
# :range option.
|
7
|
+
#
|
8
|
+
# @option opts [Range] :range the range the size of the attribute's value
|
9
|
+
# must fit in.
|
5
10
|
class LengthValidator < Validator
|
6
11
|
def valid?
|
7
12
|
@valid ||= opts[:range].include?(value.size)
|
@@ -2,6 +2,14 @@ require_relative "../validator"
|
|
2
2
|
|
3
3
|
module DailyAffirmation
|
4
4
|
module Validators
|
5
|
+
# This affirmation ensures an attribute is a Number and if the
|
6
|
+
# :greater_than or :less_then option is given, that the attribute is
|
7
|
+
# greater than or less than the value provided.
|
8
|
+
#
|
9
|
+
# @option opts [Numeric] :greater_than the value the attribute's value must
|
10
|
+
# be greater than.
|
11
|
+
# @option opts [Numeric] :less_than the value the attribute's value must be
|
12
|
+
# less than.
|
5
13
|
class NumericalityValidator < Validator
|
6
14
|
def valid?
|
7
15
|
@valid ||= value ? numeric? && greater_than? && less_than? : true
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: daily_affirmation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shane Emmons
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -114,7 +114,7 @@ files:
|
|
114
114
|
- spec/validators/length_validator_spec.rb
|
115
115
|
- spec/validators/numericality_validator_spec.rb
|
116
116
|
- spec/validators/presence_validator_spec.rb
|
117
|
-
homepage:
|
117
|
+
homepage: http://teamsnap.github.io/daily_affirmation
|
118
118
|
licenses:
|
119
119
|
- MIT
|
120
120
|
metadata: {}
|