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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 686317201ceafae45f7090b8323d256cff79d9c8
4
- data.tar.gz: cb2e69ebeca2360b62bd5c103293ceb36b822112
3
+ metadata.gz: 73996e05c98121475f85244ae3104ded46a087af
4
+ data.tar.gz: 77012b731b951bcca35196753c0644d0c7540723
5
5
  SHA512:
6
- metadata.gz: e5baeb03193d28db1119e961d83ed6023b6bc30be0bd15876430afb8987df981c1e4fa4a21247ddc4dc6535e900d816ef2e762f18828e9c595c7cc5c5ca24855
7
- data.tar.gz: 4423e6c4b291504cf1400a4c8a3e4a23fa2847d9a21f80b24677d7e01752bba9226900e2213a66c2d4b6d39709704054c3d198bbb6f2a8109abe3d2f27fa8bf0
6
+ metadata.gz: a7a90be24d972ef41d43a0d370a538e969507f2a7f9af8cebcc3a7e559bb9d50dfd8809efb6621c9be0f92b622aea81b795d7b94843ce8d05bf7ae1f8d58d313
7
+ data.tar.gz: 34615ed05aa2bad96c6bdde333613e4b9c7fcabe0bb317b695757f56f95a7cebb656edecde14e899736391cc8a0cf0a66c6f51551e45205ef465f0897223d8f3
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- act_a (0.0.1)
5
- rails (~> 4.0.0)
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
@@ -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
+ ```
@@ -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", "~> 4.0.0"
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
@@ -5,12 +5,25 @@ module ActA
5
5
  end
6
6
 
7
7
  def new(klass)
8
- Base.new(klass)
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
@@ -1,3 +1,3 @@
1
1
  module ActA
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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::Base) }
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 'when have error message' do
54
- it { expect(actor.apply(int: '文字列').validate.messages[:int]).to include('number only') }
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
- context 'when use !' do
59
- it { expect{actor.apply(str: '文字列', txt: '').validate!}.to raise_exception(ActiveRecord::RecordInvalid) }
60
- it { expect(actor.apply(str: '文字列', txt: 'aaa').validate!).to be_a(ActA::Base) }
61
- end
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
- context 'with self made validator' do
64
- it { expect(actor.apply(bol: '文字列').valid?).to be_falsey }
65
- it { expect(actor.apply(bol: true).valid?).to be_truthy }
66
- end
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.1
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-07 00:00:00.000000000 Z
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: 4.0.0
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: 4.0.0
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.rdoc
167
+ - README.md
154
168
  - Rakefile
155
169
  - act_a.gemspec
156
170
  - lib/act_a.rb
@@ -1,3 +0,0 @@
1
- = ActA
2
-
3
- This project rocks and uses MIT-LICENSE.