u-attributes 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2748d97dc4a469f8bd54558d869b735095a44c58758d8ab48a372f400e525f2f
4
- data.tar.gz: c338340af08a1013d01c61ac657f6a326d2a375d0db6476f87e983a8c7d480ba
3
+ metadata.gz: a9b3e7bb30f3fc6eb465c26378b99146dc3762f191f8554220bad22fd760cd21
4
+ data.tar.gz: 3c8de349ac5e966911877ca5c71f00ac5471f8b838323dd52b85a59e05b686a1
5
5
  SHA512:
6
- metadata.gz: 9aefaa228794f860a3859b4f7f9503d6de6a971ce1e584c5180be96567619cf070feaf0ba85b8bf44f46d636c943d74898ec14c21d410db7a1d3bc48db50dbec
7
- data.tar.gz: d3d30b8579e0ea4b710b219c4f6ebc609c603ebcb568fd190632c4dcd5d35543bb01ec68bcc191c022b793e25c97aced729d9be290c725f769e13804ad976330
6
+ metadata.gz: 14fbd07d3cbc6e31293417dba289b8bd91e0c4c1de7d74692f9b5498f7e016dcb4463ef70f0e6ff94e703e6e87ea514bbbd4ad36cf566367d2785533578f2e0c
7
+ data.tar.gz: ca9669124566003aad1d5cc520b9dcab7ec866a170b2561908a49eb8c236746352827e7fe65354584cd42120435ba7dde4723046dc6fc8d124491db000a885d1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- u-attributes (1.0.1)
4
+ u-attributes (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
- Note: If you desire require only one feature, use the `Micro::Attributes.feature()` method.
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
- # Via Micro::Attributes.features() #
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
- # Loading all features
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
- INVALID_FEATURES = 'Invalid feature name! Available options: :initialize, :strict_initialize, :diff, :activemodel_validations'.freeze
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
- 'diff' => With::Diff,
13
- 'initialize' => With::Initialize,
14
- 'strict_initialize' => With::StrictInitialize,
15
- 'activemodel_validations' => With::ActiveModelValidations,
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 self.all
29
- With::ActiveModelValidationsAndDiffAndInitialize
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 self.with(names)
33
- option = OPTIONS[names.map { |name| name.to_s.downcase }.uniq.sort.join(':')]
34
- return option if option
35
- raise ArgumentError, INVALID_FEATURES
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 self.options(init, diff, activemodel_validations)
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Attributes
5
- VERSION = '1.0.1'.freeze
5
+ VERSION = '1.1.0'.freeze
6
6
  end
7
7
  end
@@ -34,7 +34,7 @@ module Micro
34
34
 
35
35
  module StrictInitialize
36
36
  def self.included(base)
37
- base.send(:include, ::Micro::Attributes)
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, ::Micro::Attributes)
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, ::Micro::Attributes)
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, ::Micro::Attributes)
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
@@ -30,6 +30,10 @@ module Micro
30
30
  features(*Features.options(:strict_initialize, diff, activemodel_validations))
31
31
  end
32
32
 
33
+ def self.without(*names)
34
+ Features.without(names)
35
+ end
36
+
33
37
  def self.with(*names)
34
38
  Features.with(names)
35
39
  end
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.1
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-07-31 00:00:00.000000000 Z
11
+ date: 2019-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake