statinize 0.1.1 → 0.2.2
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/lib/statinize/attribute.rb +12 -2
- data/lib/statinize/errors.rb +2 -0
- data/lib/statinize/statinizable.rb +25 -6
- data/lib/statinize/statinizer.rb +16 -0
- data/lib/statinize/validation.rb +10 -7
- data/lib/statinize/validators/type_validator.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1df88422c35ca62b3fe9b3efab22633e9f72a0e59690db7a005ae2cb9ad8cb4f
|
4
|
+
data.tar.gz: d72c1c0fdae71d168d082ee6e368970bdc81748cfa7ea293ec970065cd6232ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21b6c73e849260241b4a2301bdbdf1663ceff7279b666564761818618728058be4e5e67dd9d6d11a1f7adc382468d8b5e7fdfcfaf67acbb3b294879a415d7a28
|
7
|
+
data.tar.gz: c984be3998b0e6a4ee7e0c4c57263a88f9c1c9698fc91487a499980353c5be9b6a72c2f94ba87cf95d543b9ef5418303e32b2b3edd8299163660cd4c629bae33
|
data/lib/statinize/attribute.rb
CHANGED
@@ -8,7 +8,7 @@ module Statinize
|
|
8
8
|
@klass = klass
|
9
9
|
@name = name
|
10
10
|
@options = OptionsCollection.new
|
11
|
-
@options << opts.clone.extend(Options)
|
11
|
+
@options << opts.clone.extend(Options) unless opts.empty?
|
12
12
|
end
|
13
13
|
|
14
14
|
def self.create(klass, name, opts)
|
@@ -17,7 +17,17 @@ module Statinize
|
|
17
17
|
|
18
18
|
def create
|
19
19
|
statinizer.add_attribute(self) unless attribute?
|
20
|
-
klass.
|
20
|
+
klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
21
|
+
def #{name}
|
22
|
+
@#{name}
|
23
|
+
end
|
24
|
+
|
25
|
+
def #{name}=(attr)
|
26
|
+
@#{name} = attr
|
27
|
+
validate if respond_to?(:validate)
|
28
|
+
attr
|
29
|
+
end
|
30
|
+
RUBY_EVAL
|
21
31
|
self
|
22
32
|
end
|
23
33
|
|
data/lib/statinize/errors.rb
CHANGED
@@ -6,26 +6,45 @@ module Statinize
|
|
6
6
|
end
|
7
7
|
|
8
8
|
module PrependedMethods
|
9
|
-
def initialize(*args, **kwargs)
|
10
|
-
|
11
|
-
|
9
|
+
def initialize(*args, **kwargs, &block)
|
10
|
+
if private_methods(false).include? :initialize
|
11
|
+
super(*args, **kwargs, &block)
|
12
|
+
check_defined!(kwargs)
|
13
|
+
else
|
14
|
+
statinizer.attributes.map(&:name).each do |attr|
|
15
|
+
instance_variable_set("@#{attr}", kwargs[attr]) if kwargs.key?(attr)
|
16
|
+
end
|
12
17
|
end
|
13
18
|
|
14
19
|
define_validation
|
15
20
|
validate!
|
16
|
-
|
17
|
-
super(*args, **kwargs)
|
18
21
|
end
|
19
22
|
|
20
23
|
def validation
|
21
24
|
@validation ||= Validation.new(statinizer, self)
|
22
25
|
end
|
23
26
|
|
27
|
+
def attributes
|
28
|
+
@attributes = Hash[
|
29
|
+
statinizer.attributes.map { |a| [a.name, public_send(a.name)] }
|
30
|
+
]
|
31
|
+
end
|
32
|
+
|
24
33
|
alias_method :define_validation, :validation
|
25
34
|
|
26
35
|
def statinizer
|
27
36
|
self.class.statinizer
|
28
37
|
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def check_defined!(kwargs)
|
42
|
+
statinizer.attributes.map(&:name).each do |attr|
|
43
|
+
undefined_attrs << attr if public_send(attr) != kwargs[attr] || !kwargs.key?(attr)
|
44
|
+
end
|
45
|
+
|
46
|
+
raise UndefinedAttributeError, "Not all attributes defined in statinize block are defined in initialize"
|
47
|
+
end
|
29
48
|
end
|
30
49
|
|
31
50
|
module ClassMethods
|
@@ -34,7 +53,7 @@ module Statinize
|
|
34
53
|
|
35
54
|
statinizer.instance_eval(&block)
|
36
55
|
|
37
|
-
|
56
|
+
statinizer.check_validators_exist!
|
38
57
|
end
|
39
58
|
|
40
59
|
def statinizer
|
data/lib/statinize/statinizer.rb
CHANGED
@@ -20,6 +20,22 @@ module Statinize
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
+
def with(**options, &block)
|
24
|
+
trace = TracePoint.trace(:call) do |tp|
|
25
|
+
tp.disable
|
26
|
+
|
27
|
+
if %i[attribute validate].include? tp.method_id
|
28
|
+
tp.binding.local_variable_get(:options).merge!(options)
|
29
|
+
end
|
30
|
+
|
31
|
+
tp.enable
|
32
|
+
end
|
33
|
+
|
34
|
+
trace.enable
|
35
|
+
instance_exec(&block)
|
36
|
+
trace.disable
|
37
|
+
end
|
38
|
+
|
23
39
|
def force(force = nil)
|
24
40
|
force.nil? ? @force : @force = force
|
25
41
|
end
|
data/lib/statinize/validation.rb
CHANGED
@@ -13,8 +13,8 @@ module Statinize
|
|
13
13
|
|
14
14
|
def validate
|
15
15
|
@errors = Errors.new
|
16
|
-
@erroneous_attributes = Set.new
|
17
|
-
@erroneous_forced_attributes = Set.new
|
16
|
+
@erroneous_attributes = Hash.new { |h, k| h[k] = Set.new }
|
17
|
+
@erroneous_forced_attributes = Hash.new { |h, k| h[k] = Set.new }
|
18
18
|
|
19
19
|
fill_errors
|
20
20
|
end
|
@@ -48,9 +48,13 @@ module Statinize
|
|
48
48
|
next if validator_instance.valid?
|
49
49
|
next if validator_class == TypeValidator && cast(attr, option)
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
force = option[:force] ||
|
52
|
+
(statinizer.force? && option[:force].nil?)
|
53
|
+
|
54
|
+
if erroneous_attributes[attr.name].add? option
|
55
|
+
@errors << { attr.name => validator_instance.error }
|
56
|
+
end
|
57
|
+
erroneous_forced_attributes[attr.name].add option if force
|
54
58
|
end
|
55
59
|
end
|
56
60
|
end
|
@@ -65,8 +69,7 @@ module Statinize
|
|
65
69
|
def should_raise?
|
66
70
|
return false if valid?
|
67
71
|
|
68
|
-
|
69
|
-
erroneous_attributes.intersect?(erroneous_forced_attributes)
|
72
|
+
erroneous_attributes.keys.intersect?(erroneous_forced_attributes.keys)
|
70
73
|
end
|
71
74
|
|
72
75
|
def attributes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: statinize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Barseek
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-07-
|
11
|
+
date: 2022-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Statinization gem. Allows for creation of attributes for a class with
|
14
14
|
a given type.
|