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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7939bac74159eb5cd1e4307a80805ee0712fe5233148f550bd40c3ed99773d5d
4
- data.tar.gz: ac33edcb92e41acf77e823d61e22c2b2b6abe3b4c74036dfbf82d741ee208724
3
+ metadata.gz: 31b9f3d0195fc8b0f865279a79a54bd9beb8b0644d3f1b4371b6aa4163264024
4
+ data.tar.gz: '048e8c549d655980eee398e8ae445a7eb6900143f98c8e04449d5e2c82f0c9e6'
5
5
  SHA512:
6
- metadata.gz: '0796ae31ef53f91844d02950c00f52fe263794d13af076b5814292bd63e73ff36939f5141888f9c0db9f7c5ff7e3a739be11d1b04e89b8e76ee0eff21068ab05'
7
- data.tar.gz: 16423f86a33677355c35c4aa5184d2356739a16d2506faf94cc45c50b033139c81ff11c261d9e7383228bd500d583caafd1596e89d411c2d21238c540dc63608
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.1.1 | main | >= 2.2.0 | >= 3.2, < 6.1 |
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 3 different strategies to define default values.
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
@@ -70,7 +70,7 @@ module Micro
70
70
  protected
71
71
 
72
72
  def attributes=(arg)
73
- hash = Utils.stringify_hash_keys(arg)
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
- defined_attributes.each_with_object({}) do |key, memo|
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.respond_to?(:call)
100
- callable = default.is_a?(Proc) ? default : default.method(:call)
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
@@ -18,7 +18,7 @@ module Micro::Attributes
18
18
  validate, validates = options.values_at(:validate, :validates)
19
19
 
20
20
  self.validate(validate) if validate
21
- self.validates(attr_name, validates) if validates
21
+ self.validates(attr_name, validates.dup) if validates
22
22
  end
23
23
  end
24
24
 
@@ -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, Kind::Empty::HASH)
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 Attributes
5
- module Utils
6
- def self.stringify_hash_keys(arg)
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
- 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 }
15
- end
12
+ hash.each_with_object({}) { |(key, val), memo| memo[key.to_s] = val }
16
13
  end
17
14
 
18
- HashAccess = -> (hash, key) {
19
- return hash[key.to_s] unless hash[key.to_s].nil?
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
- hash[key.to_sym]
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Attributes
5
- VERSION = '2.1.1'.freeze
5
+ VERSION = '2.2.0'.freeze
6
6
  end
7
7
  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.1.1
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-08-28 00:00:00.000000000 Z
11
+ date: 2020-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kind