validbot 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eaf7a4b43e648942e39aa87b35e98f78e73d0924
4
+ data.tar.gz: 7278d10fc61dbae5061d71781fca66683217adeb
5
+ SHA512:
6
+ metadata.gz: fd7cc84251f6e5c5385b1294f023eb04faafce05af0e0b542b538666b49855f84f67ead9f3241e675b06f37ba4c0c0d58daff162c0f4975a14b796bb8394b7bd
7
+ data.tar.gz: 99fe8edb1f17ff6fb622436d6c2ebb5042e20a428f7d24eb5977f39269ed6dc7dab832edc3a1db56a89e44594cb350933b352d2228b1f9950186909a511f97bb
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .ruby-*
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in validbot.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Brandon Dewitt
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.
@@ -0,0 +1,83 @@
1
+ # Validbot
2
+
3
+ inspired by http://reefpoints.dockyard.com/ruby/2013/05/09/context-validations.html
4
+
5
+ attempting to provide a solution to different validations for controller methods depending
6
+ on the context and also applying those validations to non-active_record objects
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'validbot'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install validbot
21
+
22
+ ## Usage
23
+
24
+ Validations are applied to the object that is passed to the validate method, the validations
25
+ in the block passed are applied when `valid?` is called on the object.
26
+
27
+
28
+ Basic controller example:
29
+
30
+ ```ruby
31
+ class AwesomeController < ApplicationController
32
+
33
+ def create
34
+ @awesome = ::Awesome.new(params)
35
+ validate_create(@aweome)
36
+
37
+ if @awesome.valid?
38
+ render :show
39
+ else
40
+ render :error
41
+ end
42
+ end
43
+
44
+ def update
45
+ @awesome = ::Awesome.find(params[:id])
46
+ validate_update(@aweome)
47
+
48
+ if @awesome.valid?
49
+ render :show
50
+ else
51
+ render :error
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def validate_create(awesome_object)
58
+ Validbot.validate(awesome_object) do
59
+ validates :name, :presence => true # name must be present on create
60
+ end
61
+ end
62
+
63
+ def validate_update(awesome_object)
64
+ Validbot.validate(awesome_object) do
65
+ validate :has_not_been_updated_recently?
66
+
67
+ def has_not_been_updated_recently?
68
+ if self.updated_at && self.updated_at > 15.minutes.ago
69
+ self.errors.add(:updated_at, 'can only update every 15 minutes')
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ ```
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create new Pull Request
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ ::Rake::TestTask.new do |t|
5
+ t.libs.push "lib"
6
+ t.libs.push "spec"
7
+ t.pattern = "spec/**/*_spec.rb"
8
+ t.verbose = true
9
+ end
10
+
11
+ task :spec => :test
12
+ task :default => :spec
@@ -0,0 +1,33 @@
1
+ require "validbot/version"
2
+ require 'active_model'
3
+
4
+ module Validbot
5
+
6
+ def self.validate(object, &block)
7
+ ::Validbot::Validator.new(object, &block)
8
+ end
9
+
10
+ class Validator
11
+ def self.validate(object, &block)
12
+ self.new(object, &block)
13
+ end
14
+
15
+ def initialize(object, &block)
16
+ klass(object).class_eval do
17
+ include ::ActiveModel::Validations
18
+ end
19
+
20
+ klass(object).class_eval(&block)
21
+ self
22
+ end
23
+
24
+ private
25
+
26
+ def klass(object)
27
+ class << object
28
+ self
29
+ end
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,3 @@
1
+ module Validbot
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require(:default, :development, :test)
4
+
5
+ require 'minitest/mock'
6
+ require 'minitest/spec'
7
+ require 'minitest/autorun'
8
+ require 'minitest/pride'
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe Validbot do
4
+ class DerpDerp < Struct.new(:derp, :version, :title)
5
+ include ActiveModel::Model
6
+ end
7
+
8
+ let (:derp) { DerpDerp.new }
9
+
10
+ it 'validates presence' do
11
+ Validbot.validate(derp) do
12
+ validates :derp, :presence => true
13
+ end
14
+
15
+ derp.valid?.must_equal(false)
16
+ derp.errors[:derp].wont_be_empty
17
+ end
18
+
19
+ it 'is additive when block passed twice' do
20
+ Validbot.validate(derp) do
21
+ validates :derp, :presence => true
22
+ end
23
+
24
+ Validbot.validate(derp) do
25
+ validates :title, :presence => true
26
+ end
27
+
28
+ derp.valid?.must_equal(false)
29
+ derp.errors[:derp].wont_be_empty
30
+ derp.errors[:title].wont_be_empty
31
+ end
32
+
33
+ it 'calls custom validation methods defined in block' do
34
+ Validbot.validate(derp) do
35
+ validate :version_must_be_big
36
+
37
+ def version_must_be_big
38
+ if version.nil? || version < 10
39
+ self.errors.add(:version, 'must be big, like ... really big')
40
+ end
41
+ end
42
+ end
43
+
44
+ derp.valid?.must_equal false
45
+ derp.errors[:version].wont_be_empty
46
+ derp.errors[:version].first.must_match /be big/i
47
+
48
+ derp.version = 40
49
+ derp.valid?.must_equal true
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'validbot/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "validbot"
8
+ gem.version = Validbot::VERSION
9
+ gem.authors = ["Brandon Dewitt"]
10
+ gem.email = ["brandonsdewitt+validbot@gmail.com"]
11
+ gem.description = %q{ contextual validations for controllers and wanna learn to do other stuff good too }
12
+ gem.summary = %q{ contextual validations for controllers and wanna learn to do other stuff good too }
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'activemodel'
21
+
22
+ gem.add_development_dependency 'bundler'
23
+ gem.add_development_dependency 'pry'
24
+ gem.add_development_dependency 'rake'
25
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validbot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Dewitt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
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: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
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
+ description: " contextual validations for controllers and wanna learn to do other
70
+ stuff good too "
71
+ email:
72
+ - brandonsdewitt+validbot@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/validbot.rb
83
+ - lib/validbot/version.rb
84
+ - spec/spec_helper.rb
85
+ - spec/validbot_spec.rb
86
+ - validbot.gemspec
87
+ homepage: ''
88
+ licenses: []
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.2.0
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: contextual validations for controllers and wanna learn to do other stuff
110
+ good too
111
+ test_files:
112
+ - spec/spec_helper.rb
113
+ - spec/validbot_spec.rb