u-attributes 2.0.1 → 2.4.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.
@@ -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 __attribute_set(key, can_overwrite, options)
21
- name = key.to_s
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
- __call_after_attribute_set__(name, options)
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 __call_after_attribute_set__(attr_name, options); end
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 { |key, val| __attribute_set(key, true, default: val) }
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.to_s)
80
+ __attributes.member?(__attribute_key__(name))
38
81
  end
39
82
 
40
83
  def attribute(name, options = Kind::Empty::HASH)
41
- __attribute_set(name, false, options)
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
- __attribute_set(arg, false, Kind::Empty::HASH)
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
- __attribute_set(name, true, options)
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 Attributes
5
- module Utils
6
- def self.stringify_hash_keys(arg)
7
- hash = Kind::Of.(::Hash, arg)
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
- if hash.respond_to?(:transform_keys)
12
- hash.transform_keys { |key| key.to_s }
13
- else
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Attributes
5
- VERSION = '2.0.1'.freeze
5
+ VERSION = '2.4.0'.freeze
6
6
  end
7
7
  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 ActiveModelValidations
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::ActiveModelValidations)
40
+ base.send(:include, ::Micro::Attributes::Features::KeysAsSymbol)
32
41
  end
33
42
  end
34
43
 
35
- module StrictInitialize
44
+ module ActiveModelValidations
36
45
  def self.included(base)
37
46
  base.send(:include, Initialize)
38
- base.send(:include, ::Micro::Attributes::Features::Initialize::Strict)
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 DiffAndInitialize
54
+ module AMValidations_Diff
46
55
  def self.included(base)
47
- base.send(:include, ::Micro::Attributes)
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 DiffAndStrictInitialize
61
+ module AMValidations_Diff_Init
54
62
  def self.included(base)
55
- base.send(:include, DiffAndInitialize)
56
- base.send(:include, ::Micro::Attributes::Features::Initialize::Strict)
63
+ base.send(:include, ActiveModelValidations)
64
+ base.send(:include, ::Micro::Attributes::Features::Diff)
57
65
  end
58
66
  end
59
67
 
60
- module ActiveModelValidationsAndDiff
68
+ module AMValidations_Diff_Init_KeysAsSymbol
61
69
  def self.included(base)
62
- base.send(:include, ::Micro::Attributes)
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 ActiveModelValidationsAndInitialize
76
+ module AMValidations_Diff_InitStrict
69
77
  def self.included(base)
70
- base.send(:include, ::Micro::Attributes)
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 ActiveModelValidationsAndStrictInitialize
83
+ module AMValidations_Diff_InitStrict_KeysAsSymbol
77
84
  def self.included(base)
78
- base.send(:include, ActiveModelValidationsAndInitialize)
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 ActiveModelValidationsAndDiffAndInitialize
91
+ module AMValidations_Diff_KeysAsSymbol
84
92
  def self.included(base)
85
- base.send(:include, ::Micro::Attributes)
86
- base.send(:include, ::Micro::Attributes::Features::Initialize)
87
- base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
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 ActiveModelValidationsAndDiffAndStrictInitialize
139
+ module Diff_Init_KeysAsSymbol
93
140
  def self.included(base)
94
- base.send(:include, ActiveModelValidationsAndDiffAndInitialize)
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
@@ -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.1
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-08-21 00:00:00.000000000 Z
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