act_a 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -2
- data/README.md +99 -0
- data/act_a.gemspec +2 -1
- data/lib/act_a.rb +15 -4
- data/lib/act_a/version.rb +1 -1
- data/spec/libs/acting_validator_spec.rb +25 -13
- metadata +21 -7
- data/README.rdoc +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73996e05c98121475f85244ae3104ded46a087af
|
4
|
+
data.tar.gz: 77012b731b951bcca35196753c0644d0c7540723
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7a90be24d972ef41d43a0d370a538e969507f2a7f9af8cebcc3a7e559bb9d50dfd8809efb6621c9be0f92b622aea81b795d7b94843ce8d05bf7ae1f8d58d313
|
7
|
+
data.tar.gz: 34615ed05aa2bad96c6bdde333613e4b9c7fcabe0bb317b695757f56f95a7cebb656edecde14e899736391cc8a0cf0a66c6f51551e45205ef465f0897223d8f3
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
act_a (0.0.
|
5
|
-
rails
|
4
|
+
act_a (0.0.2)
|
5
|
+
rails
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
@@ -79,6 +79,7 @@ GEM
|
|
79
79
|
rake (>= 0.8.7)
|
80
80
|
thor (>= 0.18.1, < 2.0)
|
81
81
|
rake (10.4.2)
|
82
|
+
rb-readline (0.5.3)
|
82
83
|
rest-client (1.8.0)
|
83
84
|
http-cookie (>= 1.0.2, < 2.0)
|
84
85
|
mime-types (>= 1.16, < 3.0)
|
@@ -138,6 +139,7 @@ DEPENDENCIES
|
|
138
139
|
coveralls
|
139
140
|
factory_girl_rails
|
140
141
|
rake (~> 10.0)
|
142
|
+
rb-readline
|
141
143
|
rspec
|
142
144
|
rspec-html-matchers
|
143
145
|
rspec-rails
|
data/README.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/mmmpa/act_a.svg)](https://travis-ci.org/mmmpa/act_a)
|
2
|
+
[![Coverage Status](https://coveralls.io/repos/mmmpa/act_a/badge.svg?branch=master)](https://coveralls.io/r/mmmpa/act_a?branch=master)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/mmmpa/act_a/badges/gpa.svg)](https://codeclimate.com/github/mmmpa/act_a)
|
4
|
+
|
5
|
+
|
6
|
+
# ActA
|
7
|
+
|
8
|
+
ActAは`ActiveRecord`というか`ActiveModel`のvalidationを指定したもののみ行い、`valid?`を得るものです。
|
9
|
+
|
10
|
+
```rb
|
11
|
+
class Model < ActiveRecord::Base
|
12
|
+
validates :str, :txt,
|
13
|
+
presence: true
|
14
|
+
|
15
|
+
validate :validate_str
|
16
|
+
|
17
|
+
def validate_str
|
18
|
+
errors.add(:str, :validate_str) if str == '失敗する'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
```rb
|
24
|
+
model = Model.new
|
25
|
+
|
26
|
+
model.assign_attributes(str: '文字列').valid?
|
27
|
+
# false
|
28
|
+
```
|
29
|
+
|
30
|
+
```rb
|
31
|
+
actor = ActA.(Model)
|
32
|
+
|
33
|
+
actor.apply(str: '文字列').valid?
|
34
|
+
# true
|
35
|
+
actor.apply(str: '').valid?
|
36
|
+
# false
|
37
|
+
```
|
38
|
+
|
39
|
+
ただしActAの`valid?`では`validates`で与えられたバリデーションしか行えないので、実際のモデルで行われる`valid?`同等のことをするには`valid_brutally?`を使う。
|
40
|
+
|
41
|
+
```rb
|
42
|
+
actor = ActA.(Model)
|
43
|
+
|
44
|
+
actor.apply(str: '失敗する').valid?
|
45
|
+
# true
|
46
|
+
actor.apply(str: '失敗する').valid_brutally?
|
47
|
+
# false
|
48
|
+
```
|
49
|
+
|
50
|
+
# Installation
|
51
|
+
|
52
|
+
```rb
|
53
|
+
gem 'act_a'
|
54
|
+
```
|
55
|
+
|
56
|
+
```
|
57
|
+
bundle install
|
58
|
+
```
|
59
|
+
|
60
|
+
# Usage
|
61
|
+
|
62
|
+
```rb
|
63
|
+
actor = ActA.(Model)
|
64
|
+
# <ActA::Actor:0x007f5577929388...
|
65
|
+
|
66
|
+
actor.apply(str: '文字列')
|
67
|
+
# <ActA::Validator:0x007f438907a250...
|
68
|
+
|
69
|
+
actor.apply(str: '文字列') == actor.apply(str: '文字列')
|
70
|
+
# false
|
71
|
+
```
|
72
|
+
|
73
|
+
```rb
|
74
|
+
actor.apply(str: '文字列').validate!
|
75
|
+
# not raise exception
|
76
|
+
|
77
|
+
actor.apply(str: '文字列', txt: '').validate!
|
78
|
+
# raise ActiveRecord::RecordInvalid
|
79
|
+
|
80
|
+
actor.apply(str: '失敗する').validate_brutally!
|
81
|
+
# raise ActiveRecord::RecordInvalid
|
82
|
+
```
|
83
|
+
|
84
|
+
```rb
|
85
|
+
actor.apply(str: '').validate.errors
|
86
|
+
# #<ActiveModel::Errors:0x007fad97b15368 @base=#<Model id: nil, str: "", txt: nil, created_at: nil, updated_at: nil>, @messages={:str=>["can't be blank"]}>
|
87
|
+
|
88
|
+
actor.apply(str: '').validate.messages
|
89
|
+
# {:str=>["can't be blank"]}
|
90
|
+
|
91
|
+
actor.apply(str: '').validate.valid?
|
92
|
+
# false
|
93
|
+
|
94
|
+
actor.apply(str: '文字列').validate.messages
|
95
|
+
# {}
|
96
|
+
|
97
|
+
actor.apply(str: '文字列').validate.valid?
|
98
|
+
# true
|
99
|
+
```
|
data/act_a.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
18
|
spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
spec.require_paths = ["lib"]
|
20
|
-
spec.add_dependency "rails"
|
20
|
+
spec.add_dependency "rails"
|
21
21
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.10"
|
23
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
@@ -27,4 +27,5 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_development_dependency "rspec-html-matchers"
|
28
28
|
spec.add_development_dependency "factory_girl_rails"
|
29
29
|
spec.add_development_dependency "coveralls"
|
30
|
+
spec.add_development_dependency "rb-readline"
|
30
31
|
end
|
data/lib/act_a.rb
CHANGED
@@ -5,12 +5,25 @@ module ActA
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def new(klass)
|
8
|
-
|
8
|
+
Actor.new(klass)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
+
class Actor
|
13
|
+
attr_accessor :klass
|
14
|
+
|
15
|
+
def initialize(klass)
|
16
|
+
self.klass = klass
|
17
|
+
end
|
18
|
+
|
19
|
+
def apply(*args)
|
20
|
+
Validator.new(klass).apply(*args)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Validator
|
25
|
+
attr_accessor :record, :keys, :klass
|
12
26
|
|
13
|
-
class Base
|
14
27
|
def initialize(klass)
|
15
28
|
self.klass = klass
|
16
29
|
end
|
@@ -78,8 +91,6 @@ module ActA
|
|
78
91
|
end
|
79
92
|
|
80
93
|
private
|
81
|
-
attr_accessor :record, :keys, :klass
|
82
|
-
|
83
94
|
def validators(attribute_name)
|
84
95
|
klass._validators[attribute_name.to_sym]
|
85
96
|
end
|
data/lib/act_a/version.rb
CHANGED
@@ -3,6 +3,10 @@ require 'rails_helper'
|
|
3
3
|
RSpec.describe ActA do
|
4
4
|
let(:actor) { ActA.(Model) }
|
5
5
|
|
6
|
+
it { expect(actor).to be_a(ActA::Actor) }
|
7
|
+
it { expect(actor.apply(str: '')).to be_a(ActA::Validator) }
|
8
|
+
|
9
|
+
|
6
10
|
describe 'validating with instance.valid?' do
|
7
11
|
context 'when validated' do
|
8
12
|
context 'with "validates"' do
|
@@ -15,6 +19,10 @@ RSpec.describe ActA do
|
|
15
19
|
context 'with other validate' do
|
16
20
|
it { expect(actor.apply(str: '失敗する').valid_brutally?).to be_falsey }
|
17
21
|
end
|
22
|
+
|
23
|
+
context 'with db limitation' do
|
24
|
+
it { expect(actor.apply(not_null: '').valid_brutally?).to be_truthy }
|
25
|
+
end
|
18
26
|
end
|
19
27
|
|
20
28
|
context 'when have error message' do
|
@@ -23,8 +31,8 @@ RSpec.describe ActA do
|
|
23
31
|
end
|
24
32
|
|
25
33
|
context 'when use !' do
|
26
|
-
it { expect{actor.apply(str: '文字列', txt: '').validate_brutally!}.to raise_exception(ActiveRecord::RecordInvalid) }
|
27
|
-
it { expect(actor.apply(str: '文字列', txt: 'aaa').validate_brutally!).to be_a(ActA::
|
34
|
+
it { expect { actor.apply(str: '文字列', txt: '').validate_brutally! }.to raise_exception(ActiveRecord::RecordInvalid) }
|
35
|
+
it { expect(actor.apply(str: '文字列', txt: 'aaa').validate_brutally!).to be_a(ActA::Validator) }
|
28
36
|
end
|
29
37
|
|
30
38
|
context 'with self made validator' do
|
@@ -50,20 +58,24 @@ RSpec.describe ActA do
|
|
50
58
|
it { expect(actor.apply(bol: '文字列').valid?).to be_falsey }
|
51
59
|
end
|
52
60
|
|
53
|
-
context '
|
54
|
-
it { expect(actor.apply(
|
55
|
-
it { expect(actor.apply(int: '文字列').validate.errors.messages[:int]).to include('number only') }
|
61
|
+
context 'with db limitation' do
|
62
|
+
it { expect(actor.apply(not_null: '').valid_brutally?).to be_truthy }
|
56
63
|
end
|
64
|
+
end
|
57
65
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
66
|
+
context 'when have error message' do
|
67
|
+
it { expect(actor.apply(int: '文字列').validate.messages[:int]).to include('number only') }
|
68
|
+
it { expect(actor.apply(int: '文字列').validate.errors.messages[:int]).to include('number only') }
|
69
|
+
end
|
62
70
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
71
|
+
context 'when use !' do
|
72
|
+
it { expect { actor.apply(str: '文字列', txt: '').validate! }.to raise_exception(ActiveRecord::RecordInvalid) }
|
73
|
+
it { expect(actor.apply(str: '文字列', txt: 'aaa').validate!).to be_a(ActA::Validator) }
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'with self made validator' do
|
77
|
+
it { expect(actor.apply(bol: '文字列').valid?).to be_falsey }
|
78
|
+
it { expect(actor.apply(bol: true).valid?).to be_truthy }
|
67
79
|
end
|
68
80
|
end
|
69
81
|
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: act_a
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mmmpa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rb-readline
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
139
153
|
description: Validate values from outside.
|
140
154
|
email:
|
141
155
|
- mmmpa.mmmpa@gmail.com
|
@@ -150,7 +164,7 @@ files:
|
|
150
164
|
- Gemfile
|
151
165
|
- Gemfile.lock
|
152
166
|
- MIT-LICENSE
|
153
|
-
- README.
|
167
|
+
- README.md
|
154
168
|
- Rakefile
|
155
169
|
- act_a.gemspec
|
156
170
|
- lib/act_a.rb
|
data/README.rdoc
DELETED