daily_affirmation 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3a8c603a904381accd3632ef5a72f8b0fdb88d7f
4
+ data.tar.gz: 92b4e916aefb6589644c63cde10a38e820768631
5
+ SHA512:
6
+ metadata.gz: fc3b958edda00cdde56f72e407d93a039e2b37ba304d47200643c5a99246912b2f8babb2664f492004c9f017f7dff32c55ca77e73d0920a3c0fbcfeb9c284c6c
7
+ data.tar.gz: 10be4946fe5db48e8794ca8dd3c0efaadee1f26de108121a7101d10f2befd2faa7c01216d6b6099644d59e99bf883a8c94c798491a130308771285e4ffa2bee0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "coveralls", :require => false
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TeamSnap
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # DailyAffirmation
2
+
3
+ [![Semaphore](https://semaphoreapp.com/api/v1/projects/7268def52c792e50bfc60e1b1a6e905ed4e2a80d/118940/shields_badge.png)](https://semaphoreapp.com/minter/daily_affirmation)
4
+ [![Code Climate](https://codeclimate.com/github/teamsnap/daily_affirmation.png)](https://codeclimate.com/github/teamsnap/daily_affirmation)
5
+ [![Coverage Status](https://coveralls.io/repos/teamsnap/daily_affirmation/badge.png?branch=master)](https://coveralls.io/r/teamsnap/daily_affirmation?branch=master)
6
+ [![Dependency Status](https://gemnasium.com/teamsnap/daily_affirmation.png)](https://gemnasium.com/teamsnap/daily_affirmation)
7
+ [![License](http://img.shields.io/license/MIT.png?color=green)](http://opensource.org/licenses/MIT)
8
+
9
+ A simple library for external validations of POROs
10
+
11
+ ![Daily Affirmation](http://i.imgur.com/rdvgFAK.jpg)
12
+
13
+ There are currently two ways to validate objects in Rails, ActiveRecord based validates and ActiveModel based validations. Both require them to be placed directly into your models, making for more heavy weight, less flexible code. DailyAffirmation allows you to validate your models (or any PORO) externally.
14
+
15
+ By validating objects externally, you can have different validations depending on your code path. Perhaps you require a password for a User object when it's being created, but do not need it again when it's being updated. Simply create a UserCreationAffirmation and a UserChangeAffirmation with the correct validations.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ gem 'daily_affirmation'
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install daily_affirmation
30
+
31
+ ## Usage
32
+
33
+ require "daily_affirmations"
34
+
35
+ class Team
36
+ attr_accessor :name, :status_cd
37
+ end
38
+
39
+ class TeamAffirmation
40
+ include DailyAffirmation.affirmations
41
+
42
+ affirms_presence_of :name
43
+ affirms_inclusion_of :status_cd, :list => 0..2
44
+ end
45
+
46
+ team1 = OpenStruct.new(:name => "", :status_cd => 3)
47
+ TeamAffirmation.new(team1).valid? #=> false
48
+
49
+ ## Roadmap
50
+
51
+ - Provide all the validations ActiveModel does
52
+ - Add a way to build custom validations (example validating a password)
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'daily_affirmation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "daily_affirmation"
8
+ spec.version = DailyAffirmation::VERSION
9
+ spec.authors = ["Shane Emmons"]
10
+ spec.email = ["oss@teamsnap.com"]
11
+ spec.description = "A simple library for external validations of POROs"
12
+ spec.summary = "A simple library for external validations of POROs"
13
+ spec.homepage = "https://github.com/teamsnap/daily_affirmation"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 3.0.0.beta1"
24
+ end
@@ -0,0 +1,88 @@
1
+ module DailyAffirmation
2
+ module Affirmations
3
+ def self.included(descendant)
4
+ descendant.extend(ClassMethods)
5
+ end
6
+
7
+ def initialize(object)
8
+ self.object = object
9
+ end
10
+
11
+ def valid?
12
+ validate[0]
13
+ end
14
+
15
+ def validate
16
+ @validate ||= [
17
+ affirmations.map(&:first).all?,
18
+ affirmations.map(&:last).compact
19
+ ]
20
+ end
21
+
22
+ def error_messages
23
+ validate[1]
24
+ end
25
+
26
+ private
27
+
28
+ attr_accessor :object
29
+
30
+ def affirmations
31
+ @affirmations ||= self.class.affirmations
32
+ .map { |affirmation| affirm(affirmation) }
33
+ end
34
+
35
+ def affirm(affirmation)
36
+ method = "affirm_#{affirmation[:type]}_of"
37
+ attribute = affirmation[:attribute]
38
+ args = affirmation.reject { |k, _| [:type, :attribute].include?(k) }
39
+ send(method, attribute, args)
40
+ end
41
+
42
+ def affirm_presence_of(attribute, _ = {})
43
+ if present?(attribute)
44
+ [true, nil]
45
+ else
46
+ [false, "#{attribute} is not present"]
47
+ end
48
+ end
49
+
50
+ def affirm_inclusion_of(attribute, list: [])
51
+ if list.include?(object.send(attribute))
52
+ [true, nil]
53
+ else
54
+ [false, "#{attribute} is not included in #{list}"]
55
+ end
56
+ end
57
+
58
+ def blank?(attribute)
59
+ value = object.send(attribute)
60
+ case value
61
+ when String
62
+ value !~ /[^[:space:]]/
63
+ else
64
+ value.respond_to?(:empty?) ? value.empty? : !value
65
+ end
66
+ end
67
+
68
+ def present?(attribute)
69
+ !blank?(attribute)
70
+ end
71
+
72
+ module ClassMethods
73
+ def affirms_presence_of(attribute)
74
+ affirmations << {:attribute => attribute, :type => :presence}
75
+ end
76
+
77
+ def affirms_inclusion_of(attribute, list: [])
78
+ affirmations << {
79
+ :attribute => attribute, :type => :inclusion, :list => list
80
+ }
81
+ end
82
+
83
+ def affirmations
84
+ @affirmations ||= []
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,3 @@
1
+ module DailyAffirmation
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "daily_affirmation/affirmations"
2
+ require_relative "daily_affirmation/version"
3
+
4
+ module DailyAffirmation
5
+ def self.affirmations
6
+ Module.new do
7
+ def self.included(descendant)
8
+ descendant.send(:include, ::DailyAffirmation::Affirmations)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,140 @@
1
+ require_relative "spec_helper"
2
+ require_relative "../lib/daily_affirmation"
3
+
4
+ describe DailyAffirmation do
5
+ describe ".affirms_presence_of" do
6
+ let(:cls) do
7
+ Class.new do
8
+ include DailyAffirmation.affirmations
9
+
10
+ affirms_presence_of :name
11
+ end
12
+ end
13
+
14
+ it "fails validation if attribute is nil" do
15
+ obj = double(:name => nil)
16
+
17
+ affirmation = cls.new(obj)
18
+ expect(affirmation).to_not be_valid
19
+ end
20
+
21
+ it "fails validation if attribute is empty" do
22
+ obj = double(:name => " ")
23
+
24
+ affirmation = cls.new(obj)
25
+ expect(affirmation).to_not be_valid
26
+ end
27
+
28
+ it "passes validation if attribute is present" do
29
+ obj = double(:name => :foo)
30
+
31
+ affirmation = cls.new(obj)
32
+ expect(affirmation).to be_valid
33
+ end
34
+ end
35
+
36
+ describe ".affirms_inclusion_of" do
37
+ let(:cls) do
38
+ Class.new do
39
+ include DailyAffirmation.affirmations
40
+
41
+ affirms_inclusion_of :age, :list => 13..18
42
+ end
43
+ end
44
+
45
+ it "fails validation if attribute is not in the list" do
46
+ obj = double(:age => 12)
47
+
48
+ affirmation = cls.new(obj)
49
+ expect(affirmation).to_not be_valid
50
+ end
51
+
52
+ it "passes validation if attribute is in the list" do
53
+ obj = double(:age => 13)
54
+
55
+ affirmation = cls.new(obj)
56
+ expect(affirmation).to be_valid
57
+ end
58
+ end
59
+
60
+ describe "#validate" do
61
+ let(:cls) do
62
+ Class.new do
63
+ include DailyAffirmation.affirmations
64
+
65
+ affirms_presence_of :name
66
+ affirms_inclusion_of :age, :list => 13..18
67
+ end
68
+ end
69
+
70
+ it "returns if object is valid as the first result" do
71
+ obj = double(:name => :foo, :age => 13)
72
+ affirmation = cls.new(obj)
73
+
74
+ is_valid = affirmation.validate[0]
75
+ expect(is_valid).to eq(true)
76
+ end
77
+
78
+ it "returns validation error messages as the second result" do
79
+ obj = double(:name => "", :age => 12)
80
+ affirmation = cls.new(obj)
81
+
82
+ messages = affirmation.validate[1]
83
+ expect(messages).to include("name is not present")
84
+ expect(messages).to include("age is not included in 13..18")
85
+ end
86
+ end
87
+
88
+ describe "#valid" do
89
+ let(:cls) do
90
+ Class.new do
91
+ include DailyAffirmation.affirmations
92
+
93
+ affirms_presence_of :name
94
+ affirms_inclusion_of :age, :list => 13..18
95
+ end
96
+ end
97
+
98
+ it "returns true if the object is valid" do
99
+ obj = double(:name => :foo, :age => 13)
100
+ affirmation = cls.new(obj)
101
+
102
+ expect(affirmation).to be_valid
103
+ end
104
+
105
+ it "returns false if the object is not valid" do
106
+ obj = double(:name => "", :age => 12)
107
+ affirmation = cls.new(obj)
108
+
109
+ expect(affirmation).to_not be_valid
110
+ end
111
+ end
112
+
113
+ describe "error_messages" do
114
+ let(:cls) do
115
+ Class.new do
116
+ include DailyAffirmation.affirmations
117
+
118
+ affirms_presence_of :name
119
+ affirms_inclusion_of :age, :list => 13..18
120
+ end
121
+ end
122
+
123
+ it "returns an array of error message if the object is not valid" do
124
+ obj = double(:name => "", :age => 12)
125
+ affirmation = cls.new(obj)
126
+ messages = affirmation.error_messages
127
+
128
+ expect(messages).to include("name is not present")
129
+ expect(messages).to include("age is not included in 13..18")
130
+ end
131
+
132
+ it "returns an empty array if the object is valid" do
133
+ obj = double(:name => :foo, :age => 13)
134
+ affirmation = cls.new(obj)
135
+ messages = affirmation.error_messages
136
+
137
+ expect(messages).to eq([])
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,6 @@
1
+ require "coveralls"
2
+ Coveralls.wear!
3
+
4
+ RSpec.configure do |config|
5
+ config.order = "random"
6
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: daily_affirmation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shane Emmons
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0.beta1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0.beta1
55
+ description: A simple library for external validations of POROs
56
+ email:
57
+ - oss@teamsnap.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - daily_affirmation.gemspec
68
+ - lib/daily_affirmation.rb
69
+ - lib/daily_affirmation/affirmations.rb
70
+ - lib/daily_affirmation/version.rb
71
+ - spec/daily_affirmation_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: https://github.com/teamsnap/daily_affirmation
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.14
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A simple library for external validations of POROs
97
+ test_files:
98
+ - spec/daily_affirmation_spec.rb
99
+ - spec/spec_helper.rb