kangaru 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/Gemfile.lock +1 -1
- data/lib/kangaru/concern.rb +31 -0
- data/lib/kangaru/concerns/attributable.rb +33 -0
- data/lib/kangaru/concerns/configurable.rb +13 -15
- data/lib/kangaru/concerns/validatable.rb +23 -33
- data/lib/kangaru/configurator.rb +2 -2
- data/lib/kangaru/database.rb +1 -1
- data/lib/kangaru/model.rb +1 -1
- data/lib/kangaru/patches/inflections.rb +4 -2
- data/lib/kangaru/request.rb +2 -2
- data/lib/kangaru/validation/attribute_validator.rb +8 -12
- data/lib/kangaru/validation/error.rb +3 -11
- data/lib/kangaru/validation/model_validator.rb +27 -0
- data/lib/kangaru/validator.rb +20 -0
- data/lib/kangaru/validators/required_validator.rb +3 -1
- data/lib/kangaru/validators.rb +29 -0
- data/lib/kangaru/version.rb +1 -1
- data/lib/kangaru.rb +2 -1
- data/sig/kangaru/concern.rbs +13 -0
- data/sig/kangaru/concerns/attributable.rbs +25 -0
- data/sig/kangaru/concerns/configurable.rbs +8 -10
- data/sig/kangaru/concerns/validatable.rbs +14 -16
- data/sig/kangaru/configurator.rbs +4 -4
- data/sig/kangaru/database.rbs +2 -2
- data/sig/kangaru/model.rbs +2 -2
- data/sig/kangaru/patches/inflections.rbs +1 -1
- data/sig/kangaru/request.rbs +1 -1
- data/sig/kangaru/validation/attribute_validator.rbs +5 -7
- data/sig/kangaru/validation/error.rbs +2 -6
- data/sig/kangaru/validation/model_validator.rbs +19 -0
- data/sig/kangaru/validator.rbs +14 -0
- data/sig/kangaru/validators/required_validator.rbs +2 -0
- data/sig/kangaru/validators.rbs +11 -0
- metadata +12 -8
- data/lib/kangaru/concerns/attributes_concern.rb +0 -35
- data/lib/kangaru/concerns/concern.rb +0 -33
- data/lib/kangaru/validators/validator.rb +0 -22
- data/sig/kangaru/concerns/attributes_concern.rbs +0 -27
- data/sig/kangaru/concerns/concern.rbs +0 -15
- data/sig/kangaru/validators/validator.rbs +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9007bb0fb5d483fe6fb13697b94ec895958b93c5db157e82e4e7178e04278137
|
4
|
+
data.tar.gz: ff23e3595c129e04ae6391269570060929ecc73409d3e024eef14374c7d5957e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3eb823da88e761346f49eff461333b79fe63768eb5093be6b6842e911eb9a4feab588755c7bceed9fc16d74118cc25cadf715b7f7dea829b47441c48843b86a
|
7
|
+
data.tar.gz: b1e203b218b378f571568e2730cd199ff8bc785929ec7eb1d8f42de3b34ce1f486fff4323607d95593bf7e2ae000db0b26a0eb0d6cd8d198464e122073b581b9
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Kangaru
|
2
|
+
module Concern
|
3
|
+
def append_features(base)
|
4
|
+
super
|
5
|
+
evaluate_concern_blocks!(base)
|
6
|
+
end
|
7
|
+
|
8
|
+
def class_methods(&)
|
9
|
+
if const_defined?(:ClassMethods)
|
10
|
+
const_get(:ClassMethods)
|
11
|
+
else
|
12
|
+
const_set(:ClassMethods, Module.new)
|
13
|
+
end.module_eval(&)
|
14
|
+
end
|
15
|
+
|
16
|
+
def included(base = nil, &block)
|
17
|
+
super base if base
|
18
|
+
return if block.nil?
|
19
|
+
|
20
|
+
@included = block
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def evaluate_concern_blocks!(base)
|
26
|
+
base.extend(const_get(:ClassMethods)) if const_defined?(:ClassMethods)
|
27
|
+
|
28
|
+
base.class_eval(&@included) if instance_variable_defined?(:@included)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Kangaru
|
2
|
+
module Attributable
|
3
|
+
extend Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
def attributes
|
7
|
+
instance_methods.grep(/\w=$/).map do |attribute|
|
8
|
+
attribute.to_s.delete_suffix("=").to_sym
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def set_default(**attributes)
|
13
|
+
defaults.merge!(**attributes)
|
14
|
+
end
|
15
|
+
|
16
|
+
def defaults
|
17
|
+
@defaults ||= {}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(**attributes)
|
22
|
+
attributes = self.class.defaults.merge(**attributes)
|
23
|
+
|
24
|
+
merge!(**attributes)
|
25
|
+
end
|
26
|
+
|
27
|
+
def merge!(**attributes)
|
28
|
+
attributes.slice(*self.class.attributes).each do |attr, value|
|
29
|
+
instance_variable_set(:"@#{attr}", value)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,23 +1,21 @@
|
|
1
1
|
module Kangaru
|
2
|
-
module
|
3
|
-
|
4
|
-
extend Concern
|
2
|
+
module Configurable
|
3
|
+
extend Concern
|
5
4
|
|
6
|
-
|
5
|
+
using Patches::Inflections
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
7
|
+
class_methods do
|
8
|
+
def configurator_key
|
9
|
+
(name || raise("class name not set"))
|
10
|
+
.gsub(/^.*::/, "")
|
11
|
+
.to_snakecase
|
12
|
+
.to_sym
|
15
13
|
end
|
14
|
+
end
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
16
|
+
def config
|
17
|
+
Kangaru.application!.config[self.class.configurator_key] ||
|
18
|
+
raise("inferred configurator not set by application")
|
21
19
|
end
|
22
20
|
end
|
23
21
|
end
|
@@ -1,46 +1,36 @@
|
|
1
1
|
module Kangaru
|
2
|
-
module
|
3
|
-
|
4
|
-
extend Concern
|
5
|
-
|
6
|
-
class_methods do
|
7
|
-
def validation_rules
|
8
|
-
@validation_rules ||= {}
|
9
|
-
end
|
10
|
-
|
11
|
-
def validates(attribute, **validations)
|
12
|
-
validation_rules[attribute] ||= {}
|
13
|
-
validation_rules[attribute].merge!(**validations)
|
14
|
-
end
|
15
|
-
end
|
2
|
+
module Validatable
|
3
|
+
extend Concern
|
16
4
|
|
17
|
-
|
18
|
-
|
5
|
+
class_methods do
|
6
|
+
def validation_rules
|
7
|
+
@validation_rules ||= {}
|
19
8
|
end
|
20
9
|
|
21
|
-
def
|
22
|
-
|
23
|
-
|
10
|
+
def validates(attribute, **validations)
|
11
|
+
validation_rules[attribute] ||= {}
|
12
|
+
validation_rules[attribute].merge!(**validations)
|
13
|
+
end
|
14
|
+
end
|
24
15
|
|
25
|
-
|
26
|
-
|
16
|
+
def errors
|
17
|
+
@errors ||= []
|
18
|
+
end
|
27
19
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
20
|
+
def validate
|
21
|
+
model_validator.validate!(**self.class.validation_rules)
|
22
|
+
end
|
32
23
|
|
33
|
-
|
34
|
-
|
24
|
+
def valid?
|
25
|
+
validate
|
35
26
|
|
36
|
-
|
37
|
-
|
27
|
+
errors.empty?
|
28
|
+
end
|
38
29
|
|
39
|
-
|
30
|
+
private
|
40
31
|
|
41
|
-
|
42
|
-
|
43
|
-
end
|
32
|
+
def model_validator
|
33
|
+
@model_validator ||= Validation::ModelValidator.new(model: self)
|
44
34
|
end
|
45
35
|
end
|
46
36
|
end
|
data/lib/kangaru/configurator.rb
CHANGED
data/lib/kangaru/database.rb
CHANGED
data/lib/kangaru/model.rb
CHANGED
@@ -2,8 +2,10 @@ module Kangaru
|
|
2
2
|
module Patches
|
3
3
|
module Inflections
|
4
4
|
refine String do
|
5
|
-
def to_class_name
|
6
|
-
Inflectors::ClassInflector.inflect(self)
|
5
|
+
def to_class_name(suffix: nil)
|
6
|
+
class_name = Inflectors::ClassInflector.inflect(self)
|
7
|
+
|
8
|
+
[class_name, suffix.to_s.capitalize].compact.join
|
7
9
|
end
|
8
10
|
|
9
11
|
def to_constant_name
|
data/lib/kangaru/request.rb
CHANGED
@@ -2,7 +2,7 @@ module Kangaru
|
|
2
2
|
class Request
|
3
3
|
using Patches::Inflections
|
4
4
|
|
5
|
-
include
|
5
|
+
include Configurable
|
6
6
|
|
7
7
|
DEFAULT_CONTROLLER = "DefaultController".freeze
|
8
8
|
|
@@ -18,7 +18,7 @@ module Kangaru
|
|
18
18
|
def controller
|
19
19
|
return default_controller if path_parser.controller.nil?
|
20
20
|
|
21
|
-
path_parser.controller&.to_class_name
|
21
|
+
path_parser.controller&.to_class_name(suffix: Controller::SUFFIX) || raise
|
22
22
|
end
|
23
23
|
|
24
24
|
def action
|
@@ -10,22 +10,18 @@ module Kangaru
|
|
10
10
|
@attribute = attribute
|
11
11
|
end
|
12
12
|
|
13
|
-
def validate(
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
private
|
13
|
+
def validate!(**validations)
|
14
|
+
validations.each do |validator, params|
|
15
|
+
params = {} if params == true
|
18
16
|
|
19
|
-
|
20
|
-
|
17
|
+
load_validator(validator:, **params).validate
|
18
|
+
end
|
21
19
|
end
|
22
20
|
|
23
|
-
|
24
|
-
name = validator_name(validator)
|
25
|
-
|
26
|
-
raise "#{name} is not defined" unless Validators.const_defined?(name)
|
21
|
+
private
|
27
22
|
|
28
|
-
|
23
|
+
def load_validator(validator:, **params)
|
24
|
+
Validators.get(validator).new(model:, attribute:, **params)
|
29
25
|
end
|
30
26
|
end
|
31
27
|
end
|
@@ -3,15 +3,11 @@ module Kangaru
|
|
3
3
|
class Error
|
4
4
|
using Patches::Inflections
|
5
5
|
|
6
|
-
|
7
|
-
blank: "can't be blank"
|
8
|
-
}.freeze
|
6
|
+
attr_reader :attribute, :message
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
def initialize(attribute:, type:)
|
8
|
+
def initialize(attribute:, message:)
|
13
9
|
@attribute = attribute
|
14
|
-
@
|
10
|
+
@message = message
|
15
11
|
end
|
16
12
|
|
17
13
|
def full_message
|
@@ -23,10 +19,6 @@ module Kangaru
|
|
23
19
|
def human_attribute
|
24
20
|
attribute.to_s.to_humanised
|
25
21
|
end
|
26
|
-
|
27
|
-
def message
|
28
|
-
MESSAGES[type] || raise("invalid message type")
|
29
|
-
end
|
30
22
|
end
|
31
23
|
end
|
32
24
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Kangaru
|
2
|
+
module Validation
|
3
|
+
class ModelValidator
|
4
|
+
attr_reader :model
|
5
|
+
|
6
|
+
def initialize(model:)
|
7
|
+
@model = model
|
8
|
+
end
|
9
|
+
|
10
|
+
def validate!(**rules)
|
11
|
+
rules.each do |attribute, validations|
|
12
|
+
validate_attribute!(attribute, validations)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def validate_attribute!(attribute, validations)
|
19
|
+
load_attribute_validator(attribute).validate!(**validations)
|
20
|
+
end
|
21
|
+
|
22
|
+
def load_attribute_validator(attribute)
|
23
|
+
AttributeValidator.new(model:, attribute:)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Kangaru
|
2
|
+
class Validator
|
3
|
+
attr_reader :model, :attribute, :params, :value
|
4
|
+
|
5
|
+
def initialize(model:, attribute:, **params)
|
6
|
+
@model = model
|
7
|
+
@attribute = attribute
|
8
|
+
@params = params
|
9
|
+
@value = model.public_send(attribute)
|
10
|
+
end
|
11
|
+
|
12
|
+
def validate
|
13
|
+
raise NotImplementedError
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_error!(message)
|
17
|
+
model.errors << Validation::Error.new(attribute:, message:)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Kangaru
|
2
|
+
module Validators
|
3
|
+
using Patches::Inflections
|
4
|
+
|
5
|
+
def self.get(name)
|
6
|
+
class_name = name.to_s.to_class_name(suffix: :validator)
|
7
|
+
|
8
|
+
from_kangaru(class_name) || from_application(class_name) ||
|
9
|
+
raise("#{class_name} is not defined")
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.from_kangaru(class_name)
|
13
|
+
return unless const_defined?(class_name)
|
14
|
+
|
15
|
+
const_get(class_name)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.from_application(class_name)
|
19
|
+
namespace = Kangaru.application&.const_get(:Validators)
|
20
|
+
|
21
|
+
return unless namespace&.const_defined?(class_name)
|
22
|
+
|
23
|
+
namespace.const_get(class_name)
|
24
|
+
end
|
25
|
+
|
26
|
+
private_class_method :from_kangaru
|
27
|
+
private_class_method :from_application
|
28
|
+
end
|
29
|
+
end
|
data/lib/kangaru/version.rb
CHANGED
data/lib/kangaru.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Kangaru
|
2
|
+
module Concern : Module
|
3
|
+
@included: untyped
|
4
|
+
|
5
|
+
def append_features: (untyped) -> void
|
6
|
+
|
7
|
+
def class_methods: { [self: singleton(Class)] -> void } -> void
|
8
|
+
|
9
|
+
def included: (?untyped?) ?{ -> void } -> void
|
10
|
+
|
11
|
+
def evaluate_concern_blocks!: (untyped) -> void
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Kangaru
|
2
|
+
module Attributable
|
3
|
+
extend Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def attributes: -> Array[Symbol]
|
7
|
+
|
8
|
+
def defaults: -> Hash[Symbol, untyped]
|
9
|
+
|
10
|
+
def set_default: (**untyped) -> void
|
11
|
+
|
12
|
+
@defaults: Hash[Symbol, untyped]
|
13
|
+
end
|
14
|
+
|
15
|
+
extend ClassMethods
|
16
|
+
|
17
|
+
attr_reader defaults: Hash[Symbol, untyped]
|
18
|
+
|
19
|
+
def instance_methods: -> Array[Symbol]
|
20
|
+
|
21
|
+
def initialize: (**untyped) -> void
|
22
|
+
|
23
|
+
def merge!: (**untyped) -> void
|
24
|
+
end
|
25
|
+
end
|
@@ -1,15 +1,13 @@
|
|
1
1
|
module Kangaru
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
extend ClassMethods
|
2
|
+
module Configurable
|
3
|
+
extend Concern
|
4
|
+
extend ClassMethods
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
def name: -> String
|
12
|
-
def config: -> untyped
|
6
|
+
module ClassMethods
|
7
|
+
def configurator_key: -> Symbol
|
13
8
|
end
|
9
|
+
|
10
|
+
def name: -> String
|
11
|
+
def config: -> untyped
|
14
12
|
end
|
15
13
|
end
|
@@ -1,28 +1,26 @@
|
|
1
1
|
module Kangaru
|
2
|
-
module
|
3
|
-
|
4
|
-
type validations = Hash[Symbol, Hash[Symbol, untyped]]
|
2
|
+
module Validatable
|
3
|
+
type validations = Hash[Symbol, Hash[Symbol, untyped]]
|
5
4
|
|
6
|
-
|
7
|
-
|
5
|
+
extend Concern
|
6
|
+
extend ClassMethods
|
8
7
|
|
9
|
-
|
8
|
+
attr_reader validation_rules: validations
|
10
9
|
|
11
|
-
|
12
|
-
|
10
|
+
module ClassMethods
|
11
|
+
attr_reader validation_rules: validations
|
13
12
|
|
14
|
-
|
15
|
-
|
13
|
+
def validates: (Symbol, **untyped) -> void
|
14
|
+
end
|
16
15
|
|
17
|
-
|
16
|
+
attr_reader errors: Array[Validation::Error]
|
18
17
|
|
19
|
-
|
18
|
+
def validate: -> void
|
20
19
|
|
21
|
-
|
20
|
+
def valid?: -> bool
|
22
21
|
|
23
|
-
|
22
|
+
private
|
24
23
|
|
25
|
-
|
26
|
-
end
|
24
|
+
attr_reader model_validator: Validation::ModelValidator
|
27
25
|
end
|
28
26
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module Kangaru
|
2
2
|
class Configurator
|
3
|
-
include
|
4
|
-
extend
|
3
|
+
include Attributable
|
4
|
+
extend Attributable::ClassMethods
|
5
5
|
|
6
|
-
include
|
7
|
-
extend
|
6
|
+
include Validatable
|
7
|
+
extend Validatable::ClassMethods
|
8
8
|
|
9
9
|
def self.key: -> Symbol
|
10
10
|
|
data/sig/kangaru/database.rbs
CHANGED
data/sig/kangaru/model.rbs
CHANGED
data/sig/kangaru/request.rbs
CHANGED
@@ -1,21 +1,19 @@
|
|
1
1
|
module Kangaru
|
2
2
|
module Validation
|
3
3
|
class AttributeValidator
|
4
|
+
type validations = Hash[Symbol, rule]
|
5
|
+
type rule = bool | Hash[Symbol, untyped]
|
6
|
+
|
4
7
|
attr_reader model: untyped
|
5
8
|
attr_reader attribute: Symbol
|
6
9
|
|
7
10
|
def initialize: (model: untyped, attribute: Symbol) -> void
|
8
11
|
|
9
|
-
def validate
|
12
|
+
def validate!: (**untyped) -> void
|
10
13
|
|
11
14
|
private
|
12
15
|
|
13
|
-
def
|
14
|
-
|
15
|
-
def load_validator: (
|
16
|
-
validator: Symbol,
|
17
|
-
params: Hash[Symbol, untyped]
|
18
|
-
) -> Validators::Validator
|
16
|
+
def load_validator: (validator: Symbol, **untyped) -> Validator
|
19
17
|
end
|
20
18
|
end
|
21
19
|
end
|
@@ -1,20 +1,16 @@
|
|
1
1
|
module Kangaru
|
2
2
|
module Validation
|
3
3
|
class Error
|
4
|
-
MESSAGES: Hash[Symbol, String]
|
5
|
-
|
6
4
|
attr_reader attribute: Symbol
|
7
|
-
attr_reader
|
5
|
+
attr_reader message: String
|
8
6
|
|
9
|
-
def initialize: (attribute: Symbol,
|
7
|
+
def initialize: (attribute: Symbol, message: String) -> void
|
10
8
|
|
11
9
|
def full_message: -> String
|
12
10
|
|
13
11
|
private
|
14
12
|
|
15
13
|
def human_attribute: -> String
|
16
|
-
|
17
|
-
def message: -> String
|
18
14
|
end
|
19
15
|
end
|
20
16
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Kangaru
|
2
|
+
module Validation
|
3
|
+
class ModelValidator
|
4
|
+
type rules = Hash[Symbol, AttributeValidator::validations]
|
5
|
+
|
6
|
+
attr_reader model: untyped
|
7
|
+
|
8
|
+
def initialize: (model: untyped) -> void
|
9
|
+
|
10
|
+
def validate!: (**rules) -> void
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def validate_attribute!: (Symbol, AttributeValidator::validations) -> void
|
15
|
+
|
16
|
+
def load_attribute_validator: (Symbol) -> AttributeValidator
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Kangaru
|
2
|
+
class Validator
|
3
|
+
attr_reader model: untyped
|
4
|
+
attr_reader attribute: Symbol
|
5
|
+
attr_reader params: Hash[Symbol, untyped]
|
6
|
+
attr_reader value: untyped
|
7
|
+
|
8
|
+
def initialize: (model: untyped, attribute: Symbol, **untyped) -> void
|
9
|
+
|
10
|
+
def validate: -> void
|
11
|
+
|
12
|
+
def add_error!: (String) -> void
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Kangaru
|
2
|
+
module Validators
|
3
|
+
def self.get: (String | Symbol) -> singleton(Validator)
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def self.from_kangaru: (String | Symbol) -> singleton(Validator)?
|
8
|
+
|
9
|
+
def self.from_application: (String | Symbol) -> singleton(Validator)?
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kangaru
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Welham
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-11-
|
11
|
+
date: 2023-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|
@@ -102,8 +102,8 @@ files:
|
|
102
102
|
- lib/kangaru/application.rb
|
103
103
|
- lib/kangaru/argument_parser.rb
|
104
104
|
- lib/kangaru/components/component.rb
|
105
|
-
- lib/kangaru/
|
106
|
-
- lib/kangaru/concerns/
|
105
|
+
- lib/kangaru/concern.rb
|
106
|
+
- lib/kangaru/concerns/attributable.rb
|
107
107
|
- lib/kangaru/concerns/configurable.rb
|
108
108
|
- lib/kangaru/concerns/validatable.rb
|
109
109
|
- lib/kangaru/config.rb
|
@@ -141,8 +141,10 @@ files:
|
|
141
141
|
- lib/kangaru/router.rb
|
142
142
|
- lib/kangaru/validation/attribute_validator.rb
|
143
143
|
- lib/kangaru/validation/error.rb
|
144
|
+
- lib/kangaru/validation/model_validator.rb
|
145
|
+
- lib/kangaru/validator.rb
|
146
|
+
- lib/kangaru/validators.rb
|
144
147
|
- lib/kangaru/validators/required_validator.rb
|
145
|
-
- lib/kangaru/validators/validator.rb
|
146
148
|
- lib/kangaru/version.rb
|
147
149
|
- rbs_collection.lock.yaml
|
148
150
|
- rbs_collection.yaml
|
@@ -151,8 +153,8 @@ files:
|
|
151
153
|
- sig/kangaru/application.rbs
|
152
154
|
- sig/kangaru/argument_parser.rbs
|
153
155
|
- sig/kangaru/components/component.rbs
|
154
|
-
- sig/kangaru/
|
155
|
-
- sig/kangaru/concerns/
|
156
|
+
- sig/kangaru/concern.rbs
|
157
|
+
- sig/kangaru/concerns/attributable.rbs
|
156
158
|
- sig/kangaru/concerns/configurable.rbs
|
157
159
|
- sig/kangaru/concerns/validatable.rbs
|
158
160
|
- sig/kangaru/config.rbs
|
@@ -188,8 +190,10 @@ files:
|
|
188
190
|
- sig/kangaru/router.rbs
|
189
191
|
- sig/kangaru/validation/attribute_validator.rbs
|
190
192
|
- sig/kangaru/validation/error.rbs
|
193
|
+
- sig/kangaru/validation/model_validator.rbs
|
194
|
+
- sig/kangaru/validator.rbs
|
195
|
+
- sig/kangaru/validators.rbs
|
191
196
|
- sig/kangaru/validators/required_validator.rbs
|
192
|
-
- sig/kangaru/validators/validator.rbs
|
193
197
|
homepage: https://github.com/apexatoll/kangaru
|
194
198
|
licenses:
|
195
199
|
- MIT
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module Kangaru
|
2
|
-
module Concerns
|
3
|
-
module AttributesConcern
|
4
|
-
extend Concern
|
5
|
-
|
6
|
-
class_methods do
|
7
|
-
def attributes
|
8
|
-
instance_methods.grep(/\w=$/).map do |attribute|
|
9
|
-
attribute.to_s.delete_suffix("=").to_sym
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def set_default(**attributes)
|
14
|
-
defaults.merge!(**attributes)
|
15
|
-
end
|
16
|
-
|
17
|
-
def defaults
|
18
|
-
@defaults ||= {}
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def initialize(**attributes)
|
23
|
-
attributes = self.class.defaults.merge(**attributes)
|
24
|
-
|
25
|
-
merge!(**attributes)
|
26
|
-
end
|
27
|
-
|
28
|
-
def merge!(**attributes)
|
29
|
-
attributes.slice(*self.class.attributes).each do |attr, value|
|
30
|
-
instance_variable_set(:"@#{attr}", value)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
module Kangaru
|
2
|
-
module Concerns
|
3
|
-
module Concern
|
4
|
-
def append_features(base)
|
5
|
-
super
|
6
|
-
evaluate_concern_blocks!(base)
|
7
|
-
end
|
8
|
-
|
9
|
-
def class_methods(&)
|
10
|
-
if const_defined?(:ClassMethods)
|
11
|
-
const_get(:ClassMethods)
|
12
|
-
else
|
13
|
-
const_set(:ClassMethods, Module.new)
|
14
|
-
end.module_eval(&)
|
15
|
-
end
|
16
|
-
|
17
|
-
def included(base = nil, &block)
|
18
|
-
super base if base
|
19
|
-
return if block.nil?
|
20
|
-
|
21
|
-
@included = block
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def evaluate_concern_blocks!(base)
|
27
|
-
base.extend(const_get(:ClassMethods)) if const_defined?(:ClassMethods)
|
28
|
-
|
29
|
-
base.class_eval(&@included) if instance_variable_defined?(:@included)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
module Kangaru
|
2
|
-
module Validators
|
3
|
-
class Validator
|
4
|
-
attr_reader :model, :attribute, :params, :value
|
5
|
-
|
6
|
-
def initialize(model:, attribute:, params:)
|
7
|
-
@model = model
|
8
|
-
@attribute = attribute
|
9
|
-
@params = params
|
10
|
-
@value = model.public_send(attribute)
|
11
|
-
end
|
12
|
-
|
13
|
-
def validate
|
14
|
-
raise NotImplementedError
|
15
|
-
end
|
16
|
-
|
17
|
-
def add_error!(type)
|
18
|
-
model.errors << Validation::Error.new(attribute:, type:)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module Kangaru
|
2
|
-
module Concerns
|
3
|
-
module AttributesConcern
|
4
|
-
extend Concern
|
5
|
-
|
6
|
-
module ClassMethods
|
7
|
-
def attributes: -> Array[Symbol]
|
8
|
-
|
9
|
-
def defaults: -> Hash[Symbol, untyped]
|
10
|
-
|
11
|
-
def set_default: (**untyped) -> void
|
12
|
-
|
13
|
-
@defaults: Hash[Symbol, untyped]
|
14
|
-
end
|
15
|
-
|
16
|
-
extend ClassMethods
|
17
|
-
|
18
|
-
attr_reader defaults: Hash[Symbol, untyped]
|
19
|
-
|
20
|
-
def instance_methods: -> Array[Symbol]
|
21
|
-
|
22
|
-
def initialize: (**untyped) -> void
|
23
|
-
|
24
|
-
def merge!: (**untyped) -> void
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module Kangaru
|
2
|
-
module Concerns
|
3
|
-
module Concern : Module
|
4
|
-
@included: untyped
|
5
|
-
|
6
|
-
def append_features: (untyped) -> void
|
7
|
-
|
8
|
-
def class_methods: { [self: singleton(Class)] -> void } -> void
|
9
|
-
|
10
|
-
def included: (?untyped?) ?{ -> void } -> void
|
11
|
-
|
12
|
-
def evaluate_concern_blocks!: (untyped) -> void
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module Kangaru
|
2
|
-
module Validators
|
3
|
-
class Validator
|
4
|
-
attr_reader model: untyped
|
5
|
-
attr_reader attribute: Symbol
|
6
|
-
attr_reader params: Hash[Symbol, untyped]
|
7
|
-
attr_reader value: untyped
|
8
|
-
|
9
|
-
def initialize: (
|
10
|
-
model: untyped,
|
11
|
-
attribute: Symbol,
|
12
|
-
params: Hash[Symbol, untyped]
|
13
|
-
) -> void
|
14
|
-
|
15
|
-
def validate: -> void
|
16
|
-
|
17
|
-
def add_error!: (Symbol) -> void
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|