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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc406c20e075564b4e2b3bf39fca70e16391f9d7c84d98fc42b86a5d3f9fc97c
4
- data.tar.gz: 227b1be8211074d1122f19728588246e57f5bbb3d3bfcf42ac1ea68f283a8da4
3
+ metadata.gz: 915a8cda0283a721c2a4aea8ff2c5a666b8ef2942b275b79e975b82e6a218c1f
4
+ data.tar.gz: cac84c74181f00037031ba561a4b98dbcd14cacc3ac19138fc073f958ad4093c
5
5
  SHA512:
6
- metadata.gz: 93805e102e1098f202a19a47899ae53f82c94e20d53d396a7f00d49134a513eaa747a16c2343179300de114ef682eb927a2a67ca6f8aca4570eb1892766e07bf
7
- data.tar.gz: 790125fc81d46272f59c5f3facf91d313e60fe9bd24937ed5da079b5e3940827501034c3f18292fd0ef44b0f078ef2cc66e8725e0ef3737d336bcca0dd9ae446
6
+ metadata.gz: 483a4aad8832d698956bbafbd71a4965dc46aa4f5fb55ae6e0bb46f2c8d6979dafb41d1ae9d50a2495eb5b3da4c674b0965408967620d26276408e6c46dac213
7
+ data.tar.gz: edc4ea8e7aa1fb94e60cd99eea654ed2bc6f0b9fb9c58932316fc5e26370473e10fe4c2d46913b5b6f29b4a920e14194fdb7add29273921eac74b40f2c630a1b
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
12
  Gemfile.lock
13
+ *.gem
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
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
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
@@ -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.valid?(value)
12
- !!validate(value) # rubocop:disable Style/DoubleNegation
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 self.failure_message(*)
16
- 'value failed validation'
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 on_validate(&block)
29
- raise ArgumentError, 'block to #on_validate should accept one argument' if block.arity.abs != 1
30
- @klass.define_singleton_method(:validate, &block)
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(&block)
34
- raise ArgumentError, 'block to #on_validate should accept one argument' if block.arity.abs != 1
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
@@ -2,5 +2,5 @@
2
2
 
3
3
  # @private
4
4
  module Rite
5
- VERSION = '0.0.1-alpha'
5
+ VERSION = '0.2.0-alpha'
6
6
  end
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.1.pre.alpha
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-17 00:00:00.000000000 Z
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/rite.rb
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:
data/lib/rite/rite.rb DELETED
@@ -1,6 +0,0 @@
1
- require "rite/version"
2
-
3
- module Rite
4
- class Error < StandardError; end
5
- # Your code goes here...
6
- end