roshi 0.6.2 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96f2b09d4f9a2cfc7266be05fee56a50bbad6609
|
4
|
+
data.tar.gz: 673294fba72c81de66be3d7a6f13b7ed4f8e9a21
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd07de35b85f1b1211b6831d4f70e69ad2f06a5ad3790252ba167871bc4a12468127558b73007a6df0820b3d005ea5465b66b4f348fea5df181727f70995c07b
|
7
|
+
data.tar.gz: b5ad9b612f018af303a5d806694078574226c395abad44fa7ff6c0d6fdba3ad4041daa74eb7a9f3c3bfc7df597f4d5f98599d14136be6bc19ece295ab0e7db95
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -31,6 +31,14 @@ class TestModel
|
|
31
31
|
validates :date, date: true
|
32
32
|
validates :hiragana, hiragana: true
|
33
33
|
validates :version_number, version_number: true
|
34
|
+
validates :accept_word, available_word: {accept_words: %w(accept available)}
|
35
|
+
validates :reject_word, available_word: {reject_words: %w(reject unavailable)}
|
36
|
+
validates :large_value, numerical_comparison: { greater_than: :small_value }
|
37
|
+
validates :large_value, numerical_comparison: { greater_than_or_equal_to: :small_value }
|
38
|
+
validates :large_value, numerical_comparison: { equal_to: :small_value }
|
39
|
+
validates :small_value, numerical_comparison: { less_than_or_equal_to: :large_value }
|
40
|
+
validates :small_value, numerical_comparison: { less_than: :large_value }
|
41
|
+
validates :middle_value, numerical_comparison: { between: { min: :small_value, max: :large_value } }
|
34
42
|
end
|
35
43
|
```
|
36
44
|
|
data/lib/roshi.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
require 'active_model'
|
2
2
|
require 'roshi/version'
|
3
|
-
%w(
|
3
|
+
%w(
|
4
|
+
email zip_code phone_number date hiragana
|
5
|
+
version_number available_word numerical_comparison
|
6
|
+
).each do |validator_name|
|
4
7
|
require "roshi/active_model/validations/#{validator_name}_validator"
|
5
8
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
class AvailableWordValidator < EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
accept_words = options[:accept_words] || []
|
6
|
+
reject_words = options[:reject_words] || []
|
7
|
+
|
8
|
+
if !(accept_words.empty? || accept_words.include?(value)) \
|
9
|
+
|| reject_words.include?(value)
|
10
|
+
record.errors.add(attribute, options[:message] || :reject_word)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
class NumericalComparisonValidator < EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
# null check
|
6
|
+
unless value.present?
|
7
|
+
record.errors.add(attribute, :blank)
|
8
|
+
return false
|
9
|
+
end
|
10
|
+
|
11
|
+
# check if value is numerical value
|
12
|
+
unless value.is_a?(Numeric)
|
13
|
+
record.errors.add(attribute, :not_a_number)
|
14
|
+
return false
|
15
|
+
end
|
16
|
+
|
17
|
+
keys = options.keys
|
18
|
+
|
19
|
+
# validate operations(>, >=, ==, <=, <)
|
20
|
+
{
|
21
|
+
greater_than: :>,
|
22
|
+
greater_than_or_equal_to: :>=,
|
23
|
+
equal_to: :==,
|
24
|
+
less_than_or_equal_to: :<=,
|
25
|
+
less_than: :<
|
26
|
+
}.each do |k, v|
|
27
|
+
check_valid(record, attribute, value, k, v) if keys.include? k
|
28
|
+
end
|
29
|
+
|
30
|
+
# validate between
|
31
|
+
if keys.include? :between
|
32
|
+
between_hash = options[:between]
|
33
|
+
min_value_name = between_hash[:min]
|
34
|
+
max_value_name = between_hash[:max]
|
35
|
+
min_value = record.send(min_value_name)
|
36
|
+
max_value = record.send(max_value_name)
|
37
|
+
|
38
|
+
unless max_value.present? && min_value.present?
|
39
|
+
record.errors.add(max_value_name, :blank) unless max_value.present?
|
40
|
+
record.errors.add(min_value_name, :blank) unless min_value.present?
|
41
|
+
return false
|
42
|
+
end
|
43
|
+
unless max_value.is_a?(Numeric) && min_value.is_a?(Numeric)
|
44
|
+
unless max_value.is_a?(Numeric)
|
45
|
+
record.errors.add(attribute, :not_a_number)
|
46
|
+
end
|
47
|
+
unless max_value.is_a?(Numeric)
|
48
|
+
record.errors.add(attribute, :not_a_number)
|
49
|
+
end
|
50
|
+
return false
|
51
|
+
end
|
52
|
+
|
53
|
+
unless (min_value..max_value).cover? value
|
54
|
+
record.errors.add(
|
55
|
+
attribute,
|
56
|
+
:must_be_in_range,
|
57
|
+
min_field: record.class.human_attribute_name(min_value_name),
|
58
|
+
max_field: record.class.human_attribute_name(max_value_name)
|
59
|
+
)
|
60
|
+
return false
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def check_valid(record, attribute, value, type, op)
|
68
|
+
compare_to = options[type]
|
69
|
+
compare_value = record.send(options[type])
|
70
|
+
|
71
|
+
unless compare_value.present?
|
72
|
+
record.errors.add(compare_to, :blank)
|
73
|
+
return false
|
74
|
+
end
|
75
|
+
unless compare_value.is_a?(Numeric)
|
76
|
+
record.errors.add(attribute, :not_a_number)
|
77
|
+
return false
|
78
|
+
end
|
79
|
+
|
80
|
+
unless value.send(op, compare_value)
|
81
|
+
record.errors.add(
|
82
|
+
attribute,
|
83
|
+
"must_be_#{type}".to_sym,
|
84
|
+
min_field: record.class.human_attribute_name(compare_to)
|
85
|
+
)
|
86
|
+
return false
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/lib/roshi/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: roshi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kawahiro311
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-05-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
@@ -91,14 +91,17 @@ extra_rdoc_files: []
|
|
91
91
|
files:
|
92
92
|
- ".gitignore"
|
93
93
|
- ".rspec"
|
94
|
+
- ".rubocop.yml"
|
94
95
|
- ".travis.yml"
|
95
96
|
- Gemfile
|
96
97
|
- README.md
|
97
98
|
- Rakefile
|
98
99
|
- lib/roshi.rb
|
100
|
+
- lib/roshi/active_model/validations/available_word_validator.rb
|
99
101
|
- lib/roshi/active_model/validations/date_validator.rb
|
100
102
|
- lib/roshi/active_model/validations/email_validator.rb
|
101
103
|
- lib/roshi/active_model/validations/hiragana_validator.rb
|
104
|
+
- lib/roshi/active_model/validations/numerical_comparison_validator.rb
|
102
105
|
- lib/roshi/active_model/validations/phone_number_validator.rb
|
103
106
|
- lib/roshi/active_model/validations/version_number_validator.rb
|
104
107
|
- lib/roshi/active_model/validations/zip_code_validator.rb
|