active_warnings 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +1 -0
- data/active_warnings.gemspec +23 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/active_warnings.rb +63 -0
- data/lib/active_warnings/validator.rb +8 -0
- data/lib/active_warnings/version.rb +3 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b2e4c511ee9a9519c3b062990f5a0c71d698fed2
|
4
|
+
data.tar.gz: db9c49584e0fdcb03ed0f64a10dd249ad882aabd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5e8dd7bf72dd7d055720b03f3110fc0adc2c1724972078316859baa5cd145fb4c495b9b5a14702a0b1b0e9bc80857b2e64f64ec70c8198fe579d19e38f13aa64
|
7
|
+
data.tar.gz: 9eef7f281248db5d30eae3b25d54a090cc99514ee8a68d232a266fa85d0007f5dbe6245ba9dd4c597d884d847a4d09bc3a4121159462f76d5f97fd29311b5346
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Steve Chung
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all 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,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# ActiveWarnings
|
2
|
+
|
3
|
+
`ActiveModel::Validations` separate for warnings.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'active_warnings'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install active_warnings
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class BasicModel
|
25
|
+
include ActiveWarnings
|
26
|
+
|
27
|
+
attr_accessor :name, :warning_name
|
28
|
+
|
29
|
+
validates :name, presence: true
|
30
|
+
|
31
|
+
warnings do
|
32
|
+
# to use same validators,
|
33
|
+
# calling #valid? or #errors will be #no_errors? and #warnings, respectively (on self or record)
|
34
|
+
validates :warning_name, presence: true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
model = BasicModel.new(name: "a")
|
39
|
+
model.valid? # => true
|
40
|
+
model.safe? == model.no_warnings? # => false
|
41
|
+
model.warnings.keys # => [:warning_name]
|
42
|
+
model.errors.keys # => []
|
43
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_warnings/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_warnings"
|
8
|
+
spec.version = ActiveWarnings::VERSION
|
9
|
+
spec.authors = ["Steve Chung"]
|
10
|
+
spec.email = ["steve.chung7@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Separate ActiveModel::Validations for warnings."
|
13
|
+
spec.homepage = "https://github.com/s12chung/active_warnings"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activemodel", "~> 4.2"
|
22
|
+
|
23
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "active_warnings"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require "active_warnings/version"
|
2
|
+
|
3
|
+
require 'active_model'
|
4
|
+
require 'active_support/all'
|
5
|
+
|
6
|
+
require 'active_warnings/validator'
|
7
|
+
|
8
|
+
module ActiveWarnings
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
include ActiveModel::Validations
|
13
|
+
|
14
|
+
define_callbacks :validate_warning, scope: :name
|
15
|
+
|
16
|
+
def errors
|
17
|
+
@run_warning_validations ? warnings : super
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
# Override and change one line:
|
22
|
+
# https://github.com/rails/rails/blob/64c1264419f766a306eba0418c1030b87489ea67/activemodel/lib/active_model/validations.rb#L406
|
23
|
+
def run_validations!
|
24
|
+
return super unless @run_warning_validations
|
25
|
+
run_callbacks :validate_warning
|
26
|
+
errors.empty?
|
27
|
+
end
|
28
|
+
|
29
|
+
class << self
|
30
|
+
def warnings
|
31
|
+
@within_warnings = true
|
32
|
+
yield
|
33
|
+
ensure
|
34
|
+
@within_warnings = nil
|
35
|
+
end
|
36
|
+
|
37
|
+
# Change first parameter of:
|
38
|
+
# https://github.com/rails/rails/blob/beb07fbfae845d20323a9863c7216c6b63aff9c7/activemodel/lib/active_model/validations.rb#L170
|
39
|
+
def set_callback(name, *filter_list, &block)
|
40
|
+
return super unless name == :validate && @within_warnings
|
41
|
+
super(:validate_warning, *filter_list, &block)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def warnings
|
47
|
+
@warnings ||= ActiveModel::Errors.new(self)
|
48
|
+
end
|
49
|
+
|
50
|
+
def has_warnings?(context=nil)
|
51
|
+
!no_warnings?(context)
|
52
|
+
end
|
53
|
+
alias_method :unsafe?, :has_warnings?
|
54
|
+
|
55
|
+
def no_warnings?(context=nil)
|
56
|
+
@run_warning_validations = true
|
57
|
+
valid?(context)
|
58
|
+
ensure
|
59
|
+
@run_warning_validations = nil
|
60
|
+
end
|
61
|
+
alias_method :safe?, :no_warnings?
|
62
|
+
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_warnings
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Chung
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-20 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: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- steve.chung7@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rspec"
|
36
|
+
- ".travis.yml"
|
37
|
+
- Gemfile
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- active_warnings.gemspec
|
42
|
+
- bin/console
|
43
|
+
- bin/setup
|
44
|
+
- lib/active_warnings.rb
|
45
|
+
- lib/active_warnings/validator.rb
|
46
|
+
- lib/active_warnings/version.rb
|
47
|
+
homepage: https://github.com/s12chung/active_warnings
|
48
|
+
licenses:
|
49
|
+
- MIT
|
50
|
+
metadata: {}
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 2.4.5
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: Separate ActiveModel::Validations for warnings.
|
71
|
+
test_files: []
|