statinize 0.3.2 → 0.4.2
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 +25 -13
- data/lib/statinize/configuration.rb +2 -6
- data/lib/statinize/statinizable.rb +14 -6
- data/lib/statinize/statinizer.rb +4 -8
- 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: 9aab9a6ffd9f3b34b5275c2af057e44177f7881ce11fcea7ec4404a7cb933bad
|
4
|
+
data.tar.gz: 8f71ae54ea17777a0440acdb966c6bc7dd59688184fe4af9b98c31d318fdcae5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05b41cf02e7a4612e1ccc4e839df19733d33ee9e9e5d70296c0e5ce81a2bd5af51de2c0572b7791b284736538dd3e12a10a55f7c4a66872b3369ee628fd377df
|
7
|
+
data.tar.gz: 697f7d67836e0057bbb8a9697e611fe026bb91276a02fea2367ec4f494a113d1ae2187b3cebc597070c540160773124e5d8cc192cc2cab213a8729713fca7053
|
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
|
@@ -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
|
@@ -57,14 +53,14 @@ module Statinize
|
|
57
53
|
attribute attr.name
|
58
54
|
attributes
|
59
55
|
.find { _1.name == attr.name }
|
60
|
-
.
|
56
|
+
.options_collection = attr.options_collection.clone
|
61
57
|
end
|
62
58
|
end
|
63
59
|
|
64
60
|
protected
|
65
61
|
|
66
62
|
def all_validators_defined?
|
67
|
-
attributes.map { |attr| attr.
|
63
|
+
attributes.map { |attr| attr.options_collection.all_validators_defined? }.all? { !!_1 }
|
68
64
|
end
|
69
65
|
end
|
70
66
|
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.
|
4
|
+
version: 0.4.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-
|
11
|
+
date: 2022-11-04 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.
|