statinize 0.3.2 → 0.4.3
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 +25 -13
- data/lib/statinize/configuration.rb +2 -6
- data/lib/statinize/dsl.rb +7 -0
- data/lib/statinize/statinizable.rb +14 -6
- data/lib/statinize/statinizer.rb +8 -11
- data/lib/statinize/validation.rb +1 -1
- data/lib/statinize/validator.rb +1 -1
- data/lib/statinize.rb +24 -17
- 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: b3184b7bdea5121e7355c91ad65b4929f9e3a13fcd6f6849a5bf971bb4f3ad3a
|
4
|
+
data.tar.gz: 6ab5f9e27b4a3f337dfb6fbffd018d847a25bf743df3788824f219668ea58650
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 134c65ae58d093141b00c284367304951e5d7da83ba07a6dc0ababc35fd5fff4020fd6d6c7ce794bdb69095fa2bb6dcf53d3fdeb387c19e79c34e814c5a292c0
|
7
|
+
data.tar.gz: bbf362bb6cf6f87b190c4042f6897ad1b7c2ccf74ead7c6646dbc8bdf8e6d829e6503ca45806d306c6ccccd80b6b040b5a962e5ee9d5114a396bc7aa5e3959a1
|
data/lib/statinize/attribute.rb
CHANGED
@@ -2,26 +2,28 @@ module Statinize
|
|
2
2
|
class Attribute
|
3
3
|
include Comparable
|
4
4
|
|
5
|
-
attr_reader :klass, :name
|
6
|
-
attr_accessor :options
|
5
|
+
attr_reader :klass, :name, :default, :arg_name
|
6
|
+
attr_accessor :options_collection, :options
|
7
7
|
|
8
|
-
def initialize(klass, name,
|
8
|
+
def initialize(klass, name, options)
|
9
9
|
@klass = klass
|
10
10
|
@name = name
|
11
|
-
@options =
|
12
|
-
@
|
11
|
+
@options = options
|
12
|
+
@options_collection = OptionsCollection.new # TODO: think of a better way
|
13
|
+
@options_collection << options.clone.extend(Options) unless options.empty?
|
14
|
+
|
15
|
+
@default = options[:default] if options.key?(:default)
|
16
|
+
@arg_name = name
|
17
|
+
@name = options[:name] || name
|
13
18
|
end
|
14
19
|
|
15
|
-
def self.create(klass, name,
|
16
|
-
new(klass, name,
|
20
|
+
def self.create(klass, name, options = {})
|
21
|
+
new(klass, name, options).create
|
17
22
|
end
|
18
23
|
|
19
24
|
def create
|
20
|
-
|
21
|
-
|
22
|
-
else
|
23
|
-
statinizer.add_attribute(self)
|
24
|
-
end
|
25
|
+
attribute? ? override : add_attribute
|
26
|
+
|
25
27
|
klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
26
28
|
def #{name}
|
27
29
|
@#{name}
|
@@ -37,7 +39,7 @@ module Statinize
|
|
37
39
|
end
|
38
40
|
|
39
41
|
def add_options(opts)
|
40
|
-
|
42
|
+
options_collection << opts.extend(Options)
|
41
43
|
end
|
42
44
|
|
43
45
|
def <=>(other)
|
@@ -48,6 +50,16 @@ module Statinize
|
|
48
50
|
|
49
51
|
private
|
50
52
|
|
53
|
+
def override
|
54
|
+
attribute = statinizer.attributes.find { |a| a.name == name }
|
55
|
+
attribute.options_collection = options_collection
|
56
|
+
attribute.options = options
|
57
|
+
end
|
58
|
+
|
59
|
+
def add_attribute
|
60
|
+
statinizer.add_attribute(self)
|
61
|
+
end
|
62
|
+
|
51
63
|
def statinizer
|
52
64
|
klass.statinizer
|
53
65
|
end
|
data/lib/statinize/dsl.rb
CHANGED
@@ -16,15 +16,22 @@ module Statinize
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def with(**options, &block)
|
19
|
+
# create new statinizer instance
|
19
20
|
instance = self.class.new(klass)
|
20
21
|
instance.force(force)
|
21
22
|
|
23
|
+
# execute block in the context of that statinizer
|
24
|
+
# while it's attached to the klass
|
25
|
+
# then rewind and attach the original statinizer(self)
|
26
|
+
# back to the klass
|
22
27
|
klass.instance_variable_set(:@statinizer, instance)
|
23
28
|
instance.instance_exec(&block)
|
24
29
|
klass.instance_variable_set(:@statinizer, self)
|
25
30
|
|
31
|
+
# merge the newly created statinizer with the options
|
26
32
|
instance.merge_options(**options)
|
27
33
|
|
34
|
+
# populate self with the instance's attributes
|
28
35
|
populate(instance.attributes)
|
29
36
|
end
|
30
37
|
|
@@ -18,15 +18,17 @@ module Statinize
|
|
18
18
|
end
|
19
19
|
|
20
20
|
module PrependedMethods
|
21
|
-
def initialize(options = {}, *args,
|
22
|
-
symbolized =
|
21
|
+
def initialize(options = {}, *args, &block)
|
22
|
+
symbolized = options.transform_keys(&:to_sym)
|
23
|
+
|
24
|
+
instantiate_defaults
|
23
25
|
|
24
26
|
if private_methods(false).include? :initialize
|
25
|
-
super(*args,
|
27
|
+
super(options, *args, &block)
|
26
28
|
check_defined!(symbolized)
|
27
29
|
else
|
28
|
-
statinizer.attributes.
|
29
|
-
instance_variable_set("@#{attr}", symbolized[attr]) if symbolized.key?(attr)
|
30
|
+
statinizer.attributes.each do |attr|
|
31
|
+
instance_variable_set("@#{attr.name}", symbolized[attr.arg_name]) if symbolized.key?(attr.arg_name)
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
@@ -60,11 +62,17 @@ module Statinize
|
|
60
62
|
|
61
63
|
def check_defined!(options)
|
62
64
|
statinizer.attributes.map(&:name).each do |attr|
|
63
|
-
undefined_attrs << attr
|
65
|
+
undefined_attrs << attr unless options.key?(attr)
|
64
66
|
end
|
65
67
|
|
66
68
|
raise UndefinedAttributeError, "Not all attributes defined in statinize block are defined in initialize"
|
67
69
|
end
|
70
|
+
|
71
|
+
def instantiate_defaults
|
72
|
+
statinizer.attributes.select { |a| a.options.key?(:default) }.each do |attribute|
|
73
|
+
public_send("#{attribute.name}=", attribute.default)
|
74
|
+
end
|
75
|
+
end
|
68
76
|
end
|
69
77
|
|
70
78
|
module ClassMethods
|
data/lib/statinize/statinizer.rb
CHANGED
@@ -4,10 +4,6 @@ module Statinize
|
|
4
4
|
|
5
5
|
attr_reader :klass, :before_callbacks
|
6
6
|
|
7
|
-
def self.configure(&block)
|
8
|
-
Configuration.configure(&block)
|
9
|
-
end
|
10
|
-
|
11
7
|
def initialize(klass)
|
12
8
|
@klass = klass
|
13
9
|
@force = config.force
|
@@ -15,7 +11,7 @@ module Statinize
|
|
15
11
|
end
|
16
12
|
|
17
13
|
def config
|
18
|
-
@config ||=
|
14
|
+
@config ||= Config
|
19
15
|
end
|
20
16
|
|
21
17
|
def force?
|
@@ -46,7 +42,7 @@ module Statinize
|
|
46
42
|
|
47
43
|
def merge_options(**options)
|
48
44
|
attributes.each do |attribute|
|
49
|
-
attribute.
|
45
|
+
attribute.options_collection.each do |option|
|
50
46
|
option.merge!(options)
|
51
47
|
end
|
52
48
|
end
|
@@ -54,17 +50,18 @@ module Statinize
|
|
54
50
|
|
55
51
|
def populate(attrs)
|
56
52
|
attrs.each do |attr|
|
57
|
-
attribute attr.name
|
58
|
-
attributes
|
59
|
-
.
|
60
|
-
.options = attr.options.clone
|
53
|
+
attribute attr.arg_name, name: attr.name, default: attr.default
|
54
|
+
attributes.find { _1.name == attr.name }.tap do |attr_to_populate|
|
55
|
+
attr_to_populate.options_collection = attr.options_collection.clone
|
56
|
+
attr_to_populate.options = attr.options.clone
|
57
|
+
end
|
61
58
|
end
|
62
59
|
end
|
63
60
|
|
64
61
|
protected
|
65
62
|
|
66
63
|
def all_validators_defined?
|
67
|
-
attributes.map { |attr| attr.
|
64
|
+
attributes.map { |attr| attr.options_collection.all_validators_defined? }.all? { !!_1 }
|
68
65
|
end
|
69
66
|
end
|
70
67
|
end
|
data/lib/statinize/validation.rb
CHANGED
data/lib/statinize/validator.rb
CHANGED
data/lib/statinize.rb
CHANGED
@@ -1,20 +1,27 @@
|
|
1
1
|
require "set"
|
2
2
|
require "pry"
|
3
|
-
require "singleton"
|
4
3
|
require "bigdecimal/util"
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
4
|
+
require "statinize/statinizable"
|
5
|
+
require "statinize/dsl"
|
6
|
+
require "statinize/statinizer"
|
7
|
+
require "statinize/configuration"
|
8
|
+
require "statinize/attribute"
|
9
|
+
require "statinize/attribute/options"
|
10
|
+
require "statinize/attribute/options/conditions"
|
11
|
+
require "statinize/attribute/options_collection"
|
12
|
+
require "statinize/validator"
|
13
|
+
require "statinize/validation"
|
14
|
+
require "statinize/validators/type_validator"
|
15
|
+
require "statinize/validators/presence_validator"
|
16
|
+
require "statinize/validators/inclusion_validator"
|
17
|
+
require "statinize/validators/nil_validator"
|
18
|
+
require "statinize/caster"
|
19
|
+
require "statinize/errors"
|
20
|
+
|
21
|
+
module Statinize
|
22
|
+
def self.included(klass)
|
23
|
+
klass.include(Statinize::Statinizable)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.configure = yield Config
|
27
|
+
end
|
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.3
|
4
|
+
version: 0.4.3
|
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
|
+
date: 2022-11-05 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.
|