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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.sh +9 -2
- data/.travis.yml +5 -5
- data/Gemfile +11 -2
- data/LICENSE.txt +2 -2
- data/README.md +418 -290
- data/Rakefile +5 -5
- data/assets/u-attributes_logo_v1.png +0 -0
- data/bin/console +4 -4
- data/lib/micro/attributes.rb +80 -41
- data/lib/micro/attributes/diff.rb +52 -0
- data/lib/micro/attributes/features.rb +41 -33
- data/lib/micro/attributes/features/activemodel_validations.rb +17 -23
- data/lib/micro/attributes/features/diff.rb +2 -50
- data/lib/micro/attributes/features/initialize.rb +5 -0
- data/lib/micro/attributes/features/initialize/strict.rb +19 -0
- data/lib/micro/attributes/macros.rb +57 -23
- data/lib/micro/attributes/utils.rb +36 -0
- data/lib/micro/attributes/version.rb +1 -1
- data/lib/micro/attributes/with.rb +6 -6
- data/u-attributes.gemspec +19 -12
- metadata +48 -11
- data/Gemfile.lock +0 -29
- data/lib/micro/attributes/attributes_utils.rb +0 -21
- data/lib/micro/attributes/features/strict_initialize.rb +0 -39
@@ -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
|