ucasy 0.0.7 → 0.1.0

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
  SHA256:
3
- metadata.gz: aa0068b7c0d66e10140eb038baa83d458071519717ba8993ef737b3e4578202d
4
- data.tar.gz: a1637b64e50c3fcf3c116a29cd65c236a73c0134525b5164260a93f9f6fb7aef
3
+ metadata.gz: 527b41741e56f37168fedca4c4a8f715d0028442c683a0307d663a66674e21d7
4
+ data.tar.gz: 1add4fd86b293082a53b227b637c8d67ec3d43272971700ef489c0cf6f5c1634
5
5
  SHA512:
6
- metadata.gz: 6d6a0e6070b2d8189791f244af93d74b84f1b345b59ee40b23ec6e24265f47210131d4e93a2f30fd937ebb3b060fcf8437accce4ac28dc15279aba3f0ae62880
7
- data.tar.gz: f5316309a283ab3238bd9c7431ba2f25ef0365f108a96391a8217be56d0129551ce565610335dac72e7296e6fbcb496eae92310b7bb3f9f11623d18fdf940a57
6
+ metadata.gz: aa60ed7eb4a8974bff5b00f8edc0fdd4108ba01da21244015bafbc463323c3e5efa20480c6512dadac7029cf2c3daa15205931b0aa5f9a71999cf36d3d03e4be
7
+ data.tar.gz: c59dbe9298d800901cc4a24cfccc444bb032a98addd36209da4d5c1aa654d1096de5d42aeaf41a5e2ad9a0c05a97283e08d428c094563e8c64ee265aafff3376
data/lib/ucasy/base.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative "context"
2
2
  require_relative "failure"
3
+ require_relative "validators/validate"
3
4
  require_relative "validators/required_attributes"
4
5
 
5
6
  module Ucasy
@@ -9,6 +10,12 @@ module Ucasy
9
10
  new(context).perform
10
11
  end
11
12
 
13
+ def validate(validator_class)
14
+ @validator_class = validator_class
15
+ end
16
+
17
+ attr_reader :validator_class
18
+
12
19
  def required_attributes(*attributes)
13
20
  @required_attributes = attributes
14
21
  end
@@ -25,7 +32,9 @@ module Ucasy
25
32
  def perform
26
33
  return if failure?
27
34
 
35
+ validate!
28
36
  validate_required_attributes!
37
+
29
38
  try(:before) if success?
30
39
  call if success?
31
40
  try(:after) if success?
@@ -39,6 +48,20 @@ module Ucasy
39
48
 
40
49
  attr_reader :context
41
50
 
51
+ def validate!
52
+ return if self.class.validator_class.blank?
53
+
54
+ validator = Validators::Validate.call(self.class.validator_class, context.to_h)
55
+
56
+ if validator.valid?
57
+ validator.to_context.each do |key, value|
58
+ context.send(:"#{key}=", value)
59
+ end
60
+ else
61
+ context.fail!(status: :unprocessable_entity, message: validator.message, errors: validator.errors)
62
+ end
63
+ end
64
+
42
65
  def validate_required_attributes!
43
66
  Validators::RequiredAttributes.call(context, self.class._required_attributes, self.class)
44
67
  end
@@ -0,0 +1,34 @@
1
+ module Ucasy::Validators
2
+ class Validate < Ucasy::Callable
3
+ def initialize(klass, attributes)
4
+ @klass = klass
5
+ @attributes = attributes
6
+ end
7
+
8
+ def call
9
+ @validator = @klass.new(@attributes) if @klass.present?
10
+
11
+ self
12
+ end
13
+
14
+ def message
15
+ @validator.try(:message_error)
16
+ end
17
+
18
+ def to_context
19
+ @validator.try(:to_context) || {}
20
+ end
21
+
22
+ def valid?
23
+ @validator&.valid? || false
24
+ end
25
+
26
+ def invalid?
27
+ !valid?
28
+ end
29
+
30
+ def errors
31
+ @validator&.errors || []
32
+ end
33
+ end
34
+ end
data/lib/ucasy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ucasy
2
- VERSION = "0.0.7"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ucasy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lavenda Software
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-06 00:00:00.000000000 Z
11
+ date: 2024-09-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -33,6 +33,7 @@ files:
33
33
  - lib/ucasy/failure.rb
34
34
  - lib/ucasy/flow.rb
35
35
  - lib/ucasy/validators/required_attributes.rb
36
+ - lib/ucasy/validators/validate.rb
36
37
  - lib/ucasy/version.rb
37
38
  - sig/ucasy/spec.rbs
38
39
  - ucasy.gemspec