u-attributes 1.0.1 → 1.1.0
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/Gemfile.lock +1 -1
- data/README.md +24 -8
- data/lib/micro/attributes/features/strict_initialize.rb +0 -4
- data/lib/micro/attributes/features.rb +71 -14
- data/lib/micro/attributes/version.rb +1 -1
- data/lib/micro/attributes/with.rb +4 -8
- data/lib/micro/attributes.rb +4 -0
- 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: a9b3e7bb30f3fc6eb465c26378b99146dc3762f191f8554220bad22fd760cd21
|
4
|
+
data.tar.gz: 3c8de349ac5e966911877ca5c71f00ac5471f8b838323dd52b85a59e05b686a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14fbd07d3cbc6e31293417dba289b8bd91e0c4c1de7d74692f9b5498f7e016dcb4463ef70f0e6ff94e703e6e87ea514bbbd4ad36cf566367d2785533578f2e0c
|
7
|
+
data.tar.gz: ca9669124566003aad1d5cc520b9dcab7ec866a170b2561908a49eb8c236746352827e7fe65354584cd42120435ba7dde4723046dc6fc8d124491db000a885d1
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -326,14 +326,14 @@ p Person.new(name: 'John').attributes # {"age"=>nil, "name"=>"John"}
|
|
326
326
|
|
327
327
|
You can use the method `Micro::Attributes.features()` or `Micro::Attributes.with()` to combine and require only the features that better fit your needs.
|
328
328
|
|
329
|
-
|
329
|
+
But, if you desire...
|
330
|
+
1. only one feature, use the `Micro::Attributes.feature()` method.
|
331
|
+
2. except one or more features, use the `Micro::Attributes.without()` method.
|
330
332
|
|
331
333
|
```ruby
|
332
|
-
|
333
|
-
#
|
334
|
-
|
335
|
-
|
336
|
-
# Loading specific features
|
334
|
+
#===========================#
|
335
|
+
# Loading specific features #
|
336
|
+
#===========================#
|
337
337
|
|
338
338
|
class Job
|
339
339
|
include Micro::Attributes.feature(:diff)
|
@@ -346,8 +346,10 @@ class Job
|
|
346
346
|
end
|
347
347
|
end
|
348
348
|
|
349
|
-
|
350
|
-
#
|
349
|
+
#======================#
|
350
|
+
# Loading all features #
|
351
|
+
# --- #
|
352
|
+
#======================#
|
351
353
|
|
352
354
|
class Job
|
353
355
|
include Micro::Attributes.features
|
@@ -395,6 +397,20 @@ class Job
|
|
395
397
|
|
396
398
|
# Same of `include Micro::Attributes.with(:strict_initialize, :activemodel_validations)`
|
397
399
|
end
|
400
|
+
|
401
|
+
#=====================================#
|
402
|
+
# Loading except one or more features #
|
403
|
+
# ----- #
|
404
|
+
#=====================================#
|
405
|
+
|
406
|
+
class Job
|
407
|
+
include Micro::Attributes.without(:diff)
|
408
|
+
|
409
|
+
attributes :id, state: 'sleeping'
|
410
|
+
end
|
411
|
+
|
412
|
+
# Note:
|
413
|
+
# The method `Micro::Attributes.without()` returns `Micro::Attributes` if all features extensions were used.
|
398
414
|
```
|
399
415
|
|
400
416
|
### ActiveModel::Validations extension
|
@@ -6,10 +6,6 @@ module Micro::Attributes
|
|
6
6
|
MISSING_KEYWORD = 'missing keyword'.freeze
|
7
7
|
MISSING_KEYWORDS = 'missing keywords'.freeze
|
8
8
|
|
9
|
-
def self.included(base)
|
10
|
-
base.send(:include, ::Micro::Attributes::Features::Initialize)
|
11
|
-
end
|
12
|
-
|
13
9
|
protected def attributes=(arg)
|
14
10
|
arg_hash = AttributesUtils.stringify_hash_keys!(arg)
|
15
11
|
att_data = self.class.attributes_data({})
|
@@ -5,14 +5,26 @@ require "micro/attributes/with"
|
|
5
5
|
module Micro
|
6
6
|
module Attributes
|
7
7
|
module Features
|
8
|
-
|
8
|
+
extend self
|
9
|
+
|
10
|
+
ALL = [
|
11
|
+
DIFF = 'diff'.freeze,
|
12
|
+
INITIALIZE = 'initialize'.freeze,
|
13
|
+
STRICT_INITIALIZE = 'strict_initialize'.freeze,
|
14
|
+
ACTIVEMODEL_VALIDATIONS = 'activemodel_validations'.freeze
|
15
|
+
].sort.freeze
|
16
|
+
|
17
|
+
INVALID_NAME = [
|
18
|
+
'Invalid feature name! Available options: ',
|
19
|
+
ALL.map { |feature_name| ":#{feature_name}" }.join(', ')
|
20
|
+
].join
|
9
21
|
|
10
22
|
OPTIONS = {
|
11
23
|
# Features
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
24
|
+
DIFF => With::Diff,
|
25
|
+
INITIALIZE => With::Initialize,
|
26
|
+
STRICT_INITIALIZE => With::StrictInitialize,
|
27
|
+
ACTIVEMODEL_VALIDATIONS => With::ActiveModelValidations,
|
16
28
|
# Combinations
|
17
29
|
'diff:initialize' => With::DiffAndInitialize,
|
18
30
|
'diff:strict_initialize' => With::DiffAndStrictInitialize,
|
@@ -20,27 +32,72 @@ module Micro
|
|
20
32
|
'activemodel_validations:initialize' => With::ActiveModelValidationsAndInitialize,
|
21
33
|
'activemodel_validations:strict_initialize' => With::ActiveModelValidationsAndStrictInitialize,
|
22
34
|
'activemodel_validations:diff:initialize' => With::ActiveModelValidationsAndDiffAndInitialize,
|
23
|
-
'activemodel_validations:diff:strict_initialize' => With::ActiveModelValidationsAndDiffAndStrictInitialize
|
35
|
+
'activemodel_validations:diff:strict_initialize' => With::ActiveModelValidationsAndDiffAndStrictInitialize,
|
36
|
+
ALL.join(':') => With::ActiveModelValidationsAndDiffAndStrictInitialize
|
24
37
|
}.freeze
|
25
38
|
|
26
|
-
private_constant :OPTIONS
|
39
|
+
private_constant :OPTIONS, :INVALID_NAME
|
40
|
+
|
41
|
+
def all
|
42
|
+
@all ||= self.with(ALL)
|
43
|
+
end
|
27
44
|
|
28
|
-
def
|
29
|
-
|
45
|
+
def with(args)
|
46
|
+
valid_names!(args) do |names|
|
47
|
+
OPTIONS.fetch(names.sort.join(':'))
|
48
|
+
end
|
30
49
|
end
|
31
50
|
|
32
|
-
def
|
33
|
-
|
34
|
-
|
35
|
-
|
51
|
+
def without(args)
|
52
|
+
valid_names!(args) do |names_to_exclude|
|
53
|
+
names = except_options(names_to_exclude)
|
54
|
+
names.empty? ? ::Micro::Attributes : self.with(names)
|
55
|
+
end
|
36
56
|
end
|
37
57
|
|
38
|
-
def
|
58
|
+
def options(init, diff, activemodel_validations)
|
39
59
|
[init].tap do |options|
|
40
60
|
options << :diff if diff
|
41
61
|
options << :activemodel_validations if activemodel_validations
|
42
62
|
end
|
43
63
|
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def normalize_names(args)
|
68
|
+
Array(args).map { |arg| arg.to_s.downcase }.uniq
|
69
|
+
end
|
70
|
+
|
71
|
+
def valid_names?(names)
|
72
|
+
names.all? { |name| ALL.include?(name) }
|
73
|
+
end
|
74
|
+
|
75
|
+
def valid_names!(args)
|
76
|
+
names = normalize_names(args)
|
77
|
+
|
78
|
+
raise ArgumentError, INVALID_NAME if args.empty? || !valid_names?(names)
|
79
|
+
|
80
|
+
yield(names)
|
81
|
+
end
|
82
|
+
|
83
|
+
def has_strict_initialize?(names)
|
84
|
+
names.include?(STRICT_INITIALIZE)
|
85
|
+
end
|
86
|
+
|
87
|
+
def any_kind_of_initialize?(names)
|
88
|
+
names.include?(INITIALIZE) || has_strict_initialize?(names)
|
89
|
+
end
|
90
|
+
|
91
|
+
def an_initialize?(name)
|
92
|
+
name == INITIALIZE || name == STRICT_INITIALIZE
|
93
|
+
end
|
94
|
+
|
95
|
+
def except_options(names_to_exclude)
|
96
|
+
(ALL - names_to_exclude).tap do |names|
|
97
|
+
names.delete_if { |name| an_initialize?(name) } if any_kind_of_initialize?(names_to_exclude)
|
98
|
+
names.delete_if { |name| name == INITIALIZE } if has_strict_initialize?(names)
|
99
|
+
end
|
100
|
+
end
|
44
101
|
end
|
45
102
|
end
|
46
103
|
end
|
@@ -34,7 +34,7 @@ module Micro
|
|
34
34
|
|
35
35
|
module StrictInitialize
|
36
36
|
def self.included(base)
|
37
|
-
base.send(:include,
|
37
|
+
base.send(:include, Initialize)
|
38
38
|
base.send(:include, ::Micro::Attributes::Features::StrictInitialize)
|
39
39
|
end
|
40
40
|
end
|
@@ -52,9 +52,8 @@ module Micro
|
|
52
52
|
|
53
53
|
module DiffAndStrictInitialize
|
54
54
|
def self.included(base)
|
55
|
-
base.send(:include,
|
55
|
+
base.send(:include, DiffAndInitialize)
|
56
56
|
base.send(:include, ::Micro::Attributes::Features::StrictInitialize)
|
57
|
-
base.send(:include, ::Micro::Attributes::Features::Diff)
|
58
57
|
end
|
59
58
|
end
|
60
59
|
|
@@ -76,9 +75,8 @@ module Micro
|
|
76
75
|
|
77
76
|
module ActiveModelValidationsAndStrictInitialize
|
78
77
|
def self.included(base)
|
79
|
-
base.send(:include,
|
78
|
+
base.send(:include, ActiveModelValidationsAndInitialize)
|
80
79
|
base.send(:include, ::Micro::Attributes::Features::StrictInitialize)
|
81
|
-
base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
|
82
80
|
end
|
83
81
|
end
|
84
82
|
|
@@ -93,10 +91,8 @@ module Micro
|
|
93
91
|
|
94
92
|
module ActiveModelValidationsAndDiffAndStrictInitialize
|
95
93
|
def self.included(base)
|
96
|
-
base.send(:include,
|
94
|
+
base.send(:include, ActiveModelValidationsAndDiffAndInitialize)
|
97
95
|
base.send(:include, ::Micro::Attributes::Features::StrictInitialize)
|
98
|
-
base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
|
99
|
-
base.send(:include, ::Micro::Attributes::Features::Diff)
|
100
96
|
end
|
101
97
|
end
|
102
98
|
end
|
data/lib/micro/attributes.rb
CHANGED
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: 1.0
|
4
|
+
version: 1.1.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: 2019-
|
11
|
+
date: 2019-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|