jaso 0.1.1 → 0.2.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
  SHA256:
3
- metadata.gz: f66e9060f14f954d9a7ba87020413c77410cc482168d33049e7051345fe7e0c8
4
- data.tar.gz: e56d2636e7f8aa0af5afd32f353b0db0e9db521141ff1eb8bd1fc92b85483b0c
3
+ metadata.gz: 35d01ca5f14dca4e5668a1d06169548adb6534bf4d9f6437f51d89b5a35488e7
4
+ data.tar.gz: bdb921465c8970455c939ff00fe626f74dd49ee6a64b0f235aa831f458d94640
5
5
  SHA512:
6
- metadata.gz: b5c239bf048ee5b0c21351bbd6f1f9cf84c501c89bd2aa3d2ab276768858039016309f7ed63af60eb470cd4c9bdd1fe1654533689f70cfa309667ebc9517e42e
7
- data.tar.gz: 17a39ef08034700a1dc4ad7623dfad7e2a2107482d3f46656742a5bf29ab4e3624e63e82bda1a2ff01fbbcee3cb9e75a540ea20bb48f9cfbfc7c4d82ccae113b
6
+ metadata.gz: 7f593f93fa46fa6e9d0d1826970a7c2b263d65c7c9b836b00ba78b727c8bca84d42f444acc59f197b88f685bc093116cb6db89a6549847dc4934ad5c51c8966d
7
+ data.tar.gz: 176f2201ad1bceac240660828ba4e5c9d134cb925d94badb356ac0ba6a3b7b1e0a09066b747500d3329d223006efe526e31f470c731932c4216f99e8a5041d71
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.2.0 - 2023-03-08
6
+
7
+ Features:
8
+ - Built-in Active Model validations through `Jaso::Validator`
9
+
10
+ Fixes:
11
+ - Load gem files after declaring `Jaso` class
12
+
5
13
  ## 0.1.1 - 2023-03-05
6
14
 
7
15
  Fixes:
@@ -7,38 +7,34 @@ module Jaso::Attributable
7
7
  end
8
8
 
9
9
  module ClassMethods
10
- private
11
-
12
- def attributes
13
- @attributes ||= {}
10
+ def __attributes
11
+ @__attributes ||= {}
14
12
  end
15
13
 
16
14
  def attribute(name, **options)
17
- attributes[name] = options
18
- private define_method(name) { attributes[name] }
15
+ __attributes[name] = options
16
+ private define_method(name) { __attributes[name] }
19
17
  end
20
18
  end
21
19
 
22
20
  module PrependMethods
23
21
  def __call
24
- assign_attributes
22
+ __assign_attributes
25
23
  super
26
24
  end
27
25
  end
28
26
 
29
- private
30
-
31
- def attributes
32
- @attributes ||= {}
27
+ def __attributes
28
+ @__attributes ||= {}
33
29
  end
34
30
 
35
- def assign_attributes
36
- self.class.send(:attributes).each do |name, options|
37
- input = @inputs[name]
31
+ def __assign_attributes
32
+ self.class.__attributes.each do |name, options|
33
+ input = @__inputs[name]
38
34
  default = options[:default]
39
35
  transform = options[:transform]
40
36
 
41
- attributes[name] = if @inputs.key?(name)
37
+ __attributes[name] = if @__inputs.key?(name)
42
38
  transform.is_a?(Proc) ? transform.call(input) : input
43
39
  else
44
40
  default.is_a?(Proc) ? default.call : default
data/lib/jaso/callable.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module Jaso::Callable
4
4
  def self.included(base)
5
5
  base.extend(ClassMethods)
6
- base.private_class_method :new
7
6
  end
8
7
 
9
8
  module ClassMethods
@@ -12,12 +11,19 @@ module Jaso::Callable
12
11
  end
13
12
  end
14
13
 
15
- private
14
+ # Must be overwritten by subclass
15
+ def call
16
+ end
17
+
18
+ # Must be overwritten by validator
19
+ def __validate!
20
+ end
16
21
 
17
22
  def __call
23
+ __validate!
18
24
  call
19
25
  raise Jaso::NotFinished
20
- rescue Jaso::Finished => wrapper
21
- wrapper.result
26
+ rescue Jaso::Finished => finished
27
+ finished.result
22
28
  end
23
29
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jaso::Configurable
4
+ class Configuration
5
+ attr_accessor :validator
6
+
7
+ def validator=(validator)
8
+ raise ArgumentError, "#{validator} is not a Jaso::Validator" unless validator_defined?(validator)
9
+ @validator = validator
10
+ end
11
+
12
+ private
13
+
14
+ def validator_defined?(validator)
15
+ validator.instance_of?(Module) && validator.to_s.start_with?("Jaso::Validator::")
16
+ end
17
+ end
18
+
19
+ def configuration
20
+ @configuration ||= Configuration.new
21
+ end
22
+
23
+ def configure
24
+ yield(configuration)
25
+ end
26
+
27
+ def inherited(base)
28
+ base.include(configuration.validator) if configuration.validator
29
+ end
30
+ end
data/lib/jaso/core.rb CHANGED
@@ -1,27 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "attributable"
4
- require_relative "callable"
5
- require_relative "finished"
6
- require_relative "not_finished"
7
- require_relative "result"
8
-
9
3
  module Jaso::Core
4
+ require_relative "attributable"
5
+ require_relative "callable"
6
+ require_relative "configurable"
7
+ require_relative "finished"
8
+ require_relative "not_finished"
9
+ require_relative "result"
10
+
11
+ require_relative "validator/active_model"
12
+
10
13
  def self.included(base)
11
14
  base.include(Jaso::Attributable)
12
15
  base.include(Jaso::Callable)
13
- end
16
+ base.extend(Jaso::Configurable)
14
17
 
15
- def initialize(inputs = {})
16
- @inputs = inputs
18
+ base.private_class_method :new
17
19
  end
18
20
 
19
- # Must be overwritten
20
- def call
21
+ def initialize(inputs = {})
22
+ @__inputs = inputs
21
23
  end
22
24
 
23
- private
24
-
25
25
  def success!(**data)
26
26
  result = Jaso::Result.new(Jaso::Result::SUCCESS, data)
27
27
  raise Jaso::Finished, result
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jaso::Validator
4
+ module ActiveModel
5
+ def self.included(base)
6
+ require "active_model"
7
+ base.include(::ActiveModel::Validations)
8
+ base.define_method(:__validate!) { failure!(errors: errors) if invalid? }
9
+ rescue LoadError
10
+ end
11
+ end
12
+ end
data/lib/jaso/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Jaso
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/jaso.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "jaso/core"
4
-
5
3
  class Jaso
4
+ require_relative "jaso/core"
6
5
  include Core
7
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jaso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vinicius Meneses
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-05 00:00:00.000000000 Z
11
+ date: 2023-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -108,10 +108,12 @@ files:
108
108
  - lib/jaso.rb
109
109
  - lib/jaso/attributable.rb
110
110
  - lib/jaso/callable.rb
111
+ - lib/jaso/configurable.rb
111
112
  - lib/jaso/core.rb
112
113
  - lib/jaso/finished.rb
113
114
  - lib/jaso/not_finished.rb
114
115
  - lib/jaso/result.rb
116
+ - lib/jaso/validator/active_model.rb
115
117
  - lib/jaso/version.rb
116
118
  homepage: https://github.com/viniciusmeneses/jaso
117
119
  licenses:
@@ -137,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
139
  - !ruby/object:Gem::Version
138
140
  version: '0'
139
141
  requirements: []
140
- rubygems_version: 3.2.3
142
+ rubygems_version: 3.0.8
141
143
  signing_key:
142
144
  specification_version: 4
143
145
  summary: Just Another Service Object