statinize 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- 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 +9 -0
- data/lib/statinize/validation.rb +8 -6
- 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: 49d4254b241b024535b320e063744a0ee01cfc45a8ec289265c89b3af65cc729
|
4
|
+
data.tar.gz: 178add180ff764c00eda41df13b9bf4d52eb51a6d4dd55b7bbc316793ac56dd6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b3c92ef8e0f345d92d307a30c8a7c60450fd9c9289e10ee88412e8bcee0859334c6be936a3e806eb08add2d270aa577658abd3998db505c962d8670cf19d723
|
7
|
+
data.tar.gz: 36a54dde0c7c615eb1610e5a0249d43c374eaed76bd8a6a447acd5fffbc62e74294c85a986c788a7c07b43d45aff39cb1629714327ff1f27eccf9ea46d07157a
|
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
@@ -8,6 +8,8 @@ module Statinize
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def attribute(*attrs, **options)
|
11
|
+
options.merge!(@validate_with) if @validate_with
|
12
|
+
|
11
13
|
attrs.each do |attr|
|
12
14
|
Attribute.create(klass, attr, options)
|
13
15
|
end
|
@@ -20,6 +22,13 @@ module Statinize
|
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
25
|
+
def validate_with(**kwargs, &block)
|
26
|
+
@validate_with = kwargs
|
27
|
+
instance_exec(&block)
|
28
|
+
|
29
|
+
remove_instance_variable(:@validate_with) if @validate_with
|
30
|
+
end
|
31
|
+
|
23
32
|
def force(force = nil)
|
24
33
|
force.nil? ? @force : @force = force
|
25
34
|
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,8 +48,11 @@ module Statinize
|
|
48
48
|
next if validator_instance.valid?
|
49
49
|
next if validator_class == TypeValidator && cast(attr, option)
|
50
50
|
|
51
|
-
|
52
|
-
|
51
|
+
force = option[:force] ||
|
52
|
+
(statinizer.force? && option[:force].nil?)
|
53
|
+
|
54
|
+
erroneous_attributes[attr.name].add option
|
55
|
+
erroneous_forced_attributes[attr.name].add option if force
|
53
56
|
@errors << { attr.name => validator_instance.error }
|
54
57
|
end
|
55
58
|
end
|
@@ -65,8 +68,7 @@ module Statinize
|
|
65
68
|
def should_raise?
|
66
69
|
return false if valid?
|
67
70
|
|
68
|
-
|
69
|
-
erroneous_attributes.intersect?(erroneous_forced_attributes)
|
71
|
+
erroneous_attributes.keys.intersect?(erroneous_forced_attributes.keys)
|
70
72
|
end
|
71
73
|
|
72
74
|
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.0
|
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-08 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.
|