tiny_validations 0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d18679f078afeb4b6a651297b270735f70e2fa1
4
+ data.tar.gz: 7a05aca0063dc2bc09601e9d306b6a0468b93045
5
+ SHA512:
6
+ metadata.gz: 74743f076bc27cb88816f80172a0905228b4e92ed698b3ae6c5cda0823a1af2e6c3111d20302b481d8c19b8d2fb082c678462b9f893ce2ad8b07a09b8a80baef
7
+ data.tar.gz: 4517371a447ca864f18cf125b90559699c9d07660cd617be5c840ae50a9f09b1bd346baa4e9559298d612ceeab3dd822675225aca70dc38e149590d979ee7865
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.rbc
2
+ *.sassc
3
+ .sass-cache
4
+ capybara-*.html
5
+ .rspec
6
+ .rvmrc
7
+ /.bundle
8
+ /vendor/bundle
9
+ /log/*
10
+ /tmp/*
11
+ /db/*.sqlite3
12
+ /public/system/*
13
+ /coverage/
14
+ /spec/tmp/*
15
+ **.orig
16
+ rerun.txt
17
+ pickle-email-*.html
18
+ .project
19
+ config/initializers/secret_token.rb
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ script: bundle exec rake test
5
+ before_script:
6
+ - psql -c 'create database tiny_validation_test;' -U postgres
7
+ notifications:
8
+ email:
9
+ on_success: never
10
+ on_failure: change
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,91 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tiny_validations (0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionmailer (4.0.3)
10
+ actionpack (= 4.0.3)
11
+ mail (~> 2.5.4)
12
+ actionpack (4.0.3)
13
+ activesupport (= 4.0.3)
14
+ builder (~> 3.1.0)
15
+ erubis (~> 2.7.0)
16
+ rack (~> 1.5.2)
17
+ rack-test (~> 0.6.2)
18
+ activemodel (4.0.3)
19
+ activesupport (= 4.0.3)
20
+ builder (~> 3.1.0)
21
+ activerecord (4.0.3)
22
+ activemodel (= 4.0.3)
23
+ activerecord-deprecated_finders (~> 1.0.2)
24
+ activesupport (= 4.0.3)
25
+ arel (~> 4.0.0)
26
+ activerecord-deprecated_finders (1.0.3)
27
+ activesupport (4.0.3)
28
+ i18n (~> 0.6, >= 0.6.4)
29
+ minitest (~> 4.2)
30
+ multi_json (~> 1.3)
31
+ thread_safe (~> 0.1)
32
+ tzinfo (~> 0.3.37)
33
+ arel (4.0.2)
34
+ atomic (1.1.15)
35
+ builder (3.1.4)
36
+ erubis (2.7.0)
37
+ hike (1.2.3)
38
+ i18n (0.6.9)
39
+ mail (2.5.4)
40
+ mime-types (~> 1.16)
41
+ treetop (~> 1.4.8)
42
+ mime-types (1.25.1)
43
+ minitest (4.7.5)
44
+ multi_json (1.8.4)
45
+ pg (0.17.1)
46
+ polyglot (0.3.4)
47
+ rack (1.5.2)
48
+ rack-test (0.6.2)
49
+ rack (>= 1.0)
50
+ rails (4.0.3)
51
+ actionmailer (= 4.0.3)
52
+ actionpack (= 4.0.3)
53
+ activerecord (= 4.0.3)
54
+ activesupport (= 4.0.3)
55
+ bundler (>= 1.3.0, < 2.0)
56
+ railties (= 4.0.3)
57
+ sprockets-rails (~> 2.0.0)
58
+ railties (4.0.3)
59
+ actionpack (= 4.0.3)
60
+ activesupport (= 4.0.3)
61
+ rake (>= 0.8.7)
62
+ thor (>= 0.18.1, < 2.0)
63
+ rake (10.1.1)
64
+ sprockets (2.11.0)
65
+ hike (~> 1.2)
66
+ multi_json (~> 1.0)
67
+ rack (~> 1.0)
68
+ tilt (~> 1.1, != 1.3.0)
69
+ sprockets-rails (2.0.1)
70
+ actionpack (>= 3.0)
71
+ activesupport (>= 3.0)
72
+ sprockets (~> 2.8)
73
+ thor (0.18.1)
74
+ thread_safe (0.2.0)
75
+ atomic (>= 1.1.7, < 2)
76
+ tilt (1.4.1)
77
+ treetop (1.4.15)
78
+ polyglot
79
+ polyglot (>= 0.3.1)
80
+ tzinfo (0.3.38)
81
+
82
+ PLATFORMS
83
+ ruby
84
+
85
+ DEPENDENCIES
86
+ bundler (~> 1.3)
87
+ minitest (~> 4.7)
88
+ pg
89
+ rails
90
+ rake
91
+ tiny_validations!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Pixelbits
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Context Validations
2
+
3
+ ```ruby
4
+ class User < ActiveRecord::Base
5
+ include ContextValidations
6
+
7
+ validates :name, :email, presence: true
8
+
9
+ validations_when_not :email_subscription do |model|
10
+ model.validates :city, :state, :country, presence: true
11
+ end
12
+
13
+ validations_when :upgrading_account do |model|
14
+ model.validates_presence_of :credit_card_number
15
+ end
16
+ end
17
+
18
+ @user = user.new(name: 'Nando Sousa', email: 'nandosousafr@gmail.com')
19
+ @ser.context = :email_subscription
20
+ @user.save #=> true
21
+
22
+ @user.context = :upgrading_account
23
+ @user.valid? #=> false
24
+
25
+ @user.credit_card = '324324-242342-2342423-232423'
26
+ @user.save #=> true
27
+
28
+
29
+
30
+
31
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task :default => :test
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
@@ -0,0 +1,47 @@
1
+ require 'rails'
2
+ require 'active_support'
3
+ require 'active_record'
4
+
5
+ module TinyValidations
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ attr_accessor :context_validation
10
+ end
11
+
12
+ def validations_when(key , &block)
13
+ validations_base(key, &block)
14
+ end
15
+
16
+ def validations_when_not(key, &block)
17
+ validations_base(key, :unless, &block)
18
+ end
19
+
20
+ def validations_base( key, condition = :if, &block)
21
+ options = {}
22
+ options[condition] = -> { context_key(key) == context_key(context_validation) }
23
+
24
+ instance_eval do
25
+ attr_accessor context_key(key)
26
+
27
+ with_options(options) do |validations|
28
+ yield validations
29
+ end
30
+ end
31
+ end
32
+
33
+ def context=(key)
34
+ if self.respond_to?(context_key(key))
35
+ self.send("context_validation=", key)
36
+ else
37
+ raise 'Context not found'
38
+ end
39
+ end
40
+
41
+ private
42
+ def context_key(key)
43
+ "context_validations_#{key}".to_sym
44
+ end
45
+ end
46
+
47
+ ActiveRecord::Base.send(:extend, TinyValidations)
@@ -0,0 +1,3 @@
1
+ module TinyValidations
2
+ VERSION = 0.1
3
+ end
@@ -0,0 +1,61 @@
1
+ require 'bundler/setup'
2
+ Bundler.require(:default)
3
+ require 'minitest/autorun'
4
+ require 'minitest/pride'
5
+
6
+ ENV['RACK_ENV'] = 'test'
7
+
8
+ require 'active_record'
9
+
10
+ # for debugging
11
+ # ActiveRecord::Base.logger = Logger.new(STDOUT)
12
+
13
+ # rails does this in activerecord/lib/active_record/railtie.rb
14
+ ActiveRecord::Base.default_timezone = :utc
15
+ ActiveRecord::Base.time_zone_aware_attributes = true
16
+
17
+ # migrations
18
+ ActiveRecord::Base.establish_connection :adapter => 'postgresql', :database => 'tiny_validation_test'
19
+
20
+ ActiveRecord::Migration.create_table :users, force: true do |t|
21
+ t.string :name
22
+ t.string :email
23
+ t.string :city
24
+ t.string :state
25
+ t.string :password
26
+ t.string :fb_token_secret
27
+
28
+ t.timestamps
29
+ end
30
+
31
+
32
+ class User < ActiveRecord::Base
33
+ include TinyValidations
34
+
35
+ validates :name, :email, presence: true
36
+
37
+ validations_when_not :password_reset do |m|
38
+ m.validates :city, :state, presence: true
39
+ m.validates_inclusion_of :state, in: ['NY']
40
+ end
41
+
42
+ validations_when :facebook_auth do |m|
43
+ m.validates_presence_of :fb_token_secret
44
+ end
45
+
46
+ validations_when :password_reset do |m|
47
+ m.validates :password, presence: true
48
+ end
49
+
50
+ end
51
+
52
+
53
+ class Minitest::Unit::TestCase
54
+ def setup
55
+ User.destroy_all
56
+ @user = User.new(name: 'Nando Sousa',
57
+ email: 'nandosousafr@gmail.com')
58
+
59
+ end
60
+ end
61
+
@@ -0,0 +1,27 @@
1
+ require_relative 'test_helper'
2
+
3
+ class ValidationsTest < Minitest::Unit::TestCase
4
+
5
+ def test_set_contexts
6
+ assert @user.respond_to?(:context_validations_password_reset)
7
+ assert @user.respond_to?(:context_validations_facebook_auth)
8
+
9
+ assert @user.respond_to?(:context_validation)
10
+ end
11
+
12
+ def test_validatios_when
13
+ # context :password_reset
14
+ @user.context = :password_reset
15
+ @user.password = 'pass19822'
16
+ assert @user.valid?
17
+
18
+ #context :facebook_auth
19
+ @user.context = :facebook_auth
20
+ @user.fb_token_secret = 'fff22111fff'
21
+ assert_equal false, @user.valid?
22
+
23
+ @user.city = 'New York'
24
+ @user.state = 'NY'
25
+ assert @user.valid?
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tiny_validations/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'tiny_validations'
8
+ spec.version = TinyValidations::VERSION
9
+ spec.authors = ['Nando Sousa']
10
+ spec.email = ['nandosousafr@gmail.com']
11
+ spec.description = %q{Context Validations to Rails models}
12
+ spec.summary = %q{Context Validations to Rails models}
13
+ spec.homepage = 'https://github.com/pixelbits/tiny_validations'
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 'minitest', '~> 4.7'
24
+ spec.add_development_dependency 'pg'
25
+ spec.add_development_dependency 'rails'
26
+ end
27
+
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_validations
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Nando Sousa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-03 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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Context Validations to Rails models
84
+ email:
85
+ - nandosousafr@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - lib/tiny_validations.rb
98
+ - lib/tiny_validations/version.rb
99
+ - test/test_helper.rb
100
+ - test/validations_test.rb
101
+ - tiny_validations.gemspec
102
+ homepage: https://github.com/pixelbits/tiny_validations
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.1
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Context Validations to Rails models
126
+ test_files:
127
+ - test/test_helper.rb
128
+ - test/validations_test.rb