ree_lib 1.0.71 → 1.0.73
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb +23 -10
- data/lib/ree_lib/packages/ree_dao/package/ree_dao/associations.rb +1 -1
- data/lib/ree_lib/packages/ree_enum/package/ree_enum/base_enum_mapper.rb +47 -0
- data/lib/ree_lib/packages/ree_enum/package/ree_enum/dsl.rb +36 -83
- data/lib/ree_lib/packages/ree_enum/package/ree_enum/enumerable.rb +11 -8
- data/lib/ree_lib/packages/ree_enum/package/ree_enum/integer_value_enum_mapper.rb +42 -0
- data/lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb +37 -0
- data/lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb +6 -10
- data/lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb +16 -4
- data/lib/ree_lib/packages/ree_enum/spec/ree_enum/dsl_spec.rb +48 -6
- data/lib/ree_lib/packages/ree_hash/package/ree_hash/functions/slice.rb +4 -3
- data/lib/ree_lib/packages/ree_i18n/schemas/ree_i18n/functions/set_locale.schema.json +1 -1
- data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/integer.rb +3 -3
- data/lib/ree_lib/packages/ree_object/package/ree_object/functions/to_hash.rb +3 -3
- data/lib/ree_lib/ree.setup.rb +0 -6
- data/lib/ree_lib/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: 8bc7c01750d09b6274eabf44aa42ab5df1638372eed12a81bf2f79d09caf2920
|
4
|
+
data.tar.gz: 8dd947819db285b9ead46579f4eacb3b482a27fa525f48e0f997f21ac230744e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 498b93f53f71563223d74404aa958ea60618c3513c21a9344ae6c18bce4fd6664113eb47aae7f4626577c632417f355a09252411ebe244921c1443f5846c8e3c
|
7
|
+
data.tar.gz: d1c3ac12551051f7f9b5f71e785e4cbe22bc5854863b8e406c6b3f950ed486043c9f9c2fffc88ce2c95df471aab47f3c3401af2b6c353bffb2c22bd24d867619
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ree_lib (1.0.
|
4
|
+
ree_lib (1.0.73)
|
5
5
|
binding_of_caller (~> 1.0.0)
|
6
6
|
i18n (~> 1.12.0)
|
7
7
|
loofah (~> 2.18.0)
|
@@ -51,7 +51,7 @@ GEM
|
|
51
51
|
rack (>= 1.3)
|
52
52
|
rainbow (3.1.1)
|
53
53
|
rake (13.0.6)
|
54
|
-
ree (1.0.
|
54
|
+
ree (1.0.31)
|
55
55
|
commander (~> 4.6.0)
|
56
56
|
rexml (3.2.5)
|
57
57
|
roda (3.58.0)
|
@@ -3,14 +3,12 @@ module ReeDao
|
|
3
3
|
include Ree::LinkDSL
|
4
4
|
include ReeDao::AssociationMethods
|
5
5
|
|
6
|
-
link :demodulize, from: :ree_string
|
7
6
|
link :group_by, from: :ree_array
|
8
7
|
link :index_by, from: :ree_array
|
9
|
-
link :underscore, from: :ree_string
|
10
8
|
|
11
9
|
attr_reader :parent, :parent_dao, :list, :global_opts
|
12
10
|
|
13
|
-
contract(ReeDao::Associations, Sequel::Dataset, Array, Ksplat[RestKeys => Any] => Any)
|
11
|
+
contract(ReeDao::Associations, Nilor[Sequel::Dataset], Array, Ksplat[RestKeys => Any] => Any)
|
14
12
|
def initialize(parent, parent_dao, list, **global_opts)
|
15
13
|
@parent = parent
|
16
14
|
@parent_dao = parent_dao
|
@@ -51,8 +49,7 @@ module ReeDao
|
|
51
49
|
|
52
50
|
dao = if scope.is_a?(Array)
|
53
51
|
return [] if scope.empty?
|
54
|
-
|
55
|
-
find_dao(name, parent, nil)
|
52
|
+
nil
|
56
53
|
else
|
57
54
|
find_dao(assoc_name, parent, scope)
|
58
55
|
end
|
@@ -125,7 +122,7 @@ module ReeDao
|
|
125
122
|
**global_opts
|
126
123
|
)
|
127
124
|
|
128
|
-
if parent_dao.db.in_transaction? || ReeDao::Associations.sync_mode?
|
125
|
+
if parent_dao.nil? || parent_dao.db.in_transaction? || ReeDao::Associations.sync_mode?
|
129
126
|
associations.instance_exec(assoc_list, &block)
|
130
127
|
else
|
131
128
|
threads = associations.instance_exec(assoc_list, &block)
|
@@ -145,7 +142,7 @@ module ReeDao
|
|
145
142
|
end
|
146
143
|
|
147
144
|
contract(
|
148
|
-
Sequel::Dataset,
|
145
|
+
Nilor[Sequel::Dataset],
|
149
146
|
Symbol,
|
150
147
|
Array,
|
151
148
|
Kwargs[
|
@@ -170,7 +167,11 @@ module ReeDao
|
|
170
167
|
if reverse
|
171
168
|
# has_one
|
172
169
|
if !foreign_key
|
173
|
-
|
170
|
+
if parent_dao.nil?
|
171
|
+
raise ArgumentError.new("foreign_key should be provided for :#{assoc_name} association")
|
172
|
+
end
|
173
|
+
|
174
|
+
foreign_key = foreign_key_from_dao(parent_dao)
|
174
175
|
end
|
175
176
|
|
176
177
|
root_ids = list.map(&:id).uniq
|
@@ -206,7 +207,7 @@ module ReeDao
|
|
206
207
|
end
|
207
208
|
|
208
209
|
contract(
|
209
|
-
Sequel::Dataset,
|
210
|
+
Nilor[Sequel::Dataset],
|
210
211
|
Symbol,
|
211
212
|
Array,
|
212
213
|
Kwargs[
|
@@ -228,7 +229,13 @@ module ReeDao
|
|
228
229
|
assoc_dao = nil
|
229
230
|
assoc_dao = find_dao(assoc_name, parent, scope)
|
230
231
|
|
231
|
-
foreign_key
|
232
|
+
if !foreign_key
|
233
|
+
if parent_dao.nil?
|
234
|
+
raise ArgumentError.new("foreign_key should be provided for :#{assoc_name} association")
|
235
|
+
end
|
236
|
+
|
237
|
+
foreign_key = foreign_key_from_dao(parent_dao)
|
238
|
+
end
|
232
239
|
|
233
240
|
root_ids = list.map(&:"#{primary_key}")
|
234
241
|
|
@@ -343,5 +350,11 @@ module ReeDao
|
|
343
350
|
|
344
351
|
parent.agg_caller.send(method, *args, &block)
|
345
352
|
end
|
353
|
+
|
354
|
+
private
|
355
|
+
|
356
|
+
def foreign_key_from_dao(dao)
|
357
|
+
"#{dao.first_source_table.to_s.gsub(/s$/, '')}_id".to_sym
|
358
|
+
end
|
346
359
|
end
|
347
360
|
end
|
@@ -87,7 +87,7 @@ module ReeDao
|
|
87
87
|
Optblock => Any
|
88
88
|
)
|
89
89
|
def association(assoc_type, assoc_name, __opts, &block)
|
90
|
-
if parent_dao.db.in_transaction? || self.class.sync_mode?
|
90
|
+
if parent_dao.nil? || parent_dao.db.in_transaction? || self.class.sync_mode?
|
91
91
|
return if association_is_not_included?(assoc_name) || list.empty?
|
92
92
|
|
93
93
|
association = Association.new(self, parent_dao, list, **global_opts)
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative "value"
|
3
|
+
|
4
|
+
class ReeEnum::BaseEnumMapper < ReeMapper::AbstractType
|
5
|
+
def initialize(enum)
|
6
|
+
@enum = enum
|
7
|
+
end
|
8
|
+
|
9
|
+
contract(
|
10
|
+
ReeEnum::Value,
|
11
|
+
Kwargs[
|
12
|
+
name: String,
|
13
|
+
role: Nilor[Symbol, ArrayOf[Symbol]]
|
14
|
+
] => Or[Integer, String]
|
15
|
+
)
|
16
|
+
def db_dump(value, name:, role: nil)
|
17
|
+
value.mapped_value
|
18
|
+
end
|
19
|
+
|
20
|
+
contract(
|
21
|
+
Or[Integer, String],
|
22
|
+
Kwargs[
|
23
|
+
name: String,
|
24
|
+
role: Nilor[Symbol, ArrayOf[Symbol]]
|
25
|
+
] => ReeEnum::Value
|
26
|
+
).throws(ReeMapper::CoercionError)
|
27
|
+
def db_load(value, name:, role: nil)
|
28
|
+
enum_val = @enum.get_values.by_mapped_value(value)
|
29
|
+
|
30
|
+
if !enum_val
|
31
|
+
raise ReeMapper::CoercionError, "`#{name}` should be one of #{enum_inspection}"
|
32
|
+
end
|
33
|
+
|
34
|
+
enum_val
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def enum_inspection
|
40
|
+
@enum_inspection ||= truncate(@enum.get_values.each.map(&:to_s).inspect)
|
41
|
+
end
|
42
|
+
|
43
|
+
def truncate(str, limit = 180)
|
44
|
+
return str if str.length <= limit
|
45
|
+
"#{str[0..limit]}..."
|
46
|
+
end
|
47
|
+
end
|
@@ -1,5 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
package_require 'ree_swagger/functions/register_type'
|
2
3
|
|
4
|
+
require_relative 'integer_value_enum_mapper'
|
5
|
+
require_relative 'string_value_enum_mapper'
|
6
|
+
|
3
7
|
module ReeEnum
|
4
8
|
module DSL
|
5
9
|
def self.included(base)
|
@@ -27,100 +31,32 @@ module ReeEnum
|
|
27
31
|
end
|
28
32
|
|
29
33
|
def type_for_mapper
|
30
|
-
@type_for_mapper
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
role: Nilor[Symbol, ArrayOf[Symbol]]
|
41
|
-
] => String
|
42
|
-
)
|
43
|
-
def serialize(value, name:, role: nil)
|
44
|
-
value.to_s
|
45
|
-
end
|
46
|
-
|
47
|
-
contract(
|
48
|
-
Any,
|
49
|
-
Kwargs[
|
50
|
-
name: String,
|
51
|
-
role: Nilor[Symbol, ArrayOf[Symbol]]
|
52
|
-
] => ReeEnum::Value
|
53
|
-
).throws(ReeMapper::CoercionError)
|
54
|
-
def cast(value, name:, role: nil)
|
55
|
-
enum_value = if value.is_a?(String)
|
56
|
-
@enum.get_values.by_value(value)
|
57
|
-
elsif value.is_a?(ReeEnum::Value)
|
58
|
-
@enum.get_values.each.find { _1 == value }
|
59
|
-
end
|
60
|
-
|
61
|
-
if enum_value.nil?
|
62
|
-
raise ReeMapper::CoercionError, "`#{name}` should be one of #{enum_inspection}"
|
63
|
-
end
|
64
|
-
|
65
|
-
enum_value
|
66
|
-
end
|
67
|
-
|
68
|
-
contract(
|
69
|
-
ReeEnum::Value,
|
70
|
-
Kwargs[
|
71
|
-
name: String,
|
72
|
-
role: Nilor[Symbol, ArrayOf[Symbol]]
|
73
|
-
] => Or[Integer, String]
|
74
|
-
)
|
75
|
-
def db_dump(value, name:, role: nil)
|
76
|
-
value.mapped_value
|
77
|
-
end
|
78
|
-
|
79
|
-
contract(
|
80
|
-
Or[Integer, String],
|
81
|
-
Kwargs[
|
82
|
-
name: String,
|
83
|
-
role: Nilor[Symbol, ArrayOf[Symbol]]
|
84
|
-
] => ReeEnum::Value
|
85
|
-
).throws(ReeMapper::CoercionError)
|
86
|
-
def db_load(value, name:, role: nil)
|
87
|
-
enum_val = @enum.get_values.by_mapped_value(value)
|
88
|
-
|
89
|
-
if !enum_val
|
90
|
-
raise ReeMapper::CoercionError, "`#{name}` should be one of #{enum_inspection}"
|
91
|
-
end
|
92
|
-
|
93
|
-
enum_val
|
94
|
-
end
|
95
|
-
|
96
|
-
private
|
97
|
-
|
98
|
-
def enum_inspection
|
99
|
-
@enum_inspect ||= truncate(@enum.get_values.each.map(&:to_s).inspect)
|
100
|
-
end
|
101
|
-
|
102
|
-
def truncate(str, limit = 180)
|
103
|
-
return str if str.length <= limit
|
104
|
-
"#{str[0..limit]}..."
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
klass.new(self)
|
34
|
+
return @type_for_mapper if defined? @type_for_mapper
|
35
|
+
|
36
|
+
value_type = get_values.value_type
|
37
|
+
|
38
|
+
klass = if value_type == String
|
39
|
+
StringValueEnumMapper
|
40
|
+
elsif value_type == Integer
|
41
|
+
IntegerValueEnumMapper
|
42
|
+
else
|
43
|
+
raise NotImplementedError, "value_type #{value_type} is not supported"
|
109
44
|
end
|
45
|
+
|
46
|
+
@type_for_mapper = klass.new(self)
|
110
47
|
end
|
111
48
|
|
112
49
|
def register_as_swagger_type
|
113
50
|
swagger_type_registrator = ReeSwagger::RegisterType.new
|
114
51
|
|
52
|
+
definition = swagger_definition
|
53
|
+
|
115
54
|
[:casters, :serializers].each do |kind|
|
116
55
|
swagger_type_registrator.call(
|
117
56
|
kind,
|
118
57
|
type_for_mapper.class,
|
119
58
|
->(*) {
|
120
|
-
|
121
|
-
type: 'string',
|
122
|
-
enum: get_values.each.map(&:to_s)
|
123
|
-
}
|
59
|
+
definition
|
124
60
|
}
|
125
61
|
)
|
126
62
|
end
|
@@ -137,6 +73,23 @@ module ReeEnum
|
|
137
73
|
self.get_enum_name, type_for_mapper
|
138
74
|
)
|
139
75
|
end
|
76
|
+
|
77
|
+
def swagger_definition
|
78
|
+
value_type = get_values.value_type
|
79
|
+
|
80
|
+
type = if value_type == String
|
81
|
+
"string"
|
82
|
+
elsif value_type == Integer
|
83
|
+
"integer"
|
84
|
+
else
|
85
|
+
raise NotImplementedError, "value_type #{value_type} is not supported"
|
86
|
+
end
|
87
|
+
|
88
|
+
{
|
89
|
+
type: type,
|
90
|
+
enum: get_values.each.map(&:value)
|
91
|
+
}
|
92
|
+
end
|
140
93
|
end
|
141
94
|
end
|
142
95
|
end
|
@@ -29,8 +29,9 @@ module ReeEnum::Enumerable
|
|
29
29
|
@values&.enum_name
|
30
30
|
end
|
31
31
|
|
32
|
-
def val(value, mapped_value =
|
33
|
-
value = value.to_s
|
32
|
+
def val(value, mapped_value = nil, method: value.to_s.to_sym)
|
33
|
+
value = value.to_s if value.is_a?(Symbol)
|
34
|
+
mapped_value ||= value
|
34
35
|
|
35
36
|
if RESTRICTED_METHODS.include?(method)
|
36
37
|
raise ArgumentError.new("#{method.inspect} is not allowed as enum method")
|
@@ -38,13 +39,15 @@ module ReeEnum::Enumerable
|
|
38
39
|
|
39
40
|
enum_value = @values.add(value, mapped_value, method)
|
40
41
|
|
41
|
-
|
42
|
-
|
43
|
-
|
42
|
+
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
43
|
+
def #{method}
|
44
|
+
get_values.by_value(#{value.inspect}.freeze)
|
45
|
+
end
|
44
46
|
|
45
|
-
|
46
|
-
|
47
|
-
|
47
|
+
def self.#{method}
|
48
|
+
get_values.by_value(#{value.inspect}.freeze)
|
49
|
+
end
|
50
|
+
RUBY
|
48
51
|
|
49
52
|
enum_value
|
50
53
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative "base_enum_mapper"
|
3
|
+
|
4
|
+
class ReeEnum::IntegerValueEnumMapper < ReeEnum::BaseEnumMapper
|
5
|
+
contract(
|
6
|
+
ReeEnum::Value,
|
7
|
+
Kwargs[
|
8
|
+
name: String,
|
9
|
+
role: Nilor[Symbol, ArrayOf[Symbol]]
|
10
|
+
] => Integer
|
11
|
+
)
|
12
|
+
def serialize(value, name:, role: nil)
|
13
|
+
value.value
|
14
|
+
end
|
15
|
+
|
16
|
+
contract(
|
17
|
+
Any,
|
18
|
+
Kwargs[
|
19
|
+
name: String,
|
20
|
+
role: Nilor[Symbol, ArrayOf[Symbol]]
|
21
|
+
] => ReeEnum::Value
|
22
|
+
).throws(ReeMapper::CoercionError)
|
23
|
+
def cast(value, name:, role: nil)
|
24
|
+
enum_value = case value
|
25
|
+
when Integer
|
26
|
+
@enum.get_values.by_value(value)
|
27
|
+
when String
|
28
|
+
value = Integer(value, exception: false)
|
29
|
+
if !value.nil?
|
30
|
+
@enum.get_values.by_value(value)
|
31
|
+
end
|
32
|
+
when ReeEnum::Value
|
33
|
+
@enum.get_values.each.find { _1 == value }
|
34
|
+
end
|
35
|
+
|
36
|
+
if enum_value.nil?
|
37
|
+
raise ReeMapper::CoercionError, "`#{name}` should be one of #{enum_inspection}"
|
38
|
+
end
|
39
|
+
|
40
|
+
enum_value
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative "base_enum_mapper"
|
3
|
+
|
4
|
+
class ReeEnum::StringValueEnumMapper < ReeEnum::BaseEnumMapper
|
5
|
+
contract(
|
6
|
+
ReeEnum::Value,
|
7
|
+
Kwargs[
|
8
|
+
name: String,
|
9
|
+
role: Nilor[Symbol, ArrayOf[Symbol]]
|
10
|
+
] => String
|
11
|
+
)
|
12
|
+
def serialize(value, name:, role: nil)
|
13
|
+
value.value
|
14
|
+
end
|
15
|
+
|
16
|
+
contract(
|
17
|
+
Any,
|
18
|
+
Kwargs[
|
19
|
+
name: String,
|
20
|
+
role: Nilor[Symbol, ArrayOf[Symbol]]
|
21
|
+
] => ReeEnum::Value
|
22
|
+
).throws(ReeMapper::CoercionError)
|
23
|
+
def cast(value, name:, role: nil)
|
24
|
+
enum_value = case value
|
25
|
+
when String
|
26
|
+
@enum.get_values.by_value(value)
|
27
|
+
when ReeEnum::Value
|
28
|
+
@enum.get_values.each.find { _1 == value }
|
29
|
+
end
|
30
|
+
|
31
|
+
if enum_value.nil?
|
32
|
+
raise ReeMapper::CoercionError, "`#{name}` should be one of #{enum_inspection}"
|
33
|
+
end
|
34
|
+
|
35
|
+
enum_value
|
36
|
+
end
|
37
|
+
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
class ReeEnum::Value
|
4
4
|
attr_reader :enum_class, :enum_name, :value, :method, :mapped_value
|
5
5
|
|
6
|
-
contract(Class, Symbol, String, Or[Integer, String], Symbol => Any)
|
6
|
+
contract(Class, Symbol, Or[String, Integer], Or[Integer, String], Symbol => Any)
|
7
7
|
def initialize(enum_class, enum_name, value, mapped_value, method)
|
8
8
|
@enum_class = enum_class
|
9
9
|
@enum_name = enum_name
|
@@ -13,25 +13,21 @@ class ReeEnum::Value
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def to_s
|
16
|
-
value
|
16
|
+
value.to_s
|
17
17
|
end
|
18
18
|
|
19
19
|
def as_json(*args)
|
20
|
-
|
20
|
+
value
|
21
21
|
end
|
22
22
|
|
23
23
|
contract(Or[ReeEnum::Value, String, Symbol, Integer, Any] => Bool)
|
24
24
|
def ==(compare)
|
25
25
|
if compare.is_a?(self.class)
|
26
26
|
value == compare.value
|
27
|
-
elsif compare.is_a?(Symbol)
|
28
|
-
value == compare
|
29
|
-
elsif compare.is_a?(String)
|
30
|
-
value == compare || mapped_value == compare
|
31
|
-
elsif compare.is_a?(Integer)
|
32
|
-
mapped_value == compare
|
27
|
+
elsif compare.is_a?(Symbol) && value.is_a?(String)
|
28
|
+
value.to_sym == compare
|
33
29
|
else
|
34
|
-
|
30
|
+
value == compare || mapped_value == compare
|
35
31
|
end
|
36
32
|
end
|
37
33
|
|
@@ -11,6 +11,11 @@ class ReeEnum::Values
|
|
11
11
|
@collection_by_mapped_value = {}
|
12
12
|
end
|
13
13
|
|
14
|
+
def value_type
|
15
|
+
raise ArgumentError, "value_type is not defined" unless defined?(@value_type)
|
16
|
+
@value_type
|
17
|
+
end
|
18
|
+
|
14
19
|
def to_a
|
15
20
|
@collection
|
16
21
|
end
|
@@ -19,12 +24,13 @@ class ReeEnum::Values
|
|
19
24
|
@collection.each(&)
|
20
25
|
end
|
21
26
|
|
22
|
-
contract(Or[Symbol, String] => Nilor[ReeEnum::Value])
|
27
|
+
contract(Or[Symbol, String, Integer] => Nilor[ReeEnum::Value])
|
23
28
|
def by_value(value)
|
24
|
-
|
29
|
+
value = value.to_s if value.is_a?(Symbol)
|
30
|
+
@collection_by_value[value]
|
25
31
|
end
|
26
32
|
|
27
|
-
contract(Or[Symbol, String] => ReeEnum::Value).throws(ArgumentError)
|
33
|
+
contract(Or[Symbol, String, Integer] => ReeEnum::Value).throws(ArgumentError)
|
28
34
|
def by_value!(value)
|
29
35
|
by_value(value) ||
|
30
36
|
(raise ArgumentError.new("constant for value #{value.inspect} is not found in #{self.inspect}"))
|
@@ -45,8 +51,14 @@ class ReeEnum::Values
|
|
45
51
|
@collection.map(&:inspect).inspect
|
46
52
|
end
|
47
53
|
|
48
|
-
contract(String, Or[Integer, String], Symbol => ReeEnum::Value)
|
54
|
+
contract(Or[String, Integer], Or[Integer, String], Symbol => ReeEnum::Value)
|
49
55
|
def add(value, mapped_value, method)
|
56
|
+
if @value_type.nil?
|
57
|
+
@value_type = value.class
|
58
|
+
elsif @value_type != value.class
|
59
|
+
raise ArgumentError, "#{@klass}: value types should be the same for all enum values"
|
60
|
+
end
|
61
|
+
|
50
62
|
if @collection.any? { _1.method == method }
|
51
63
|
raise ArgumentError, "#{@klass}: method #{method.inspect} was already added"
|
52
64
|
end
|
@@ -37,6 +37,17 @@ RSpec.describe ReeEnum::DSL do
|
|
37
37
|
register_as_mapper_type
|
38
38
|
end
|
39
39
|
|
40
|
+
class Numbers
|
41
|
+
include ReeEnum::DSL
|
42
|
+
|
43
|
+
enum :numbers
|
44
|
+
|
45
|
+
val 0, method: :zero
|
46
|
+
val 1, method: :one
|
47
|
+
|
48
|
+
register_as_mapper_type
|
49
|
+
end
|
50
|
+
|
40
51
|
class Reflexives
|
41
52
|
include ReeEnum::DSL
|
42
53
|
|
@@ -52,19 +63,22 @@ RSpec.describe ReeEnum::DSL do
|
|
52
63
|
mapper :test_mapper do
|
53
64
|
link :states
|
54
65
|
link :types
|
66
|
+
link :numbers
|
55
67
|
end
|
56
68
|
|
57
69
|
class Dto
|
58
|
-
attr_reader :type, :state
|
59
|
-
def initialize(type, state)
|
70
|
+
attr_reader :type, :state, :number
|
71
|
+
def initialize(type, state, number)
|
60
72
|
@type = type
|
61
73
|
@state = state
|
74
|
+
@number = number
|
62
75
|
end
|
63
76
|
end
|
64
77
|
|
65
78
|
build_mapper.use(:serialize).use(:cast).use(:db_dump).use(:db_load, dto: Dto) do
|
66
79
|
types :type
|
67
80
|
states :state
|
81
|
+
numbers :number
|
68
82
|
end
|
69
83
|
end
|
70
84
|
|
@@ -86,6 +100,8 @@ RSpec.describe ReeEnum::DSL do
|
|
86
100
|
expect(o.second).to eq(1)
|
87
101
|
expect(o.get_values.by_value(:first)).to eq(o.first)
|
88
102
|
expect(o.get_values.by_value(:second)).to eq(o.second)
|
103
|
+
expect(o.get_values.by_value("first")).to eq(o.first)
|
104
|
+
expect(o.get_values.by_value("second")).to eq(o.second)
|
89
105
|
expect(o.get_values.by_mapped_value(0)).to eq(o.first)
|
90
106
|
expect(o.get_values.by_mapped_value(1)).to eq(o.second)
|
91
107
|
expect(o.get_values.to_a).to eq([o.first, o.second])
|
@@ -108,11 +124,13 @@ RSpec.describe ReeEnum::DSL do
|
|
108
124
|
mapper.serialize({
|
109
125
|
state: TestReeEnum::States.first,
|
110
126
|
type: TestReeEnum::Types.account,
|
127
|
+
number: TestReeEnum::Numbers.zero,
|
111
128
|
})
|
112
129
|
).to eq(
|
113
130
|
{
|
114
131
|
state: 'first',
|
115
|
-
type: 'account'
|
132
|
+
type: 'account',
|
133
|
+
number: 0,
|
116
134
|
}
|
117
135
|
)
|
118
136
|
|
@@ -120,6 +138,7 @@ RSpec.describe ReeEnum::DSL do
|
|
120
138
|
mapper.cast({
|
121
139
|
state: 'first',
|
122
140
|
type: 'invalid',
|
141
|
+
number: 0,
|
123
142
|
})
|
124
143
|
}.to raise_error(ReeMapper::CoercionError, '`type` should be one of ["account"]')
|
125
144
|
|
@@ -127,11 +146,13 @@ RSpec.describe ReeEnum::DSL do
|
|
127
146
|
mapper.cast({
|
128
147
|
state: 'first',
|
129
148
|
type: 'account',
|
149
|
+
number: 0,
|
130
150
|
})
|
131
151
|
).to eq(
|
132
152
|
{
|
133
153
|
state: TestReeEnum::States.first,
|
134
|
-
type: TestReeEnum::Types.account
|
154
|
+
type: TestReeEnum::Types.account,
|
155
|
+
number: TestReeEnum::Numbers.zero,
|
135
156
|
}
|
136
157
|
)
|
137
158
|
|
@@ -139,11 +160,27 @@ RSpec.describe ReeEnum::DSL do
|
|
139
160
|
mapper.cast({
|
140
161
|
state: TestReeEnum::States.first,
|
141
162
|
type: TestReeEnum::Types.account,
|
163
|
+
number: TestReeEnum::Numbers.zero,
|
142
164
|
})
|
143
165
|
).to eq(
|
144
166
|
{
|
145
167
|
state: TestReeEnum::States.first,
|
146
|
-
type: TestReeEnum::Types.account
|
168
|
+
type: TestReeEnum::Types.account,
|
169
|
+
number: TestReeEnum::Numbers.zero,
|
170
|
+
}
|
171
|
+
)
|
172
|
+
|
173
|
+
expect(
|
174
|
+
mapper.cast({
|
175
|
+
state: TestReeEnum::States.first,
|
176
|
+
type: TestReeEnum::Types.account,
|
177
|
+
number: TestReeEnum::Numbers.zero,
|
178
|
+
})
|
179
|
+
).to eq(
|
180
|
+
{
|
181
|
+
state: "first",
|
182
|
+
type: "account",
|
183
|
+
number: 0,
|
147
184
|
}
|
148
185
|
)
|
149
186
|
|
@@ -151,23 +188,28 @@ RSpec.describe ReeEnum::DSL do
|
|
151
188
|
mapper.db_dump({
|
152
189
|
state: TestReeEnum::States.first,
|
153
190
|
type: TestReeEnum::Types.account,
|
191
|
+
number: TestReeEnum::Numbers.zero,
|
154
192
|
})
|
155
193
|
).to eq(
|
156
194
|
{
|
157
195
|
state: 0,
|
158
|
-
type: "account"
|
196
|
+
type: "account",
|
197
|
+
number: 0,
|
159
198
|
}
|
160
199
|
)
|
161
200
|
|
162
201
|
dto = mapper.db_load({
|
163
202
|
state: 0,
|
164
203
|
type: "account",
|
204
|
+
number: 0,
|
165
205
|
})
|
166
206
|
|
167
207
|
expect(dto.state).to eq(TestReeEnum::States.first)
|
168
208
|
expect(dto.state).to be_a(ReeEnum::Value)
|
169
209
|
expect(dto.type).to eq(TestReeEnum::Types.account)
|
170
210
|
expect(dto.type).to be_a(ReeEnum::Value)
|
211
|
+
expect(dto.number).to eq(TestReeEnum::Numbers.zero)
|
212
|
+
expect(dto.number).to be_a(ReeEnum::Value)
|
171
213
|
|
172
214
|
expect(TestReeEnum::Reflexives.myself).to eq(:self)
|
173
215
|
}
|
@@ -6,14 +6,15 @@ class ReeHash::Slice
|
|
6
6
|
fn :slice do
|
7
7
|
link :build_filter_keys
|
8
8
|
link 'ree_hash/contracts/hash_keys_contract', -> { HashKeysContract }
|
9
|
-
def_error { MissingKeyErr }
|
10
|
-
def_error { InvalidFilterKey }
|
11
9
|
end
|
12
10
|
|
11
|
+
MissingKeyErr = Class.new(ArgumentError)
|
12
|
+
InvalidFilterKey = Class.new(ArgumentError)
|
13
|
+
|
13
14
|
doc(<<~DOC)
|
14
15
|
Replaces the hash with only the given keys.
|
15
16
|
Returns a hash containing the removed key/value pairs.
|
16
|
-
|
17
|
+
|
17
18
|
hash = { a: 1, b: {e: 2, f: 1}, c: 3, d: 4 }
|
18
19
|
slice(hash, [:a, :b]) # => {a: 1, b: 2}
|
19
20
|
slice(hash, [:a, b: [:e]]) # => {a: 1, b: {e: 2}}
|
@@ -15,11 +15,11 @@ class ReeMapper::Integer < ReeMapper::AbstractType
|
|
15
15
|
if value.is_a?(Integer)
|
16
16
|
value
|
17
17
|
elsif value.is_a?(String)
|
18
|
-
|
19
|
-
|
20
|
-
rescue ArgumentError => e
|
18
|
+
value = Integer(value, exception: false)
|
19
|
+
if value.nil?
|
21
20
|
raise ReeMapper::CoercionError, "`#{name}` is invalid integer"
|
22
21
|
end
|
22
|
+
value
|
23
23
|
else
|
24
24
|
raise ReeMapper::TypeError, "`#{name}` should be an integer"
|
25
25
|
end
|
@@ -3,9 +3,9 @@
|
|
3
3
|
class ReeObject::ToHash
|
4
4
|
include Ree::FnDSL
|
5
5
|
|
6
|
-
fn :to_hash
|
7
|
-
|
8
|
-
|
6
|
+
fn :to_hash
|
7
|
+
|
8
|
+
RecursiveObjectErr = Class.new(ArgumentError)
|
9
9
|
|
10
10
|
BASIC_TYPES = [
|
11
11
|
Date, Time, Numeric, String, FalseClass, TrueClass, NilClass, Symbol,
|
data/lib/ree_lib/ree.setup.rb
CHANGED
data/lib/ree_lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ree_lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.73
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruslan Gatiyatov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-08-
|
11
|
+
date: 2023-08-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ree
|
@@ -809,9 +809,12 @@ files:
|
|
809
809
|
- lib/ree_lib/packages/ree_enum/Package.schema.json
|
810
810
|
- lib/ree_lib/packages/ree_enum/bin/console
|
811
811
|
- lib/ree_lib/packages/ree_enum/package/ree_enum.rb
|
812
|
+
- lib/ree_lib/packages/ree_enum/package/ree_enum/base_enum_mapper.rb
|
812
813
|
- lib/ree_lib/packages/ree_enum/package/ree_enum/contractable.rb
|
813
814
|
- lib/ree_lib/packages/ree_enum/package/ree_enum/dsl.rb
|
814
815
|
- lib/ree_lib/packages/ree_enum/package/ree_enum/enumerable.rb
|
816
|
+
- lib/ree_lib/packages/ree_enum/package/ree_enum/integer_value_enum_mapper.rb
|
817
|
+
- lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
|
815
818
|
- lib/ree_lib/packages/ree_enum/package/ree_enum/value.rb
|
816
819
|
- lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb
|
817
820
|
- lib/ree_lib/packages/ree_enum/spec/package_schema_spec.rb
|