rulix 0.2.0 → 0.3.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 +4 -4
- data/lib/rulix/active_record_validator.rb +35 -0
- data/lib/rulix/mutators/replace.rb +23 -0
- data/lib/rulix/mutators/squeeze_spaces.rb +17 -0
- data/lib/rulix/mutators/strip.rb +29 -0
- data/lib/rulix/mutators/truncate.rb +23 -0
- data/lib/rulix/registry.rb +1 -1
- data/lib/rulix/validators/alpha_numeric_validator.rb +6 -17
- data/lib/rulix/validators/alpha_validator.rb +6 -17
- data/lib/rulix/validators/email_validator.rb +6 -17
- data/lib/rulix/validators/format_validator.rb +1 -1
- data/lib/rulix/validators/length_validator.rb +2 -0
- data/lib/rulix/validators/not_one_of_validator.rb +1 -1
- data/lib/rulix/validators/number_validator.rb +3 -1
- data/lib/rulix/validators/one_of_validator.rb +1 -1
- data/lib/rulix/validators/required_validator.rb +1 -1
- data/lib/rulix/version.rb +1 -1
- data/lib/rulix.rb +2 -0
- metadata +7 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d3a105f632d0c729e93bc8ed9cb1389f8a3386f5
|
|
4
|
+
data.tar.gz: 1b83a610f1aed3fe46d65ff75a459e155fb3827b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b9e44f9296c40ccd8cfbcc5e822f30ba90b378a8e603791f3e786caedc3e0e2299c45b172913374fcffde163f7723cbc4b7694194279304d04d9d9a583c3e5b9
|
|
7
|
+
data.tar.gz: 3b7f4e2de34f8a6a8008f12cd9ede722a79f4f08af9b516237ca373eab509cbd25007ff121f6f59191bf95acfb7df4f0a8b758ca669ce6b944befdd0485f9ffb
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module Rulix
|
|
2
|
+
class ActiveRecordValidator
|
|
3
|
+
attr_accessor :ruleset
|
|
4
|
+
|
|
5
|
+
def initialize *args
|
|
6
|
+
ruleset = args.first[:ruleset]
|
|
7
|
+
|
|
8
|
+
self.ruleset = ruleset
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def validate record
|
|
12
|
+
unless Rulix::Validator.valid? record, ruleset
|
|
13
|
+
errors = Rulix::Validator.errors record, ruleset
|
|
14
|
+
|
|
15
|
+
reduce_errors_into record, errors
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def reduce_errors_into record, error_hash
|
|
20
|
+
error_hash.reduce(record) do |subject, error|
|
|
21
|
+
key, val = error
|
|
22
|
+
|
|
23
|
+
if val.is_a? Hash
|
|
24
|
+
reduce_errors_into record.send(key), val
|
|
25
|
+
else
|
|
26
|
+
val.flatten.map do |err|
|
|
27
|
+
subject.errors.add key, err
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
subject
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Rulix
|
|
2
|
+
module Mutators
|
|
3
|
+
class Replace
|
|
4
|
+
attr_accessor :args
|
|
5
|
+
|
|
6
|
+
def initialize args
|
|
7
|
+
self.args = args
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def to_proc
|
|
11
|
+
method(:call)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def call string
|
|
15
|
+
raise ArgumentError, "argument is not a string" unless string.is_a? String
|
|
16
|
+
|
|
17
|
+
string.gsub *args
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
Rulix::Mutator.register :replace, Rulix::Mutators::Replace
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module Rulix
|
|
2
|
+
module Mutators
|
|
3
|
+
class SqueezeSpaces
|
|
4
|
+
def self.to_proc
|
|
5
|
+
new.method(:call)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def call string
|
|
9
|
+
raise ArgumentError, "argument is not a string" unless string.is_a? String
|
|
10
|
+
|
|
11
|
+
string.squeeze ' '
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
Rulix::Mutator.register :squeeze_spaces, Rulix::Mutators::SqueezeSpaces
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module Rulix
|
|
2
|
+
module Mutators
|
|
3
|
+
class Strip
|
|
4
|
+
attr_accessor :pattern
|
|
5
|
+
|
|
6
|
+
def initialize pattern = nil
|
|
7
|
+
self.pattern = pattern
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def to_proc
|
|
11
|
+
method(:call)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def call string
|
|
15
|
+
raise ArgumentError, "argument is not a string" unless string.is_a? String
|
|
16
|
+
|
|
17
|
+
if pattern
|
|
18
|
+
string.gsub pattern, ''
|
|
19
|
+
else
|
|
20
|
+
# Just in case someone actually meant the string method :strip
|
|
21
|
+
# If they didn't provide any other arguments, we can assume this
|
|
22
|
+
string.strip
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
Rulix::Mutator.register :strip, Rulix::Mutators::Strip
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Rulix
|
|
2
|
+
module Mutators
|
|
3
|
+
class Truncate
|
|
4
|
+
attr_accessor :length
|
|
5
|
+
|
|
6
|
+
def initialize length
|
|
7
|
+
self.length = length
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def to_proc
|
|
11
|
+
method(:call)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def call string
|
|
15
|
+
raise ArgumentError, "argument is not a string" unless string.is_a? String
|
|
16
|
+
|
|
17
|
+
string.slice(0, length)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
Rulix::Mutator.register :truncate, Rulix::Mutators::Truncate
|
data/lib/rulix/registry.rb
CHANGED
|
@@ -8,7 +8,7 @@ module Rulix
|
|
|
8
8
|
return register_block symbol, &block if block_given?
|
|
9
9
|
|
|
10
10
|
if !procable.respond_to?(:to_proc)
|
|
11
|
-
unless (procable.respond_to?(:new) && procable.
|
|
11
|
+
unless (procable.respond_to?(:new) && procable.instance_methods.include?(:to_proc))
|
|
12
12
|
raise ArgumentError, "You attempted to register :#{symbol}, but the argument you passed can't be coerced into a proc!"
|
|
13
13
|
end
|
|
14
14
|
end
|
|
@@ -1,19 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
module Validators
|
|
3
|
-
class AlphaNumericValidator
|
|
4
|
-
def self.to_proc
|
|
5
|
-
new.method(:call)
|
|
6
|
-
end
|
|
1
|
+
require_relative './format_validator'
|
|
7
2
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
validator = Rulix::Validators::FormatValidator.new(
|
|
4
|
+
pattern: /^[a-zA-Z0-9\s?]*$/,
|
|
5
|
+
message: "contains non-alpha-numeric characters"
|
|
6
|
+
)
|
|
11
7
|
|
|
12
|
-
|
|
13
|
-
"contains non-alpha-numeric characters"
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
Rulix::Validator.register :alphanum, Rulix::Validators::AlphaNumericValidator
|
|
8
|
+
Rulix::Validator.register :alphanum, validator
|
|
@@ -1,19 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
module Validators
|
|
3
|
-
class AlphaValidator
|
|
4
|
-
def self.to_proc
|
|
5
|
-
new.method(:call)
|
|
6
|
-
end
|
|
1
|
+
require_relative './format_validator'
|
|
7
2
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
validator = Rulix::Validators::FormatValidator.new(
|
|
4
|
+
pattern: /^[a-zA-Z\s?]*$/,
|
|
5
|
+
message: "contains non-alpha characters"
|
|
6
|
+
)
|
|
11
7
|
|
|
12
|
-
|
|
13
|
-
"contains non-alpha characters"
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
Rulix::Validator.register :alpha, Rulix::Validators::AlphaValidator
|
|
8
|
+
Rulix::Validator.register :alpha, validator
|
|
@@ -1,19 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
module Validators
|
|
3
|
-
class EmailValidator
|
|
4
|
-
def self.to_proc
|
|
5
|
-
new.method(:call)
|
|
6
|
-
end
|
|
1
|
+
require_relative './format_validator'
|
|
7
2
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
validator = Rulix::Validators::FormatValidator.new(
|
|
4
|
+
pattern: /.*@.*/,
|
|
5
|
+
message: 'is not an email address'
|
|
6
|
+
)
|
|
11
7
|
|
|
12
|
-
|
|
13
|
-
"is not an email address"
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
Rulix::Validator.register :email, Rulix::Validators::EmailValidator
|
|
8
|
+
Rulix::Validator.register :email, validator
|
|
@@ -6,6 +6,8 @@ module Rulix
|
|
|
6
6
|
end
|
|
7
7
|
|
|
8
8
|
def call value
|
|
9
|
+
return [false, error_message] unless value
|
|
10
|
+
|
|
9
11
|
case value
|
|
10
12
|
when Integer, Fixnum
|
|
11
13
|
true
|
|
@@ -16,7 +18,7 @@ module Rulix
|
|
|
16
18
|
end
|
|
17
19
|
end
|
|
18
20
|
|
|
19
|
-
def error_message value
|
|
21
|
+
def error_message value = nil
|
|
20
22
|
"is not a number"
|
|
21
23
|
end
|
|
22
24
|
end
|
data/lib/rulix/version.rb
CHANGED
data/lib/rulix.rb
CHANGED
|
@@ -2,8 +2,10 @@ require_relative './rulix/version'
|
|
|
2
2
|
require_relative './rulix/registry'
|
|
3
3
|
require_relative './rulix/mutator'
|
|
4
4
|
require_relative './rulix/validator'
|
|
5
|
+
require_relative './rulix/active_record_validator'
|
|
5
6
|
require_relative './rulix/core_ext/hash'
|
|
6
7
|
|
|
8
|
+
Dir[File.dirname(__FILE__) + '/rulix/mutators/*.rb'].each { |f| require f }
|
|
7
9
|
Dir[File.dirname(__FILE__) + '/rulix/validators/*.rb'].each { |f| require f }
|
|
8
10
|
|
|
9
11
|
module Rulix; end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rulix
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mitch Monsen
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-06-
|
|
11
|
+
date: 2016-06-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -68,8 +68,13 @@ files:
|
|
|
68
68
|
- bin/console
|
|
69
69
|
- bin/setup
|
|
70
70
|
- lib/rulix.rb
|
|
71
|
+
- lib/rulix/active_record_validator.rb
|
|
71
72
|
- lib/rulix/core_ext/hash.rb
|
|
72
73
|
- lib/rulix/mutator.rb
|
|
74
|
+
- lib/rulix/mutators/replace.rb
|
|
75
|
+
- lib/rulix/mutators/squeeze_spaces.rb
|
|
76
|
+
- lib/rulix/mutators/strip.rb
|
|
77
|
+
- lib/rulix/mutators/truncate.rb
|
|
73
78
|
- lib/rulix/registry.rb
|
|
74
79
|
- lib/rulix/validator.rb
|
|
75
80
|
- lib/rulix/validators/alpha_numeric_validator.rb
|