u-attributes 2.2.0 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.sh +33 -19
- data/.travis.yml +12 -7
- data/Gemfile +15 -10
- data/README.md +192 -43
- data/lib/micro/attributes.rb +70 -20
- data/lib/micro/attributes/diff.rb +26 -14
- data/lib/micro/attributes/features.rb +128 -75
- data/lib/micro/attributes/features/accept.rb +132 -0
- data/lib/micro/attributes/features/accept/strict.rb +26 -0
- data/lib/micro/attributes/features/activemodel_validations.rb +53 -9
- data/lib/micro/attributes/features/initialize.rb +3 -8
- data/lib/micro/attributes/features/keys_as_symbol.rb +31 -0
- data/lib/micro/attributes/macros.rb +146 -27
- data/lib/micro/attributes/utils.rb +42 -22
- data/lib/micro/attributes/version.rb +1 -1
- data/test.sh +7 -3
- data/u-attributes.gemspec +5 -6
- metadata +14 -14
- data/assets/u-attributes_logo_v1.png +0 -0
- data/lib/micro/attributes/with.rb +0 -100
@@ -1,36 +1,56 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module Micro::Attributes
|
4
|
-
module Utils
|
5
|
-
module Hashes
|
6
|
-
def self.stringify_keys(arg)
|
7
|
-
hash = Kind::Of.(::Hash, arg)
|
3
|
+
module Micro::Attributes::Utils
|
8
4
|
|
9
|
-
|
10
|
-
|
5
|
+
module Hashes
|
6
|
+
extend self
|
11
7
|
|
12
|
-
|
13
|
-
|
8
|
+
def stringify_keys(arg)
|
9
|
+
hash = Kind::Hash[arg]
|
14
10
|
|
15
|
-
|
16
|
-
|
11
|
+
return hash if hash.empty?
|
12
|
+
return hash.transform_keys(&:to_s) if hash.respond_to?(:transform_keys)
|
17
13
|
|
18
|
-
|
19
|
-
end
|
14
|
+
hash.each_with_object({}) { |(key, val), memo| memo[key.to_s] = val }
|
20
15
|
end
|
21
16
|
|
22
|
-
|
23
|
-
|
24
|
-
return object.public_send(key) if object.respond_to?(key)
|
17
|
+
def symbolize_keys(arg)
|
18
|
+
hash = Kind::Hash[arg]
|
25
19
|
|
26
|
-
|
27
|
-
|
20
|
+
return hash if hash.empty?
|
21
|
+
return hash.transform_keys(&:to_sym) if hash.respond_to?(:transform_keys)
|
22
|
+
|
23
|
+
hash.each_with_object({}) { |(key, val), memo| memo[key.to_sym] = val }
|
24
|
+
end
|
25
|
+
|
26
|
+
def keys_as(type, hash)
|
27
|
+
return Kind::Hash[hash] unless type
|
28
|
+
|
29
|
+
return symbolize_keys(hash) if type == Symbol || type == :symbol
|
30
|
+
return stringify_keys(hash) if type == String || type == :string
|
31
|
+
|
32
|
+
raise ArgumentError, 'argument must be one of these values: :symbol, :string, Symbol, String'.freeze
|
33
|
+
end
|
28
34
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
35
|
+
def assoc(hash, key)
|
36
|
+
value = hash[key.to_s]
|
37
|
+
|
38
|
+
value.nil? ? hash[key.to_sym] : value
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
module ExtractAttribute
|
43
|
+
def self.call(object, key:)
|
44
|
+
return object.public_send(key) if object.respond_to?(key)
|
45
|
+
|
46
|
+
Hashes.assoc(object, key) if object.respond_to?(:[])
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.from(object, keys:)
|
50
|
+
Kind::Array[keys].each_with_object({}) do |key, memo|
|
51
|
+
memo[key] = call(object, key: key)
|
33
52
|
end
|
34
53
|
end
|
35
54
|
end
|
55
|
+
|
36
56
|
end
|
data/test.sh
CHANGED
data/u-attributes.gemspec
CHANGED
@@ -9,18 +9,17 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ['Rodrigo Serradura']
|
10
10
|
spec.email = ['rodrigo.serradura@gmail.com']
|
11
11
|
|
12
|
-
spec.summary = %q{Create "immutable" objects
|
12
|
+
spec.summary = %q{Create "immutable" objects with no setters, just getters.}
|
13
13
|
spec.description =
|
14
|
-
|
15
|
-
|
16
|
-
"That is, you transform the object instead of modifying it."
|
14
|
+
'This gem allows you to define "immutable" objects, when using it your objects will only have getters and no setters.' \
|
15
|
+
'So, if you change an attribute of the object, you’ll have a new object instance.'
|
17
16
|
spec.homepage = 'https://github.com/serradura/u-attributes'
|
18
17
|
spec.license = 'MIT'
|
19
18
|
|
20
19
|
# Specify which files should be added to the gem when it is released.
|
21
20
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
21
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|assets)/}) }
|
24
23
|
end
|
25
24
|
spec.bindir = 'exe'
|
26
25
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
@@ -28,7 +27,7 @@ Gem::Specification.new do |spec|
|
|
28
27
|
|
29
28
|
spec.required_ruby_version = '>= 2.2.0'
|
30
29
|
|
31
|
-
spec.add_runtime_dependency 'kind', '>=
|
30
|
+
spec.add_runtime_dependency 'kind', '>= 4.0', '< 6.0'
|
32
31
|
|
33
32
|
spec.add_development_dependency 'bundler'
|
34
33
|
spec.add_development_dependency 'rake', '~> 13.0'
|
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.7.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:
|
11
|
+
date: 2021-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: kind
|
@@ -16,20 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.0'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '6.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
29
|
+
version: '4.0'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '6.0'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: bundler
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,10 +58,9 @@ dependencies:
|
|
58
58
|
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '13.0'
|
61
|
-
description: This gem allows you to define "immutable" objects,
|
62
|
-
|
63
|
-
have a new object instance.
|
64
|
-
it.
|
61
|
+
description: This gem allows you to define "immutable" objects, when using it your
|
62
|
+
objects will only have getters and no setters.So, if you change an attribute of
|
63
|
+
the object, you’ll have a new object instance.
|
65
64
|
email:
|
66
65
|
- rodrigo.serradura@gmail.com
|
67
66
|
executables: []
|
@@ -76,20 +75,21 @@ files:
|
|
76
75
|
- LICENSE.txt
|
77
76
|
- README.md
|
78
77
|
- Rakefile
|
79
|
-
- assets/u-attributes_logo_v1.png
|
80
78
|
- bin/console
|
81
79
|
- bin/setup
|
82
80
|
- lib/micro/attributes.rb
|
83
81
|
- lib/micro/attributes/diff.rb
|
84
82
|
- lib/micro/attributes/features.rb
|
83
|
+
- lib/micro/attributes/features/accept.rb
|
84
|
+
- lib/micro/attributes/features/accept/strict.rb
|
85
85
|
- lib/micro/attributes/features/activemodel_validations.rb
|
86
86
|
- lib/micro/attributes/features/diff.rb
|
87
87
|
- lib/micro/attributes/features/initialize.rb
|
88
88
|
- lib/micro/attributes/features/initialize/strict.rb
|
89
|
+
- lib/micro/attributes/features/keys_as_symbol.rb
|
89
90
|
- lib/micro/attributes/macros.rb
|
90
91
|
- lib/micro/attributes/utils.rb
|
91
92
|
- lib/micro/attributes/version.rb
|
92
|
-
- lib/micro/attributes/with.rb
|
93
93
|
- lib/u-attributes.rb
|
94
94
|
- test.sh
|
95
95
|
- u-attributes.gemspec
|
@@ -112,8 +112,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
112
|
- !ruby/object:Gem::Version
|
113
113
|
version: '0'
|
114
114
|
requirements: []
|
115
|
-
rubygems_version: 3.
|
115
|
+
rubygems_version: 3.1.4
|
116
116
|
signing_key:
|
117
117
|
specification_version: 4
|
118
|
-
summary: Create "immutable" objects
|
118
|
+
summary: Create "immutable" objects with no setters, just getters.
|
119
119
|
test_files: []
|
Binary file
|
@@ -1,100 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'micro/attributes/features/diff'
|
4
|
-
require 'micro/attributes/features/initialize'
|
5
|
-
require 'micro/attributes/features/initialize/strict'
|
6
|
-
require 'micro/attributes/features/activemodel_validations'
|
7
|
-
|
8
|
-
module Micro
|
9
|
-
module Attributes
|
10
|
-
module With
|
11
|
-
#
|
12
|
-
# Features
|
13
|
-
#
|
14
|
-
module Diff
|
15
|
-
def self.included(base)
|
16
|
-
base.send(:include, ::Micro::Attributes)
|
17
|
-
base.send(:include, ::Micro::Attributes::Features::Diff)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
module Initialize
|
22
|
-
def self.included(base)
|
23
|
-
base.send(:include, ::Micro::Attributes)
|
24
|
-
base.send(:include, ::Micro::Attributes::Features::Initialize)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
module ActiveModelValidations
|
29
|
-
def self.included(base)
|
30
|
-
base.send(:include, Initialize)
|
31
|
-
base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
module StrictInitialize
|
36
|
-
def self.included(base)
|
37
|
-
base.send(:include, Initialize)
|
38
|
-
base.send(:include, ::Micro::Attributes::Features::Initialize::Strict)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
#
|
43
|
-
# Combinations
|
44
|
-
#
|
45
|
-
module DiffAndInitialize
|
46
|
-
def self.included(base)
|
47
|
-
base.send(:include, ::Micro::Attributes)
|
48
|
-
base.send(:include, ::Micro::Attributes::Features::Initialize)
|
49
|
-
base.send(:include, ::Micro::Attributes::Features::Diff)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
module DiffAndStrictInitialize
|
54
|
-
def self.included(base)
|
55
|
-
base.send(:include, DiffAndInitialize)
|
56
|
-
base.send(:include, ::Micro::Attributes::Features::Initialize::Strict)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
module ActiveModelValidationsAndDiff
|
61
|
-
def self.included(base)
|
62
|
-
base.send(:include, ::Micro::Attributes)
|
63
|
-
base.send(:include, ::Micro::Attributes::Features::ActiveModelValidations)
|
64
|
-
base.send(:include, ::Micro::Attributes::Features::Diff)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
module ActiveModelValidationsAndInitialize
|
69
|
-
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)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
module ActiveModelValidationsAndStrictInitialize
|
77
|
-
def self.included(base)
|
78
|
-
base.send(:include, ActiveModelValidationsAndInitialize)
|
79
|
-
base.send(:include, ::Micro::Attributes::Features::Initialize::Strict)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
module ActiveModelValidationsAndDiffAndInitialize
|
84
|
-
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)
|
88
|
-
base.send(:include, ::Micro::Attributes::Features::Diff)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
module ActiveModelValidationsAndDiffAndStrictInitialize
|
93
|
-
def self.included(base)
|
94
|
-
base.send(:include, ActiveModelValidationsAndDiffAndInitialize)
|
95
|
-
base.send(:include, ::Micro::Attributes::Features::Initialize::Strict)
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|