u-attributes 2.0.1 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -2
- data/Gemfile +10 -1
- data/README.md +274 -47
- data/lib/micro/attributes.rb +69 -20
- data/lib/micro/attributes/diff.rb +26 -14
- data/lib/micro/attributes/features.rb +72 -77
- data/lib/micro/attributes/features/activemodel_validations.rb +2 -2
- data/lib/micro/attributes/features/initialize/strict.rb +6 -26
- data/lib/micro/attributes/features/keys_as_symbol.rb +31 -0
- data/lib/micro/attributes/macros.rb +58 -10
- data/lib/micro/attributes/utils.rb +48 -9
- data/lib/micro/attributes/version.rb +1 -1
- data/lib/micro/attributes/with.rb +107 -25
- data/u-attributes.gemspec +1 -1
- metadata +3 -2
@@ -3,10 +3,49 @@
|
|
3
3
|
module Micro
|
4
4
|
module Attributes
|
5
5
|
module Macros
|
6
|
+
def attributes_are_all_required?
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
10
|
+
def attributes_access
|
11
|
+
:indifferent
|
12
|
+
end
|
13
|
+
|
14
|
+
def __attribute_access__(value)
|
15
|
+
value
|
16
|
+
end
|
17
|
+
|
18
|
+
def __attribute_key__(value)
|
19
|
+
value.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
def __attributes_keys__(hash)
|
23
|
+
Utils::Hashes.stringify_keys(hash)
|
24
|
+
end
|
25
|
+
|
26
|
+
# NOTE: can't be renamed! It is used by u-case v4.
|
6
27
|
def __attributes_data__
|
7
28
|
@__attributes_data__ ||= {}
|
8
29
|
end
|
9
30
|
|
31
|
+
def __attributes_required__
|
32
|
+
@__attributes_required__ ||= Set.new
|
33
|
+
end
|
34
|
+
|
35
|
+
def __attributes_required_add(name, is_required, hasnt_default)
|
36
|
+
if is_required || (attributes_are_all_required? && hasnt_default)
|
37
|
+
__attributes_required__.add(name)
|
38
|
+
end
|
39
|
+
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def __attributes_data_to_assign(name, options)
|
44
|
+
hasnt_default = !options.key?(:default)
|
45
|
+
|
46
|
+
hasnt_default ? __attributes_required_add(name, options[:required], hasnt_default) : options[:default]
|
47
|
+
end
|
48
|
+
|
10
49
|
def __attributes
|
11
50
|
@__attributes ||= Set.new
|
12
51
|
end
|
@@ -17,48 +56,57 @@ module Micro
|
|
17
56
|
attr_reader(name)
|
18
57
|
end
|
19
58
|
|
20
|
-
def
|
21
|
-
name = key
|
59
|
+
def __attribute_assign(key, can_overwrite, options)
|
60
|
+
name = __attribute_access__(__attribute_key__(key))
|
22
61
|
has_attribute = attribute?(name)
|
23
62
|
|
24
63
|
__attribute_reader(name) unless has_attribute
|
25
|
-
__attributes_data__[name] = options[:default] if can_overwrite || !has_attribute
|
26
64
|
|
27
|
-
|
65
|
+
__attributes_data__[name] = __attributes_data_to_assign(name, options) if can_overwrite || !has_attribute
|
66
|
+
|
67
|
+
__call_after_attribute_assign__(name, options)
|
28
68
|
end
|
29
69
|
|
30
|
-
def
|
70
|
+
def __call_after_attribute_assign__(attr_name, options); end
|
31
71
|
|
72
|
+
# NOTE: can't be renamed! It is used by u-case v4.
|
32
73
|
def __attributes_set_after_inherit__(arg)
|
33
|
-
arg.each
|
74
|
+
arg.each do |key, val|
|
75
|
+
__attribute_assign(key, true, val ? { default: val } : {})
|
76
|
+
end
|
34
77
|
end
|
35
78
|
|
36
79
|
def attribute?(name)
|
37
|
-
__attributes.member?(name
|
80
|
+
__attributes.member?(__attribute_key__(name))
|
38
81
|
end
|
39
82
|
|
40
83
|
def attribute(name, options = Kind::Empty::HASH)
|
41
|
-
|
84
|
+
__attribute_assign(name, false, options)
|
42
85
|
end
|
43
86
|
|
44
87
|
def attributes(*args)
|
45
88
|
return __attributes.to_a if args.empty?
|
46
89
|
|
47
90
|
args.flatten!
|
91
|
+
|
92
|
+
options =
|
93
|
+
args.size > 1 && args.last.is_a?(::Hash) ? args.pop : Kind::Empty::HASH
|
94
|
+
|
48
95
|
args.each do |arg|
|
49
96
|
if arg.is_a?(String) || arg.is_a?(Symbol)
|
50
|
-
|
97
|
+
__attribute_assign(arg, false, options)
|
51
98
|
else
|
52
99
|
raise Kind::Error.new('String/Symbol'.freeze, arg)
|
53
100
|
end
|
54
101
|
end
|
55
102
|
end
|
56
103
|
|
104
|
+
# NOTE: can't be renamed! It is used by u-case v4.
|
57
105
|
module ForSubclasses
|
58
106
|
WRONG_NUMBER_OF_ARGS = 'wrong number of arguments (given 0, expected 1 or more)'.freeze
|
59
107
|
|
60
108
|
def attribute!(name, options = Kind::Empty::HASH)
|
61
|
-
|
109
|
+
__attribute_assign(name, true, options)
|
62
110
|
end
|
63
111
|
|
64
112
|
private_constant :WRONG_NUMBER_OF_ARGS
|
@@ -1,17 +1,56 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module Micro
|
4
|
-
module
|
5
|
-
module
|
6
|
-
def self.
|
7
|
-
|
3
|
+
module Micro::Attributes
|
4
|
+
module Utils
|
5
|
+
module Hashes
|
6
|
+
def self.kind(hash)
|
7
|
+
Kind::Of.(::Hash, hash)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.stringify_keys(arg)
|
11
|
+
hash = kind(arg)
|
12
|
+
|
13
|
+
return hash if hash.empty?
|
14
|
+
return hash.transform_keys(&:to_s) if hash.respond_to?(:transform_keys)
|
15
|
+
|
16
|
+
hash.each_with_object({}) { |(key, val), memo| memo[key.to_s] = val }
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.symbolize_keys(arg)
|
20
|
+
hash = kind(arg)
|
8
21
|
|
9
22
|
return hash if hash.empty?
|
23
|
+
return hash.transform_keys(&:to_sym) if hash.respond_to?(:transform_keys)
|
24
|
+
|
25
|
+
hash.each_with_object({}) { |(key, val), memo| memo[key.to_sym] = val }
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.keys_as(type, hash)
|
29
|
+
return kind(hash) unless type
|
30
|
+
|
31
|
+
return symbolize_keys(hash) if type == Symbol || type == :symbol
|
32
|
+
return stringify_keys(hash) if type == String || type == :string
|
33
|
+
|
34
|
+
raise ArgumentError, 'argument must be one of these values: :symbol, :string, Symbol, String'.freeze
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.assoc(hash, key)
|
38
|
+
value = hash[key.to_s]
|
39
|
+
|
40
|
+
value.nil? ? hash[key.to_sym] : value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module ExtractAttribute
|
45
|
+
def self.call(object, key:)
|
46
|
+
return object.public_send(key) if object.respond_to?(key)
|
47
|
+
|
48
|
+
Hashes.assoc(object, key) if object.respond_to?(:[])
|
49
|
+
end
|
10
50
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
hash.each_with_object({}) { |(key, val), memo| memo[key.to_s] = val }
|
51
|
+
def self.from(object, keys:)
|
52
|
+
Kind::Of.(::Array, keys).each_with_object({}) do |key, memo|
|
53
|
+
memo[key] = call(object, key: key)
|
15
54
|
end
|
16
55
|
end
|
17
56
|
end
|
@@ -3,8 +3,10 @@
|
|
3
3
|
require 'micro/attributes/features/diff'
|
4
4
|
require 'micro/attributes/features/initialize'
|
5
5
|
require 'micro/attributes/features/initialize/strict'
|
6
|
+
require 'micro/attributes/features/keys_as_symbol'
|
6
7
|
require 'micro/attributes/features/activemodel_validations'
|
7
8
|
|
9
|
+
|
8
10
|
module Micro
|
9
11
|
module Attributes
|
10
12
|
module With
|
@@ -25,76 +27,156 @@ module Micro
|
|
25
27
|
end
|
26
28
|
end
|
27
29
|
|
28
|
-
module
|
30
|
+
module StrictInitialize
|
31
|
+
def self.included(base)
|
32
|
+
base.send(:include, Initialize)
|
33
|
+
base.send(:include, ::Micro::Attributes::Features::Initialize::Strict)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module KeysAsSymbol
|
29
38
|
def self.included(base)
|
30
39
|
base.send(:include, ::Micro::Attributes)
|
31
|
-
base.send(:include, ::Micro::Attributes::Features::
|
40
|
+
base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
|
32
41
|
end
|
33
42
|
end
|
34
43
|
|
35
|
-
module
|
44
|
+
module ActiveModelValidations
|
36
45
|
def self.included(base)
|
37
46
|
base.send(:include, Initialize)
|
38
|
-
base.send(:include, ::Micro::Attributes::Features::
|
47
|
+
base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
|
39
48
|
end
|
40
49
|
end
|
41
50
|
|
42
51
|
#
|
43
52
|
# Combinations
|
44
53
|
#
|
45
|
-
module
|
54
|
+
module AMValidations_Diff
|
46
55
|
def self.included(base)
|
47
|
-
base.send(:include,
|
48
|
-
base.send(:include, ::Micro::Attributes::Features::Initialize)
|
56
|
+
base.send(:include, ActiveModelValidations)
|
49
57
|
base.send(:include, ::Micro::Attributes::Features::Diff)
|
50
58
|
end
|
51
59
|
end
|
52
60
|
|
53
|
-
module
|
61
|
+
module AMValidations_Diff_Init
|
54
62
|
def self.included(base)
|
55
|
-
base.send(:include,
|
56
|
-
base.send(:include, ::Micro::Attributes::Features::
|
63
|
+
base.send(:include, ActiveModelValidations)
|
64
|
+
base.send(:include, ::Micro::Attributes::Features::Diff)
|
57
65
|
end
|
58
66
|
end
|
59
67
|
|
60
|
-
module
|
68
|
+
module AMValidations_Diff_Init_KeysAsSymbol
|
61
69
|
def self.included(base)
|
62
|
-
base.send(:include,
|
63
|
-
base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
|
70
|
+
base.send(:include, ActiveModelValidations)
|
64
71
|
base.send(:include, ::Micro::Attributes::Features::Diff)
|
72
|
+
base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
|
65
73
|
end
|
66
74
|
end
|
67
75
|
|
68
|
-
module
|
76
|
+
module AMValidations_Diff_InitStrict
|
69
77
|
def self.included(base)
|
70
|
-
base.send(:include,
|
71
|
-
base.send(:include, ::Micro::Attributes::Features::Initialize)
|
72
|
-
base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
|
78
|
+
base.send(:include, AMValidations_Diff_Init)
|
79
|
+
base.send(:include, ::Micro::Attributes::Features::Initialize::Strict)
|
73
80
|
end
|
74
81
|
end
|
75
82
|
|
76
|
-
module
|
83
|
+
module AMValidations_Diff_InitStrict_KeysAsSymbol
|
77
84
|
def self.included(base)
|
78
|
-
base.send(:include,
|
85
|
+
base.send(:include, AMValidations_Diff_Init)
|
79
86
|
base.send(:include, ::Micro::Attributes::Features::Initialize::Strict)
|
87
|
+
base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
|
80
88
|
end
|
81
89
|
end
|
82
90
|
|
83
|
-
module
|
91
|
+
module AMValidations_Diff_KeysAsSymbol
|
84
92
|
def self.included(base)
|
85
|
-
base.send(:include,
|
86
|
-
base.send(:include, ::Micro::Attributes::Features::
|
87
|
-
|
93
|
+
base.send(:include, AMValidations_Diff)
|
94
|
+
base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
module AMValidations_Init
|
99
|
+
def self.included(base)
|
100
|
+
base.send(:include, ActiveModelValidations)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
module AMValidations_Init_KeysAsSymbol
|
105
|
+
def self.included(base)
|
106
|
+
base.send(:include, AMValidations_Init)
|
107
|
+
base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
module AMValidations_InitStrict
|
112
|
+
def self.included(base)
|
113
|
+
base.send(:include, ActiveModelValidations)
|
114
|
+
base.send(:include, ::Micro::Attributes::Features::Initialize::Strict)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
module AMValidations_InitStrict_KeysAsSymbol
|
119
|
+
def self.included(base)
|
120
|
+
base.send(:include, AMValidations_InitStrict)
|
121
|
+
base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
module AMValidations_KeysAsSymbol
|
126
|
+
def self.included(base)
|
127
|
+
base.send(:include, ActiveModelValidations)
|
128
|
+
base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
module Diff_Init
|
133
|
+
def self.included(base)
|
134
|
+
base.send(:include, Initialize)
|
88
135
|
base.send(:include, ::Micro::Attributes::Features::Diff)
|
89
136
|
end
|
90
137
|
end
|
91
138
|
|
92
|
-
module
|
139
|
+
module Diff_Init_KeysAsSymbol
|
93
140
|
def self.included(base)
|
94
|
-
base.send(:include,
|
141
|
+
base.send(:include, Diff_Init)
|
142
|
+
base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
module Diff_InitStrict
|
147
|
+
def self.included(base)
|
148
|
+
base.send(:include, Diff_Init)
|
95
149
|
base.send(:include, ::Micro::Attributes::Features::Initialize::Strict)
|
96
150
|
end
|
97
151
|
end
|
152
|
+
|
153
|
+
module Diff_InitStrict_KeysAsSymbol
|
154
|
+
def self.included(base)
|
155
|
+
base.send(:include, Diff_InitStrict)
|
156
|
+
base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
module Diff_KeysAsSymbol
|
161
|
+
def self.included(base)
|
162
|
+
base.send(:include, Diff)
|
163
|
+
base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
module Init_KeysAsSymbol
|
168
|
+
def self.included(base)
|
169
|
+
base.send(:include, Initialize)
|
170
|
+
base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
module InitStrict_KeysAsSymbol
|
175
|
+
def self.included(base)
|
176
|
+
base.send(:include, StrictInitialize)
|
177
|
+
base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
|
178
|
+
end
|
179
|
+
end
|
98
180
|
end
|
99
181
|
end
|
100
182
|
end
|
data/u-attributes.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
# Specify which files should be added to the gem when it is released.
|
21
21
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
22
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|assets)/}) }
|
24
24
|
end
|
25
25
|
spec.bindir = 'exe'
|
26
26
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: u-attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kind
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- lib/micro/attributes/features/diff.rb
|
86
86
|
- lib/micro/attributes/features/initialize.rb
|
87
87
|
- lib/micro/attributes/features/initialize/strict.rb
|
88
|
+
- lib/micro/attributes/features/keys_as_symbol.rb
|
88
89
|
- lib/micro/attributes/macros.rb
|
89
90
|
- lib/micro/attributes/utils.rb
|
90
91
|
- lib/micro/attributes/version.rb
|