eac_ruby_utils 0.79.0 → 0.80.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/eac_ruby_utils/compare_by.rb +28 -0
- data/lib/eac_ruby_utils/enum.rb +102 -0
- data/lib/eac_ruby_utils/patches/module/compare_by.rb +9 -0
- data/lib/eac_ruby_utils/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6933141ed3e3fd7368dadec28e6fbb7dce7510f90e8938f721c8ad19550ca68
|
4
|
+
data.tar.gz: 2f0aae05e9b1ec61dea7b9f0ae4894bcf4f3f6ffef5bf3169fc3397dc99a3dc0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3329528886bd52798bf9773dd13bf76e8fd8cb932fd152755ca5537a5da7b14a92139ddab81ab2e45357e6063608ba9c1eb5e1dd83306f297799755d4e2ad5f
|
7
|
+
data.tar.gz: 419c602edc61ef54f06004298ecae4c8c9c29c3419b96b9a335fd2804e554dc97b170a25c3a66ff4a79c0c433357309d8d958632ecb22f1c8c883ffab1a806f7
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/common_concern/module_setup'
|
4
|
+
|
5
|
+
module EacRubyUtils
|
6
|
+
class CompareBy
|
7
|
+
attr_reader :fields
|
8
|
+
|
9
|
+
def initialize(fields)
|
10
|
+
@fields = fields
|
11
|
+
end
|
12
|
+
|
13
|
+
def apply(klass)
|
14
|
+
pself = self
|
15
|
+
klass.include(::Comparable)
|
16
|
+
%i[<=> eql?].each do |cmp_method|
|
17
|
+
klass.define_method(cmp_method) do |other|
|
18
|
+
pself.object_values(self).send(cmp_method, pself.object_values(other))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
klass.define_method(:hash) { pself.object_values(self).hash }
|
22
|
+
end
|
23
|
+
|
24
|
+
def object_values(object)
|
25
|
+
fields.map { |field| object.send(field) }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/inflector'
|
4
|
+
|
5
|
+
module EacRubyUtils
|
6
|
+
# Similar to Java's enum type (https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html).
|
7
|
+
class Enum
|
8
|
+
class << self
|
9
|
+
def enum(key, *args, &block)
|
10
|
+
value = Value.new(self, key, args, &block)
|
11
|
+
raise ::ArgumentError, "#{self} already has a value with key=#{value.key}" if
|
12
|
+
value_set.include?(value)
|
13
|
+
|
14
|
+
value_set << value.apply
|
15
|
+
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def values
|
20
|
+
value_set.map(&:value)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def value_set
|
26
|
+
@value_set ||= []
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
attr_reader :key
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def initialize(key)
|
35
|
+
@key = key
|
36
|
+
end
|
37
|
+
|
38
|
+
class Value
|
39
|
+
include ::Comparable
|
40
|
+
|
41
|
+
attr_reader :klass, :key, :args, :block
|
42
|
+
|
43
|
+
def initialize(klass, key, args, &block)
|
44
|
+
@klass = klass
|
45
|
+
@key = ::EacRubyUtils::Inflector.variableize(key.to_s).to_sym
|
46
|
+
@args = args
|
47
|
+
@block = block
|
48
|
+
end
|
49
|
+
|
50
|
+
def <=>(other)
|
51
|
+
key <=> other.key
|
52
|
+
end
|
53
|
+
|
54
|
+
def apply
|
55
|
+
define_constant
|
56
|
+
define_class_method
|
57
|
+
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
61
|
+
# @return [String]
|
62
|
+
def class_method_name
|
63
|
+
key.to_s
|
64
|
+
end
|
65
|
+
|
66
|
+
# @return [String]
|
67
|
+
def constant_name
|
68
|
+
class_method_name.upcase
|
69
|
+
end
|
70
|
+
|
71
|
+
def value
|
72
|
+
@value ||= uncached_value
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def define_class_method
|
78
|
+
the_value = self
|
79
|
+
klass.define_singleton_method(class_method_name) { the_value.value }
|
80
|
+
end
|
81
|
+
|
82
|
+
def define_constant
|
83
|
+
klass.const_set(constant_name, value)
|
84
|
+
end
|
85
|
+
|
86
|
+
def uncached_value
|
87
|
+
klass.new(key, *value_args)
|
88
|
+
end
|
89
|
+
|
90
|
+
def value_args
|
91
|
+
r = if block
|
92
|
+
block.call
|
93
|
+
elsif args.count == 1 && args.first.is_a?(::Enumerable)
|
94
|
+
args.first
|
95
|
+
else
|
96
|
+
args
|
97
|
+
end
|
98
|
+
r.is_a?(::Enumerable) ? r : [r]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eac_ruby_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.80.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Esquilo Azul Company
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-11-
|
11
|
+
date: 2021-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -107,10 +107,12 @@ files:
|
|
107
107
|
- lib/eac_ruby_utils/common_constructor/instance_initialize.rb
|
108
108
|
- lib/eac_ruby_utils/common_constructor/super_args.rb
|
109
109
|
- lib/eac_ruby_utils/compact.rb
|
110
|
+
- lib/eac_ruby_utils/compare_by.rb
|
110
111
|
- lib/eac_ruby_utils/context.rb
|
111
112
|
- lib/eac_ruby_utils/contextualizable.rb
|
112
113
|
- lib/eac_ruby_utils/core_ext.rb
|
113
114
|
- lib/eac_ruby_utils/custom_format.rb
|
115
|
+
- lib/eac_ruby_utils/enum.rb
|
114
116
|
- lib/eac_ruby_utils/envs.rb
|
115
117
|
- lib/eac_ruby_utils/envs/base_env.rb
|
116
118
|
- lib/eac_ruby_utils/envs/command.rb
|
@@ -176,6 +178,7 @@ files:
|
|
176
178
|
- lib/eac_ruby_utils/patches/module.rb
|
177
179
|
- lib/eac_ruby_utils/patches/module/abstract_methods.rb
|
178
180
|
- lib/eac_ruby_utils/patches/module/common_concern.rb
|
181
|
+
- lib/eac_ruby_utils/patches/module/compare_by.rb
|
179
182
|
- lib/eac_ruby_utils/patches/module/context.rb
|
180
183
|
- lib/eac_ruby_utils/patches/module/i18n_translate.rb
|
181
184
|
- lib/eac_ruby_utils/patches/module/immutable.rb
|