rite 0.0.1.pre.alpha → 0.2.0.pre.alpha
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/.gitignore +1 -0
- data/bin/console +4 -6
- data/lib/rite.rb +12 -0
- data/lib/rite/validator.rb +19 -26
- data/lib/rite/validator_dsl.rb +53 -0
- data/lib/rite/version.rb +1 -1
- metadata +4 -3
- data/lib/rite/rite.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 915a8cda0283a721c2a4aea8ff2c5a666b8ef2942b275b79e975b82e6a218c1f
|
4
|
+
data.tar.gz: cac84c74181f00037031ba561a4b98dbcd14cacc3ac19138fc073f958ad4093c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 483a4aad8832d698956bbafbd71a4965dc46aa4f5fb55ae6e0bb46f2c8d6979dafb41d1ae9d50a2495eb5b3da4c674b0965408967620d26276408e6c46dac213
|
7
|
+
data.tar.gz: edc4ea8e7aa1fb94e60cd99eea654ed2bc6f0b9fb9c58932316fc5e26370473e10fe4c2d46913b5b6f29b4a920e14194fdb7add29273921eac74b40f2c630a1b
|
data/.gitignore
CHANGED
data/bin/console
CHANGED
@@ -1,14 +1,12 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
3
5
|
require 'bundler/setup'
|
4
6
|
require 'rite'
|
5
7
|
|
6
8
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
9
|
# with your gem easier. You can also use a different console, if you like.
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require 'irb'
|
14
|
-
IRB.start(__FILE__)
|
11
|
+
require 'pry'
|
12
|
+
Pry.start
|
data/lib/rite.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rite/version'
|
4
|
+
require 'rite/validator'
|
5
|
+
require 'rite/validator_dsl'
|
6
|
+
|
7
|
+
module Rite
|
8
|
+
class Error < StandardError; end
|
9
|
+
class ValidationError < Error; end
|
10
|
+
class DSLError < Error; end
|
11
|
+
# Your code goes here...
|
12
|
+
end
|
data/lib/rite/validator.rb
CHANGED
@@ -4,41 +4,34 @@
|
|
4
4
|
module Rite
|
5
5
|
# Validator handles logic for validating a value
|
6
6
|
class Validator
|
7
|
-
def self.validate(*)
|
7
|
+
def self.validate(_value, *_args)
|
8
8
|
raise NotImplementedError, 'validate is not implemented yet'
|
9
9
|
end
|
10
10
|
|
11
|
-
def self.
|
12
|
-
|
11
|
+
def self.validate!(value, *args)
|
12
|
+
raise Rite::ValidationError, failure_message(value, *args) unless validate(value, args)
|
13
|
+
true
|
13
14
|
end
|
14
15
|
|
15
|
-
def
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
# @private
|
21
|
-
class ValidatorDefinition
|
22
|
-
attr_reader :klass
|
23
|
-
|
24
|
-
def initialize
|
25
|
-
@klass = Class.new(Validator)
|
16
|
+
def handle_error(error, _value, *_args)
|
17
|
+
raise error
|
26
18
|
end
|
27
19
|
|
28
|
-
def
|
29
|
-
|
30
|
-
|
20
|
+
def self.valid?(value, *args)
|
21
|
+
validate(value, *args)
|
22
|
+
rescue StandardError => e
|
23
|
+
handle_error(e)
|
31
24
|
end
|
32
25
|
|
33
|
-
def failure_message(
|
34
|
-
|
35
|
-
@klass.define_singleton_method(:failure_message, &block)
|
26
|
+
def self.failure_message(value, *_args)
|
27
|
+
%("#{value}" failed validation)
|
36
28
|
end
|
37
29
|
end
|
38
|
-
|
39
|
-
def self.define_validator(&block)
|
40
|
-
definition = ValidatorDefinition.new
|
41
|
-
definition.instance_eval(&block)
|
42
|
-
definition.klass
|
43
|
-
end
|
44
30
|
end
|
31
|
+
|
32
|
+
# [
|
33
|
+
# {
|
34
|
+
# validator: ClassValidator,
|
35
|
+
# arguments: [String],
|
36
|
+
# },
|
37
|
+
# ]
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rite/validator'
|
4
|
+
|
5
|
+
module Rite
|
6
|
+
VALIDATOR_METHOD_ARITY = 1
|
7
|
+
|
8
|
+
class ValidatorDSL
|
9
|
+
def self.call(&block)
|
10
|
+
builder = new
|
11
|
+
builder.instance_eval(&block)
|
12
|
+
builder.klass
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :klass
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@klass = Class.new(Rite::Validator)
|
19
|
+
end
|
20
|
+
|
21
|
+
def validate(&block)
|
22
|
+
define_validator_method(:validate, &block)
|
23
|
+
end
|
24
|
+
|
25
|
+
def message(message = nil, &block)
|
26
|
+
if block_given?
|
27
|
+
define_validator_method(:failure_message, &block)
|
28
|
+
else
|
29
|
+
define_validator_method(:failure_message) do |value, _args = []|
|
30
|
+
message.gsub(/%value/, value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def define_validator_method(name, &block)
|
36
|
+
if block.arity.abs < VALIDATOR_METHOD_ARITY
|
37
|
+
raise Rite::DSLError, "validate method requires #{VALIDATOR_METHOD_ARITY} argument"
|
38
|
+
end
|
39
|
+
|
40
|
+
@klass.class_eval do
|
41
|
+
define_singleton_method(name) do |value, args = []|
|
42
|
+
block.call(value, *args)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.define_validator(&block)
|
51
|
+
ValidatorDSL.call(&block)
|
52
|
+
end
|
53
|
+
end
|
data/lib/rite/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.2.0.pre.alpha
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Buck
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-04-
|
11
|
+
date: 2020-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -70,8 +70,9 @@ files:
|
|
70
70
|
- Rakefile
|
71
71
|
- bin/console
|
72
72
|
- bin/setup
|
73
|
-
- lib/rite
|
73
|
+
- lib/rite.rb
|
74
74
|
- lib/rite/validator.rb
|
75
|
+
- lib/rite/validator_dsl.rb
|
75
76
|
- lib/rite/version.rb
|
76
77
|
- rite.gemspec
|
77
78
|
homepage:
|