u-attributes 1.2.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Micro
4
- module Attributes
5
- module AttributesUtils
6
- ARGUMENT_ERROR_MSG = 'argument must be a Hash'.freeze
7
-
8
- def self.hash_argument!(arg)
9
- return arg if arg.is_a?(Hash)
10
-
11
- raise ArgumentError, ARGUMENT_ERROR_MSG
12
- end
13
-
14
- def self.stringify_hash_keys!(arg)
15
- hash_argument!(arg).each_with_object({}) do |(key, val), memo|
16
- memo[key.to_s] = val
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Micro::Attributes
4
- module Features
5
- module StrictInitialize
6
- MISSING_KEYWORD = 'missing keyword'.freeze
7
- MISSING_KEYWORDS = 'missing keywords'.freeze
8
-
9
- protected def attributes=(arg)
10
- arg_hash = AttributesUtils.stringify_hash_keys!(arg)
11
- att_data = self.class.attributes_data({})
12
-
13
- attributes_missing!(ref: att_data, arg: arg_hash)
14
-
15
- att_data.merge(arg_hash).each { |name, value| __attribute_set(name, value) }
16
-
17
- __attributes.freeze
18
- end
19
-
20
- private def attributes_missing!(ref:, arg:)
21
- missing_keys = attributes_missing(ref, arg)
22
-
23
- return if missing_keys.empty?
24
-
25
- label = missing_keys.size == 1 ? MISSING_KEYWORD : MISSING_KEYWORDS
26
-
27
- raise ArgumentError, "#{label}: #{missing_keys.join(', ')}"
28
- end
29
-
30
- private def attributes_missing(ref, arg)
31
- ref.each_with_object([]) do |(key, val), memo|
32
- memo << ":#{key}" if val.nil? && !arg.has_key?(key)
33
- end
34
- end
35
-
36
- private_constant :MISSING_KEYWORD, :MISSING_KEYWORDS
37
- end
38
- end
39
- end