statinize 0.4.4 → 0.4.6
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 532ce092aa6b70f135589b09d8c8f7574105e7ecbc17036e3fd2d50ffaf47813
|
4
|
+
data.tar.gz: deb8189b91a4703070b769968d1858ff77a261afa9565a7695e957bdbf381a86
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83956b014f99a2a5438bbf0304d0bc094cd0ed83329269d1d7d46fa6ca5dd46abf2b19948e980216e1e5bb1a906a9670b26bcd966d2c327f5bd567f59e5f1340
|
7
|
+
data.tar.gz: 49bc1829a8955fccc561277a5568f7cad00fa129e6c20e8956d2037dbbea29600052eff2da1f0b8f5af720e87a9548f621cbd9488da855a3b53b8f79b652a96d
|
data/lib/statinize/attribute.rb
CHANGED
@@ -2,7 +2,7 @@ module Statinize
|
|
2
2
|
class Attribute
|
3
3
|
include Comparable
|
4
4
|
|
5
|
-
attr_reader :klass, :name, :default, :arg_name
|
5
|
+
attr_reader :klass, :name, :default, :default_exec, :arg_name
|
6
6
|
attr_accessor :options_collection, :options
|
7
7
|
|
8
8
|
def initialize(klass, name, options)
|
@@ -13,6 +13,7 @@ module Statinize
|
|
13
13
|
@options_collection << options.clone.extend(Options) unless options.empty?
|
14
14
|
|
15
15
|
@default = options[:default] if options.key?(:default)
|
16
|
+
@default_exec = options[:default_exec] if options.key?(:default_exec)
|
16
17
|
@arg_name = name
|
17
18
|
@name = options[:name] || name
|
18
19
|
end
|
@@ -72,6 +72,10 @@ module Statinize
|
|
72
72
|
statinizer.attributes.select { |a| a.options.key?(:default) }.each do |attribute|
|
73
73
|
public_send("#{attribute.name}=", attribute.default.deep_dup)
|
74
74
|
end
|
75
|
+
|
76
|
+
statinizer.attributes.select { |a| a.options.key?(:default_exec) }.each do |attribute|
|
77
|
+
public_send("#{attribute.name}=", instance_exec(&attribute.default_exec))
|
78
|
+
end
|
75
79
|
end
|
76
80
|
end
|
77
81
|
|
data/lib/statinize/statinizer.rb
CHANGED
@@ -50,7 +50,7 @@ module Statinize
|
|
50
50
|
|
51
51
|
def populate(attrs)
|
52
52
|
attrs.each do |attr|
|
53
|
-
attribute attr.arg_name, name: attr.name, default: attr.default
|
53
|
+
attribute attr.arg_name, name: attr.name, default: attr.default, default_exec: attr.default_exec
|
54
54
|
attributes.find { _1.name == attr.name }.tap do |attr_to_populate|
|
55
55
|
attr_to_populate.options_collection = attr.options_collection.clone
|
56
56
|
attr_to_populate.options = attr.options.clone
|
data/lib/statinize/validation.rb
CHANGED
@@ -43,7 +43,7 @@ module Statinize
|
|
43
43
|
attr_value = instance.public_send(attr.name)
|
44
44
|
|
45
45
|
option.validators.each do |validator_class, validator_value|
|
46
|
-
validator_instance = validator_class.new(attr_value, validator_value)
|
46
|
+
validator_instance = validator_class.new(attr_value, validator_value, instance)
|
47
47
|
|
48
48
|
if option[:type] && option.should_cast? && attr_value.nil? && (option[:presence] || option[:nil] == false)
|
49
49
|
cast(attr, option)
|
data/lib/statinize/validator.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
module Statinize
|
2
2
|
class Validator
|
3
|
-
NOT_VALIDATORS = %i[force cast if unless default name]
|
3
|
+
NOT_VALIDATORS = %i[force cast if unless default default_exec name]
|
4
4
|
|
5
|
-
attr_accessor :attr_value, :validator_value
|
5
|
+
attr_accessor :attr_value, :validator_value, :instance
|
6
6
|
|
7
|
-
def initialize(attr_value, validator_value)
|
7
|
+
def initialize(attr_value, validator_value, instance)
|
8
8
|
@attr_value = attr_value
|
9
9
|
@validator_value = validator_value
|
10
|
+
@instance = instance
|
10
11
|
end
|
11
12
|
|
12
13
|
def valid?
|
@@ -1,7 +1,11 @@
|
|
1
1
|
module Statinize
|
2
2
|
class TypeValidator < Validator
|
3
3
|
def valid?
|
4
|
-
|
4
|
+
if validator_value.is_a? Class
|
5
|
+
attr_value.is_a?(validator_value)
|
6
|
+
elsif validator_value.is_a? Proc
|
7
|
+
attr_value.is_a? instance.instance_exec(&validator_value)
|
8
|
+
end || attr_value.nil?
|
5
9
|
end
|
6
10
|
|
7
11
|
def error
|
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.
|
4
|
+
version: 0.4.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Barseek
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-11-
|
11
|
+
date: 2022-11-28 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.
|