fragmented_validation 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
18
+ .rvmrc
19
+ spec/fragmented_validation.sqlite3
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fragmented_validation.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Paul Pacquing
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,47 @@
1
+ Fragmented Validation
2
+ ============
3
+
4
+ FragmentedValidation allows your model to only validate selected attributes.
5
+
6
+ Installation
7
+ ============
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'fragmented_validation'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install fragmented_validation
19
+
20
+ Usage
21
+ ============
22
+ ### Include the FragmentedValidation module by calling the 'fragmented_validation' in your model.
23
+ e.g.
24
+
25
+ class User < ActiveRecord::Base
26
+ fragmented_validation
27
+ ..
28
+ #### Validate single attribute
29
+
30
+ person.email_valid?
31
+
32
+ #### Validate multiple attributes
33
+
34
+ person.frag_validation_on(:username, :email)
35
+
36
+ or
37
+
38
+ person.frag_validation_except(:password)
39
+
40
+ Contributing
41
+ ============
42
+
43
+ 1. Fork it
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/fragmented_validation/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Paul Pacquing"]
6
+ gem.email = ["dominicpacquing@gmail.com"]
7
+ gem.description = %q{FragmentedValidation allows your model to only validate selected attributes.}
8
+ gem.summary = %q{}
9
+ gem.homepage = "https://github.com/dominicpacquing/fragmented_validation"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "fragmented_validation"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = FragmentedValidation::VERSION
17
+
18
+ gem.add_development_dependency "bundler", ">= 1.0.0"
19
+ gem.add_development_dependency "rspec", "~> 2.6"
20
+ gem.add_development_dependency "sqlite3-ruby"
21
+ gem.add_dependency "activerecord", "~> 3.0"
22
+ end
23
+
@@ -0,0 +1,47 @@
1
+ module FragmentedValidation
2
+ module Core
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+ end
7
+
8
+ module ClassMethods
9
+ def fragmented_validation
10
+ include InstanceMethods
11
+ attributes = self.new.attributes.keys
12
+ attributes.each do |key|
13
+ define_method "valid_#{key}?" do
14
+ frag_valid_on key
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ module InstanceMethods
21
+ def frag_valid_on(*attrs)
22
+ frag_validation(attrs)
23
+ end
24
+
25
+ def frag_valid_except(*attrs)
26
+ frag_validation(attributes.keys - attrs.map{|att| att.to_s})
27
+ end
28
+
29
+ def frag_validation(*attrs)
30
+ valid?
31
+ _errors = {}
32
+ attrs.flatten.uniq.each do |attr|
33
+ _errors[attr] = errors[attr]
34
+ end
35
+
36
+ errors.clear
37
+ _errors.each_pair do |key, values|
38
+ values.each do |val|
39
+ errors.add(key, val)
40
+ end
41
+ end
42
+
43
+ errors.empty?
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,3 @@
1
+ module FragmentedValidation
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_record'
2
+ require "fragmented_validation/version"
3
+ require 'fragmented_validation/core'
4
+
5
+ ActiveRecord::Base.send(:include, FragmentedValidation::Core)
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe FragmentedValidation do
4
+
5
+ let(:person) do
6
+ User.class_eval do
7
+ fragmented_validation
8
+ end
9
+ User.new
10
+ end
11
+
12
+ it "should generate valid_\#{attribute}? methods" do
13
+ person.should respond_to(:valid_username?, :valid_password?)
14
+ end
15
+
16
+ describe "#frag_validation" do
17
+ it "should only validate the passed attribute names" do
18
+ person.password = ""
19
+ person.username = ""
20
+
21
+ person.frag_validation(:username, :password).should be_false
22
+ person.errors[:email].should be_empty
23
+ person.errors[:password].should_not be_empty
24
+ person.errors[:username].should_not be_empty
25
+ end
26
+
27
+ it "should ignore unknown attributes" do
28
+ person.frag_validation(:ghost_method, :bad_method).should be_true
29
+ end
30
+ end
31
+
32
+ describe "#frag_valid_except" do
33
+ it "should ignore the attributes included in the parameter" do
34
+ person.password = ""
35
+ person.username = "myusername"
36
+ person.email = "test@mail.com"
37
+ person.frag_valid_except(:password).should be_true
38
+ end
39
+ end
40
+
41
+ describe "#frag_valid_on" do
42
+ it "should only validate the attributes which are included in the parameter" do
43
+ person.password = "mypassword"
44
+ person.password = "mypassword"
45
+ person.username = "myusername"
46
+ person.email = ""
47
+
48
+ person.frag_valid_on(:username, :password).should be_true
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,16 @@
1
+ require 'fragmented_validation'
2
+
3
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
4
+ :database => File.dirname(__FILE__) + "/fragmented_validation.sqlite3")
5
+
6
+ load File.dirname(__FILE__) + '/support/schema.rb'
7
+ load File.dirname(__FILE__) + '/support/models.rb'
8
+ require 'rspec'
9
+
10
+
11
+ RSpec.configure do |config|
12
+ config.color_enabled = true
13
+ config.formatter = 'documentation'
14
+ end
15
+
16
+
@@ -0,0 +1,10 @@
1
+ class User < ActiveRecord::Base
2
+
3
+ attr_accessor :password_confirmation
4
+
5
+ validates :email, :presence => true
6
+ validates :password, :presence => true
7
+ validates :password, :confirmation => true
8
+ validates :username, :presence => true
9
+
10
+ end
@@ -0,0 +1,10 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :users, :force => true do |t|
5
+ t.string :username
6
+ t.string :password
7
+ t.string :email
8
+ t.timestamps
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fragmented_validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Paul Pacquing
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.6'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.6'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3-ruby
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: activerecord
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.0'
78
+ description: FragmentedValidation allows your model to only validate selected attributes.
79
+ email:
80
+ - dominicpacquing@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - LICENSE
89
+ - README.md
90
+ - Rakefile
91
+ - fragmented_validation.gemspec
92
+ - lib/fragmented_validation.rb
93
+ - lib/fragmented_validation/core.rb
94
+ - lib/fragmented_validation/version.rb
95
+ - spec/fragmented_validation_spec.rb
96
+ - spec/spec_helper.rb
97
+ - spec/support/models.rb
98
+ - spec/support/schema.rb
99
+ homepage: https://github.com/dominicpacquing/fragmented_validation
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.19
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: ''
123
+ test_files:
124
+ - spec/fragmented_validation_spec.rb
125
+ - spec/spec_helper.rb
126
+ - spec/support/models.rb
127
+ - spec/support/schema.rb