u-attributes 2.1.1 → 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/README.md +2 -3
- data/lib/micro/attributes.rb +4 -14
- data/lib/micro/attributes/features/activemodel_validations.rb +1 -1
- data/lib/micro/attributes/macros.rb +5 -1
- data/lib/micro/attributes/utils.rb +24 -13
- data/lib/micro/attributes/version.rb +1 -1
- 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: 31b9f3d0195fc8b0f865279a79a54bd9beb8b0644d3f1b4371b6aa4163264024
|
4
|
+
data.tar.gz: '048e8c549d655980eee398e8ae445a7eb6900143f98c8e04449d5e2c82f0c9e6'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a776e02b0708d566156ce8adc7d0ad5813a7c752f8f19615eeea734b46ee34995d69a8cab263ed1e1ffbba64140eee4a630ff18fdcb6cf54cd5fb0a404d6fe8
|
7
|
+
data.tar.gz: 4e7869905c50b2f0ea288f404900c3c2b645ca3c8cad6404c2d8aa8913a0ad87874b43288c8f408e1a23252d9ab114fa8664a6a21914f71c2ffbf4ee04bb3d68
|
data/README.md
CHANGED
@@ -75,7 +75,7 @@ gem 'u-attributes'
|
|
75
75
|
|
76
76
|
| u-attributes | branch | ruby | activemodel |
|
77
77
|
| -------------- | ------- | -------- | ------------- |
|
78
|
-
| 2.
|
78
|
+
| 2.2.0 | main | >= 2.2.0 | >= 3.2, < 6.1 |
|
79
79
|
| 1.2.0 | v1.x | >= 2.2.0 | >= 3.2, < 6.1 |
|
80
80
|
|
81
81
|
> **Note**: The activemodel is an optional dependency, this module [can be enabled](#activemodelvalidation-extension) to validate the attributes.
|
@@ -320,10 +320,9 @@ class Person
|
|
320
320
|
end
|
321
321
|
```
|
322
322
|
|
323
|
-
There are
|
323
|
+
There are two different strategies to define default values.
|
324
324
|
1. Pass a regular object, like in the previous example.
|
325
325
|
2. Pass a `proc`/`lambda`, and if it has an argument you will receive the attribute value to do something before assign it.
|
326
|
-
3. Pass a **callable**, that is, a `class`, `module` or `instance` which responds to the `call` method. The behavior will be like the previous item (`proc`/`lambda`).
|
327
326
|
|
328
327
|
```ruby
|
329
328
|
class Person
|
data/lib/micro/attributes.rb
CHANGED
@@ -70,7 +70,7 @@ module Micro
|
|
70
70
|
protected
|
71
71
|
|
72
72
|
def attributes=(arg)
|
73
|
-
hash = Utils.
|
73
|
+
hash = Utils::Hashes.stringify_keys(arg)
|
74
74
|
|
75
75
|
__attributes_missing!(hash)
|
76
76
|
|
@@ -79,16 +79,8 @@ module Micro
|
|
79
79
|
|
80
80
|
private
|
81
81
|
|
82
|
-
ExtractAttribute = -> (other, key) {
|
83
|
-
return Utils::HashAccess.(other, key) if other.respond_to?(:[])
|
84
|
-
|
85
|
-
other.public_send(key) if other.respond_to?(key)
|
86
|
-
}
|
87
|
-
|
88
82
|
def extract_attributes_from(other)
|
89
|
-
|
90
|
-
memo[key] = ExtractAttribute.(other, key)
|
91
|
-
end
|
83
|
+
Utils::ExtractAttribute.from(other, keys: defined_attributes)
|
92
84
|
end
|
93
85
|
|
94
86
|
def __attributes
|
@@ -96,10 +88,8 @@ module Micro
|
|
96
88
|
end
|
97
89
|
|
98
90
|
FetchValueToAssign = -> (value, default) do
|
99
|
-
if default.
|
100
|
-
|
101
|
-
|
102
|
-
callable.arity > 0 ? callable.call(value) : callable.call
|
91
|
+
if default.is_a?(Proc)
|
92
|
+
default.arity > 0 ? default.call(value) : default.call
|
103
93
|
else
|
104
94
|
value.nil? ? default : value
|
105
95
|
end
|
@@ -72,9 +72,13 @@ module Micro
|
|
72
72
|
return __attributes.to_a if args.empty?
|
73
73
|
|
74
74
|
args.flatten!
|
75
|
+
|
76
|
+
options =
|
77
|
+
args.size > 1 && args.last.is_a?(::Hash) ? args.pop : Kind::Empty::HASH
|
78
|
+
|
75
79
|
args.each do |arg|
|
76
80
|
if arg.is_a?(String) || arg.is_a?(Symbol)
|
77
|
-
__attribute_assign(arg, false,
|
81
|
+
__attribute_assign(arg, false, options)
|
78
82
|
else
|
79
83
|
raise Kind::Error.new('String/Symbol'.freeze, arg)
|
80
84
|
end
|
@@ -1,25 +1,36 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module Micro
|
4
|
-
module
|
5
|
-
module
|
6
|
-
def self.
|
3
|
+
module Micro::Attributes
|
4
|
+
module Utils
|
5
|
+
module Hashes
|
6
|
+
def self.stringify_keys(arg)
|
7
7
|
hash = Kind::Of.(::Hash, arg)
|
8
8
|
|
9
9
|
return hash if hash.empty?
|
10
|
+
return hash.transform_keys(&:to_s) if hash.respond_to?(:transform_keys)
|
10
11
|
|
11
|
-
|
12
|
-
hash.transform_keys { |key| key.to_s }
|
13
|
-
else
|
14
|
-
hash.each_with_object({}) { |(key, val), memo| memo[key.to_s] = val }
|
15
|
-
end
|
12
|
+
hash.each_with_object({}) { |(key, val), memo| memo[key.to_s] = val }
|
16
13
|
end
|
17
14
|
|
18
|
-
|
19
|
-
|
15
|
+
def self.get(hash, key)
|
16
|
+
value = hash[key.to_s]
|
17
|
+
|
18
|
+
value.nil? ? hash[key.to_sym] : value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module ExtractAttribute
|
23
|
+
def self.call(object, key:)
|
24
|
+
return object.public_send(key) if object.respond_to?(key)
|
20
25
|
|
21
|
-
|
22
|
-
|
26
|
+
Hashes.get(object, key) if object.respond_to?(:[])
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.from(object, keys:)
|
30
|
+
Kind::Of.(::Array, keys).each_with_object({}) do |key, memo|
|
31
|
+
memo[key] = call(object, key: key)
|
32
|
+
end
|
33
|
+
end
|
23
34
|
end
|
24
35
|
end
|
25
36
|
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: 2.
|
4
|
+
version: 2.2.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-
|
11
|
+
date: 2020-09-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kind
|