ree_lib 1.0.30 → 1.0.31

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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/lib/ree_lib/packages/ree_dao/package/ree_dao/functions/one_to_many.rb +2 -5
  4. data/lib/ree_lib/packages/ree_dao/package/ree_dao/functions/one_to_one.rb +2 -5
  5. data/lib/ree_lib/packages/ree_dao/package/ree_dao/functions/persist_assoc.rb +1 -4
  6. data/lib/ree_lib/packages/ree_dto/package/ree_dto/entity_dsl.rb +3 -1
  7. data/lib/ree_lib/packages/ree_dto/spec/ree_dto/entity_dsl_spec.rb +2 -0
  8. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/default_factory.rb +4 -4
  9. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/functions/build_mapper_strategy.rb +4 -12
  10. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper.rb +15 -1
  11. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory.rb +9 -21
  12. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb +2 -1
  13. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_strategy.rb +30 -15
  14. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/strategy_outputs/{symbol_key_hash_output.rb → hash_output.rb} +4 -4
  15. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/strategy_outputs/object_output.rb +2 -7
  16. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/strategy_outputs/strategy_output.rb +9 -12
  17. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/strategy_outputs/struct_output.rb +28 -0
  18. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper.rb +6 -6
  19. data/lib/ree_lib/packages/ree_mapper/schemas/ree_mapper/functions/build_mapper_strategy.schema.json +3 -3
  20. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/functions/build_mapper_factory_spec.rb +1 -1
  21. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/functions/build_mapper_strategy_spec.rb +1 -1
  22. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/mapper_factory_spec.rb +8 -1
  23. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/mapper_spec.rb +76 -11
  24. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/array_spec.rb +8 -8
  25. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/bool_spec.rb +4 -4
  26. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/date_spec.rb +4 -4
  27. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/date_time_spec.rb +4 -4
  28. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/float_spec.rb +4 -4
  29. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/hash_spec.rb +5 -27
  30. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/integer_spec.rb +4 -4
  31. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/string_spec.rb +4 -4
  32. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/time_spec.rb +4 -4
  33. data/lib/ree_lib/packages/ree_mapper/spec/ree_mapper/types/type_options_spec.rb +4 -4
  34. data/lib/ree_lib/packages/ree_swagger/spec/functions/build_endpoint_schema_spec.rb +2 -2
  35. data/lib/ree_lib/packages/ree_swagger/spec/functions/build_parameters_spec.rb +2 -2
  36. data/lib/ree_lib/packages/ree_swagger/spec/functions/build_request_body_spec.rb +2 -2
  37. data/lib/ree_lib/packages/ree_swagger/spec/functions/build_serializer_schema_spec.rb +2 -2
  38. data/lib/ree_lib/packages/ree_swagger/spec/functions/register_type_spec.rb +3 -3
  39. data/lib/ree_lib/version.rb +1 -1
  40. metadata +4 -4
  41. data/lib/ree_lib/packages/ree_mapper/package/ree_mapper/strategy_outputs/string_key_hash_output.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0384a60b8e6da77137b281c223ae80df078ad10389876fb719bd6eb7264f305c'
4
- data.tar.gz: 3771e23d3f69dbb9ba8ff56375b35ee039b92c2132e0398912e32677754f444b
3
+ metadata.gz: 5a28b9fc25900e54ca38f0a0a4fc47c8a46ceea9543d9a0cc2b95d72ff17c9cd
4
+ data.tar.gz: 19ace80db90cabee5a3909c9322c725c8c43b8a9766e2718ebd9b666bc929e56
5
5
  SHA512:
6
- metadata.gz: b0e8d42f124e6252ce81fc64a7980c22c85e252ebdcedb5130fcaa53fda721d80c87804e33926251e5da9b753389593252309e5c09a9df4a769399084b0c158d
7
- data.tar.gz: a8c6acc47b934e848ef7a98ad579b114cd8988bb78872ef5f2c616638e1e6cbe2d64a5618d6efd8fd5dd1ee1000397bab178fe16948617dff574cfa594eb3cf8
6
+ metadata.gz: 71287702a0bab2686193543bb28bd6eb322789293b3bec1605419e9bbe0dbecee651e5b0ceb7ce9311eff71a7f10513c47dd44811cfe65b48748ee42a4192d63
7
+ data.tar.gz: dcc67851e4a71dea3ee27c057d725861a2e75253d94cad46c137eb6307acadc0d4792542926145c3023e7241ee6557cfe7cf5a9392ab18ad5d6960c0b58f9707
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ree_lib (1.0.30)
4
+ ree_lib (1.0.31)
5
5
  binding_of_caller (~> 1.0.0)
6
6
  i18n (~> 1.12.0)
7
7
  loofah (~> 2.18.0)
@@ -3,8 +3,8 @@ class ReeDao::OneToMany
3
3
 
4
4
  fn :one_to_many do
5
5
  link :demodulize, from: :ree_string
6
- link :underscore, from: :ree_string
7
6
  link :group_by, from: :ree_array
7
+ link :underscore, from: :ree_string
8
8
  end
9
9
 
10
10
  contract(
@@ -24,10 +24,7 @@ class ReeDao::OneToMany
24
24
  else
25
25
  dto_class = assoc_dao
26
26
  .opts[:schema_mapper]
27
- .strategies
28
- .detect {_1 .method == :db_load }
29
- .output
30
- .dto
27
+ .dto(:db_load)
31
28
 
32
29
  name = underscore(demodulize(dto_class.name))
33
30
  "set_#{name}s".to_sym
@@ -3,8 +3,8 @@ class ReeDao::OneToOne
3
3
 
4
4
  fn :one_to_one do
5
5
  link :demodulize, from: :ree_string
6
- link :underscore, from: :ree_string
7
6
  link :index_by, from: :ree_array
7
+ link :underscore, from: :ree_string
8
8
  end
9
9
 
10
10
  contract(
@@ -21,10 +21,7 @@ class ReeDao::OneToOne
21
21
 
22
22
  dto_class = assoc_dao
23
23
  .opts[:schema_mapper]
24
- .strategies
25
- .detect {_1 .method == :db_load }
26
- .output
27
- .dto
24
+ .dto(:db_load)
28
25
 
29
26
  assoc_name = underscore(demodulize(dto_class.name))
30
27
  reverse = opts[:reverse]
@@ -27,10 +27,7 @@ class ReeDao::PersistAssoc
27
27
  assoc_name = if opts[:child_assoc].nil?
28
28
  dto_class = assoc_dao
29
29
  .opts[:schema_mapper]
30
- .strategies
31
- .detect {_1 .method == :db_load }
32
- .output
33
- .dto
30
+ .dto(:db_load)
34
31
 
35
32
  name = underscore(demodulize(dto_class.name))
36
33
  "#{name}s"
@@ -63,8 +63,10 @@ module ReeDto::EntityDSL
63
63
  }
64
64
  end
65
65
 
66
- contract self => Bool
66
+ contract Any => Bool
67
67
  def ==(val)
68
+ return false unless val.is_a?(self.class)
69
+
68
70
  #{
69
71
  args.map {|k, _|
70
72
  "@#{k} == val.#{k}"
@@ -38,6 +38,8 @@ RSpec.describe ReeDto::EntityDSL do
38
38
  it { expect(@test_dto.email).to eq('test@example.com') }
39
39
  it { expect(@test_dto).to eq(@compare_dto) }
40
40
  it { expect(@test_dto).to_not eq(@uncompareable_dto)}
41
+ it { expect(@test_dto == Object.new).to eq(false) }
42
+ it { expect(@test_dto == @test_dto).to eq(true) }
41
43
 
42
44
  context "missing args" do
43
45
  it {
@@ -10,10 +10,10 @@ class ReeMapper::DefaultFactory
10
10
 
11
11
  def build
12
12
  build_mapper_factory(strategies: [
13
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
14
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
15
- build_mapper_strategy(method: :db_dump, output: :symbol_key_hash),
16
- build_mapper_strategy(method: :db_load, output: :object, always_optional: true)
13
+ build_mapper_strategy(method: :cast),
14
+ build_mapper_strategy(method: :serialize),
15
+ build_mapper_strategy(method: :db_dump),
16
+ build_mapper_strategy(method: :db_load, dto: Object, always_optional: true)
17
17
  ])
18
18
  end
19
19
  end
@@ -5,23 +5,15 @@ class ReeMapper::BuildMapperStrategy
5
5
 
6
6
  fn :build_mapper_strategy
7
7
 
8
- OUTPUT_MAP = {
9
- string_key_hash: ReeMapper::StringKeyHashOutput,
10
- symbol_key_hash: ReeMapper::SymbolKeyHashOutput,
11
- object: ReeMapper::ObjectOutput
12
- }.freeze
13
-
14
8
  contract(Kwargs[
15
9
  method: Symbol,
16
- output: Symbol,
10
+ dto: Class,
17
11
  always_optional: Bool
18
- ] => ReeMapper::MapperStrategy).throws(ArgumentError)
19
- def call(method:, output:, always_optional: false)
20
- raise ArgumentError, 'invalid output' unless OUTPUT_MAP.key?(output)
21
-
12
+ ] => ReeMapper::MapperStrategy)
13
+ def call(method:, dto: Hash, always_optional: false)
22
14
  ReeMapper::MapperStrategy.new(
23
15
  method: method,
24
- output: OUTPUT_MAP.fetch(output).new,
16
+ dto: dto,
25
17
  always_optional: always_optional
26
18
  )
27
19
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ReeMapper::Mapper
4
- contract(ArrayOf[ReeMapper::MapperStrategy], Nilor[ReeMapper::AbstractType] => Any)
4
+ contract(ArrayOf[ReeMapper::MapperStrategy], Nilor[ReeMapper::AbstractType] => Any).throws(ReeMapper::UnsupportedTypeError)
5
5
  def self.build(strategies, type = nil)
6
6
  if type
7
7
  strategies.each do |strategy|
@@ -110,4 +110,18 @@ class ReeMapper::Mapper
110
110
  def name=(name)
111
111
  @name = name
112
112
  end
113
+
114
+ contract(Symbol => Class).throws(ArgumentError)
115
+ def dto(strategy_method)
116
+ strategy = strategies.detect { _1.method == strategy_method }
117
+ raise ArgumentError, "there is no :#{strategy_method} strategy" unless strategy
118
+ strategy.dto
119
+ end
120
+
121
+ contract(None => nil).throws(ReeMapper::ArgumentError)
122
+ def prepare_dto
123
+ raise ReeMapper::ArgumentError, "mapper should contain at least one field" if fields.empty?
124
+ strategies.each { _1.prepare_dto(fields.keys) }
125
+ nil
126
+ end
113
127
  end
@@ -5,12 +5,6 @@ class ReeMapper::MapperFactory
5
5
  attr_reader :types, :strategies
6
6
  end
7
7
 
8
- HASH_KEY_OPTION_VALUES = [:symbol, :string, nil].freeze
9
- HASH_KEY_OPTION_MAP = {
10
- symbol: ReeMapper::SymbolKeyHashOutput,
11
- string: ReeMapper::StringKeyHashOutput
12
- }.freeze
13
-
14
8
  contract(Symbol, Any => Class).throws(ArgumentError)
15
9
  def self.register_type(name, object_type)
16
10
  register(
@@ -70,17 +64,17 @@ class ReeMapper::MapperFactory
70
64
  @mapper = mapper
71
65
  end
72
66
 
73
- contract(Nilor[Symbol], Kwargs[each: Nilor[ReeMapper::Field], optional: Bool, key: Nilor[Symbol]], Ksplat[RestKeys => Any], Optblock => Nilor[ReeMapper::Field])
74
- def array(field_name = nil, each: nil, optional: false, key: nil, **opts, &blk)
67
+ contract(Nilor[Symbol], Kwargs[each: Nilor[ReeMapper::Field], optional: Bool, dto: Nilor[Class]], Ksplat[RestKeys => Any], Optblock => Nilor[ReeMapper::Field])
68
+ def array(field_name = nil, each: nil, optional: false, dto: nil, **opts, &blk)
75
69
  raise ReeMapper::Error, "invalid DSL usage" unless @mapper
76
70
  raise ArgumentError, "array item can't be optional" if field_name.nil? && optional
77
71
  raise ArgumentError, 'array type should use either :each or :block' if each && blk || !each && !blk
78
- raise ArgumentError, 'invalid :key option value' unless HASH_KEY_OPTION_VALUES.include?(key)
72
+ raise ArgumentError, 'array does not permit :dto without :block' if dto && !blk
79
73
  raise ArgumentError, 'array does not permit :only and :except keys' if opts.key?(:only) || opts.key?(:except)
80
74
 
81
75
  if blk
82
76
  each = ReeMapper::Field.new(
83
- hash_from_blk(key: key, &blk)
77
+ hash_from_blk(dto: dto, &blk)
84
78
  )
85
79
  end
86
80
 
@@ -98,12 +92,11 @@ class ReeMapper::MapperFactory
98
92
  array(field_name, each: each, optional: true, **opts, &blk)
99
93
  end
100
94
 
101
- contract(Symbol, Kwargs[key: Nilor[Symbol]], Ksplat[RestKeys => Any], Block => nil)
102
- def hash(field_name, key: nil, **opts, &blk)
95
+ contract(Symbol, Kwargs[dto: Nilor[Class]], Ksplat[RestKeys => Any], Block => nil)
96
+ def hash(field_name, dto: nil, **opts, &blk)
103
97
  raise ReeMapper::Error, "invalid DSL usage" unless @mapper
104
- raise ArgumentError, 'invalid :key option value' unless HASH_KEY_OPTION_VALUES.include?(key)
105
98
 
106
- type = hash_from_blk(key: key, &blk)
99
+ type = hash_from_blk(dto: dto, &blk)
107
100
 
108
101
  @mapper.add_field(type, field_name, **opts)
109
102
  end
@@ -115,17 +108,12 @@ class ReeMapper::MapperFactory
115
108
 
116
109
  private
117
110
 
118
- def hash_from_blk(key:, &blk)
111
+ def hash_from_blk(dto:, &blk)
119
112
  mapper_proxy = self.class.call
120
113
 
121
114
  strategies = @mapper.strategies.map do |strategy|
122
115
  strategy = strategy.dup
123
- output = strategy.output
124
- if key
125
- strategy.output = HASH_KEY_OPTION_MAP.fetch(key).new
126
- elsif !(output.is_a?(ReeMapper::SymbolKeyHashOutput) || output.is_a?(ReeMapper::StringKeyHashOutput))
127
- strategy.output = ReeMapper::SymbolKeyHashOutput.new
128
- end
116
+ strategy.dto = dto if dto
129
117
  strategy
130
118
  end
131
119
 
@@ -24,7 +24,7 @@ class ReeMapper::MapperFactoryProxy
24
24
  else
25
25
  strategy = mapper_factory.strategies.detect { _1.method == strategy_or_method }
26
26
  raise ArgumentError, "MapperFactory strategy :#{strategy_or_method} not found" unless strategy
27
- strategy = Marshal.load(Marshal.dump(strategy))
27
+ strategy = strategy.dup
28
28
  strategy.dto = dto if dto
29
29
  end
30
30
 
@@ -34,6 +34,7 @@ class ReeMapper::MapperFactoryProxy
34
34
 
35
35
  mapper = ReeMapper::Mapper.build(strategies)
36
36
  mapper_factory.new(mapper).instance_exec(&blk)
37
+ mapper.prepare_dto
37
38
 
38
39
  mapper_factory.register(register_as, mapper) if register_as
39
40
 
@@ -3,24 +3,19 @@
3
3
  class ReeMapper::MapperStrategy
4
4
  attr_reader :method, :always_optional
5
5
 
6
- contract(Symbol, ReeMapper::StrategyOutput, Bool => Any)
7
- def initialize(method:, output:, always_optional:)
6
+ contract(Symbol, Class, Bool => Any)
7
+ def initialize(method:, dto:, always_optional:)
8
8
  @method = method
9
- @output = output
9
+ @output = build_output(dto)
10
10
  @always_optional = always_optional
11
11
  end
12
12
 
13
- contract None => ReeMapper::StrategyOutput
14
- def output
15
- @output
13
+ def initialize_dup(_orig)
14
+ @output = @output.dup
15
+ super
16
16
  end
17
17
 
18
- contract ReeMapper::StrategyOutput => ReeMapper::StrategyOutput
19
- def output=(output)
20
- @output = output
21
- end
22
-
23
- contract(Any)
18
+ contract(None => Object)
24
19
  def build_object
25
20
  output.build_object
26
21
  end
@@ -48,13 +43,33 @@ class ReeMapper::MapperStrategy
48
43
  end
49
44
  end
50
45
 
51
- contract(Class => Class)
46
+ contract(Class => nil)
52
47
  def dto=(dto)
53
- output.dto = dto
48
+ @output = build_output(dto)
49
+ nil
54
50
  end
55
51
 
56
- contract(None => Nilor[Class])
52
+ contract(None => Class)
57
53
  def dto
58
54
  output.dto
59
55
  end
56
+
57
+ contract(ArrayOf[Symbol] => nil)
58
+ def prepare_dto(field_names)
59
+ output.prepare_dto(field_names)
60
+ end
61
+
62
+ private
63
+
64
+ attr_reader :output
65
+
66
+ def build_output(dto)
67
+ if dto == Hash || (defined?(OpenStruct) && dto == OpenStruct)
68
+ ReeMapper::HashOutput.new(dto)
69
+ elsif dto == Struct
70
+ ReeMapper::StructOutput.new
71
+ else
72
+ ReeMapper::ObjectOutput.new(dto)
73
+ end
74
+ end
60
75
  end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class ReeMapper::SymbolKeyHashOutput < ReeMapper::StrategyOutput
4
- contract(Hash)
3
+ class ReeMapper::HashOutput < ReeMapper::StrategyOutput
4
+ contract(None => Object)
5
5
  def build_object
6
- Hash.new
6
+ dto.new
7
7
  end
8
8
 
9
9
  contract(Object, ReeMapper::Field, Any => nil)
@@ -11,4 +11,4 @@ class ReeMapper::SymbolKeyHashOutput < ReeMapper::StrategyOutput
11
11
  object[field.name] = value
12
12
  nil
13
13
  end
14
- end
14
+ end
@@ -1,14 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ReeMapper::ObjectOutput < ReeMapper::StrategyOutput
4
- def initialize(*)
5
- @dto = Object
6
- super
7
- end
8
-
9
- contract(Object)
4
+ contract(None => Object)
10
5
  def build_object
11
- @dto.allocate
6
+ dto.allocate
12
7
  end
13
8
 
14
9
  contract(Object, ReeMapper::Field, Any => nil)
@@ -1,21 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ReeMapper::StrategyOutput
4
- def build_object
5
- raise NotImplementedError
6
- end
4
+ attr_reader :dto
7
5
 
8
- def assign_value(*)
9
- raise NotImplementedError
10
- end
11
-
12
- contract(Class => Class)
13
- def dto=(dto)
6
+ contract(Class => Any)
7
+ def initialize(dto)
14
8
  @dto = dto
15
9
  end
16
10
 
17
- contract(None => Nilor[Class])
18
- def dto
19
- @dto
11
+ contract(Object, ReeMapper::Field, Any => nil).throws(NotImplementedError)
12
+ def assign_value(object, field, value)
13
+ raise NotImplementedError
20
14
  end
15
+
16
+ contract(ArrayOf[Symbol] => nil)
17
+ def prepare_dto(field_names); end
21
18
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ReeMapper::StructOutput < ReeMapper::StrategyOutput
4
+ contract(None => Any)
5
+ def initialize; end
6
+
7
+ def initialize_dup(orig)
8
+ @dto = nil
9
+ super
10
+ end
11
+
12
+ contract(None => Object)
13
+ def build_object
14
+ dto.allocate
15
+ end
16
+
17
+ contract(Object, ReeMapper::Field, Any => nil)
18
+ def assign_value(object, field, value)
19
+ object[field.name] = value
20
+ nil
21
+ end
22
+
23
+ contract(ArrayOf[Symbol] => nil)
24
+ def prepare_dto(field_names)
25
+ @dto = Struct.new(*field_names)
26
+ nil
27
+ end
28
+ end
@@ -33,8 +33,8 @@ module ReeMapper
33
33
 
34
34
  require_relative 'ree_mapper/strategy_outputs/strategy_output'
35
35
  require_relative 'ree_mapper/strategy_outputs/object_output'
36
- require_relative 'ree_mapper/strategy_outputs/string_key_hash_output'
37
- require_relative 'ree_mapper/strategy_outputs/symbol_key_hash_output'
36
+ require_relative 'ree_mapper/strategy_outputs/hash_output'
37
+ require_relative 'ree_mapper/strategy_outputs/struct_output'
38
38
 
39
39
  require_relative 'ree_mapper/mapper_strategy'
40
40
  require_relative 'ree_mapper/mapper'
@@ -154,10 +154,10 @@ Create `mapper_factory.rb` file to declare `MapperFactory` class.
154
154
 
155
155
  def build
156
156
  mapper_factory = build_mapper_factory(strategies: [
157
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
158
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
159
- build_mapper_strategy(method: :db_dump, output: :string_key_hash),
160
- build_mapper_strategy(method: :db_load, output: :object)
157
+ build_mapper_strategy(method: :cast, dto: Hash),
158
+ build_mapper_strategy(method: :serialize, dto: Hash),
159
+ build_mapper_strategy(method: :db_dump, dto: Hash),
160
+ build_mapper_strategy(method: :db_load, dto: Object)
161
161
  ])
162
162
 
163
163
  mapper_factory.register(:cart_user, user_caster)
@@ -10,7 +10,7 @@
10
10
  {
11
11
  "doc": "",
12
12
  "throws": [
13
- "ArgumentError"
13
+
14
14
  ],
15
15
  "return": "ReeMapper::MapperStrategy",
16
16
  "args": [
@@ -19,8 +19,8 @@
19
19
  "type": "Symbol"
20
20
  },
21
21
  {
22
- "arg": "output",
23
- "type": "Symbol"
22
+ "arg": "dto",
23
+ "type": "Class"
24
24
  },
25
25
  {
26
26
  "arg": "always_optional",
@@ -6,7 +6,7 @@ RSpec.describe :build_mapper_factory do
6
6
 
7
7
  it {
8
8
  result = build_mapper_factory(strategies: [
9
- build_mapper_strategy(method: :cast, output: :symbol_key_hash)
9
+ build_mapper_strategy(method: :cast, dto: Hash)
10
10
  ])
11
11
 
12
12
  expect(result).to be_a(Class)
@@ -4,7 +4,7 @@ RSpec.describe :build_mapper_strategy do
4
4
  link :build_mapper_strategy, from: :ree_mapper
5
5
 
6
6
  it {
7
- result = build_mapper_strategy(method: :cast, output: :symbol_key_hash)
7
+ result = build_mapper_strategy(method: :cast, dto: Hash)
8
8
 
9
9
  expect(result).to be_a(ReeMapper::MapperStrategy)
10
10
  }
@@ -7,7 +7,7 @@ RSpec.describe ReeMapper::MapperFactory do
7
7
 
8
8
  let(:mapper_factory) {
9
9
  build_mapper_factory(strategies: [
10
- build_mapper_strategy(method: :cast, output: :symbol_key_hash)
10
+ build_mapper_strategy(method: :cast, dto: Hash)
11
11
  ])
12
12
  }
13
13
 
@@ -72,5 +72,12 @@ RSpec.describe ReeMapper::MapperFactory do
72
72
  end
73
73
  }.to raise_error(ArgumentError, 'MapperFactory strategy :not_found not found')
74
74
  }
75
+
76
+ it {
77
+ expect {
78
+ mapper_factory.call.use(:cast) do
79
+ end
80
+ }.to raise_error(ReeMapper::ArgumentError, "mapper should contain at least one field")
81
+ }
75
82
  end
76
83
  end
@@ -4,11 +4,11 @@ RSpec.describe ReeMapper::Mapper do
4
4
  link :build_mapper_factory, from: :ree_mapper
5
5
  link :build_mapper_strategy, from: :ree_mapper
6
6
 
7
- describe 'input' do
7
+ describe '#:strategy_method' do
8
8
  let(:mapper) {
9
9
  build_mapper_factory(
10
10
  strategies: [
11
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
11
+ build_mapper_strategy(method: :cast, dto: Hash),
12
12
  ]
13
13
  ).call.use(:cast) do
14
14
  integer :my_field
@@ -26,13 +26,19 @@ RSpec.describe ReeMapper::Mapper do
26
26
  it {
27
27
  expect(mapper.cast(OpenStruct.new({ my_field: 1 }))).to eq({ my_field: 1 })
28
28
  }
29
+
30
+ it {
31
+ obj = Object.new
32
+ obj.define_singleton_method(:my_field) { 1 }
33
+ expect(mapper.cast(obj)).to eq({ my_field: 1 })
34
+ }
29
35
  end
30
36
 
31
- describe 'string key hash output' do
37
+ describe 'hash dto' do
32
38
  let(:mapper) {
33
39
  build_mapper_factory(
34
40
  strategies: [
35
- build_mapper_strategy(method: :cast, output: :string_key_hash),
41
+ build_mapper_strategy(method: :cast, dto: Hash),
36
42
  ]
37
43
  ).call.use(:cast) do
38
44
  integer :my_field
@@ -40,15 +46,15 @@ RSpec.describe ReeMapper::Mapper do
40
46
  }
41
47
 
42
48
  it {
43
- expect(mapper.cast({ my_field: 1 })).to eq({ 'my_field' => 1 })
49
+ expect(mapper.cast({ my_field: 1 })).to eq({ my_field: 1 })
44
50
  }
45
51
  end
46
52
 
47
- describe 'string key hash output' do
53
+ describe 'ostruct dto' do
48
54
  let(:mapper) {
49
55
  build_mapper_factory(
50
56
  strategies: [
51
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
57
+ build_mapper_strategy(method: :cast, dto: OpenStruct),
52
58
  ]
53
59
  ).call.use(:cast) do
54
60
  integer :my_field
@@ -56,16 +62,47 @@ RSpec.describe ReeMapper::Mapper do
56
62
  }
57
63
 
58
64
  it {
59
- expect(mapper.cast({ my_field: 1 })).to eq({ my_field: 1 })
65
+ expect(mapper.cast({ my_field: 1 }).to_h).to eq({ my_field: 1 })
66
+ }
67
+
68
+ it {
69
+ expect(mapper.cast({ my_field: 1 })).to be_a(OpenStruct)
60
70
  }
61
71
  end
62
72
 
63
- describe 'object output' do
73
+ describe 'struct dto' do
74
+ let(:mapper) {
75
+ build_mapper_factory(
76
+ strategies: [
77
+ build_mapper_strategy(method: :cast, dto: Struct),
78
+ ]
79
+ ).call.use(:cast) do
80
+ integer :my_field
81
+ hash :hsh do
82
+ integer :nested_field
83
+ end
84
+ end
85
+ }
86
+
87
+ it {
88
+ nested_struct = Struct.new(:nested_field)
89
+ struct = Struct.new(:my_field, :hsh)
90
+ expect(mapper.cast({ my_field: 1, hsh: { nested_field: 1 } }).inspect).to eq(
91
+ struct.new(1, nested_struct.new(1)).inspect
92
+ )
93
+ }
94
+
95
+ it {
96
+ expect(mapper.cast({ my_field: 1, hsh: { nested_field: 1 } })).to be_a(Struct)
97
+ }
98
+ end
99
+
100
+ describe 'object dto' do
64
101
  let(:dto) { Class.new }
65
102
  let(:mapper) {
66
103
  build_mapper_factory(
67
104
  strategies: [
68
- build_mapper_strategy(method: :cast, output: :object),
105
+ build_mapper_strategy(method: :cast, dto: Object),
69
106
  ]
70
107
  ).call.use(:cast, dto: dto) do
71
108
  integer :my_field
@@ -87,7 +124,7 @@ RSpec.describe ReeMapper::Mapper do
87
124
  let(:mapper_factory) {
88
125
  build_mapper_factory(
89
126
  strategies: [
90
- build_mapper_strategy(method: :cast, output: :symbol_key_hash, always_optional: true),
127
+ build_mapper_strategy(method: :cast, dto: Hash, always_optional: true),
91
128
  ]
92
129
  )
93
130
  }
@@ -101,4 +138,32 @@ RSpec.describe ReeMapper::Mapper do
101
138
  expect(mapper.cast({})).to eq({})
102
139
  }
103
140
  end
141
+
142
+ describe '#dto' do
143
+ let(:mapper_factory) {
144
+ build_mapper_factory(
145
+ strategies: [
146
+ build_mapper_strategy(method: :cast, dto: Hash, always_optional: true),
147
+ build_mapper_strategy(method: :serialize, dto: Object),
148
+ ]
149
+ )
150
+ }
151
+ let(:mapper) {
152
+ mapper_factory.call.use(:cast).use(:serialize, dto: Struct) do
153
+ integer :my_field
154
+ end
155
+ }
156
+
157
+ it {
158
+ expect(mapper.dto(:cast)).to eq(Hash)
159
+ }
160
+
161
+ it {
162
+ expect(mapper.dto(:serialize)).to be < Struct
163
+ }
164
+
165
+ it {
166
+ expect { mapper.dto(:db_dump) }.to raise_error(ArgumentError, "there is no :db_dump strategy")
167
+ }
168
+ end
104
169
  end
@@ -7,10 +7,10 @@ RSpec.describe 'ReeMapper::Array' do
7
7
  let(:mapper_factory) {
8
8
  build_mapper_factory(
9
9
  strategies: [
10
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
11
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
12
- build_mapper_strategy(method: :db_dump, output: :symbol_key_hash),
13
- build_mapper_strategy(method: :db_load, output: :symbol_key_hash)
10
+ build_mapper_strategy(method: :cast, dto: Hash),
11
+ build_mapper_strategy(method: :serialize, dto: Hash),
12
+ build_mapper_strategy(method: :db_dump, dto: Hash),
13
+ build_mapper_strategy(method: :db_load, dto: Hash)
14
14
  ]
15
15
  )
16
16
  }
@@ -135,10 +135,10 @@ RSpec.describe 'ReeMapper::Array' do
135
135
  }
136
136
  end
137
137
 
138
- context 'with array of hashes with key option' do
138
+ context 'with array of hashes with dto option' do
139
139
  let(:mapper) {
140
140
  mapper_factory.call.use(:serialize) {
141
- array :coords, key: :string do
141
+ array :coords, dto: OpenStruct do
142
142
  integer :x
143
143
  integer :y
144
144
  end
@@ -146,8 +146,8 @@ RSpec.describe 'ReeMapper::Array' do
146
146
  }
147
147
 
148
148
  it {
149
- expect(mapper.serialize({ coords: [{ x: 1, y: 1 }, { x: 2, y: 2 }] }))
150
- .to eq({ coords: [{ 'x' => 1, 'y' => 1 }, { 'x' => 2, 'y' => 2 }] })
149
+ expect(mapper.serialize({ coords: [{ x: 1, y: 1 }] }))
150
+ .to eq({ coords: [OpenStruct.new({ x: 1, y: 1 })] })
151
151
  }
152
152
  end
153
153
  end
@@ -7,10 +7,10 @@ RSpec.describe 'ReeMapper::Bool' do
7
7
  let(:mapper_factory) {
8
8
  build_mapper_factory(
9
9
  strategies: [
10
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
11
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
12
- build_mapper_strategy(method: :db_dump, output: :symbol_key_hash),
13
- build_mapper_strategy(method: :db_load, output: :symbol_key_hash)
10
+ build_mapper_strategy(method: :cast, dto: Hash),
11
+ build_mapper_strategy(method: :serialize, dto: Hash),
12
+ build_mapper_strategy(method: :db_dump, dto: Hash),
13
+ build_mapper_strategy(method: :db_load, dto: Hash)
14
14
  ]
15
15
  )
16
16
  }
@@ -7,10 +7,10 @@ RSpec.describe 'ReeMapper::Date' do
7
7
  let(:mapper_factory) {
8
8
  build_mapper_factory(
9
9
  strategies: [
10
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
11
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
12
- build_mapper_strategy(method: :db_dump, output: :symbol_key_hash),
13
- build_mapper_strategy(method: :db_load, output: :symbol_key_hash)
10
+ build_mapper_strategy(method: :cast, dto: Hash),
11
+ build_mapper_strategy(method: :serialize, dto: Hash),
12
+ build_mapper_strategy(method: :db_dump, dto: Hash),
13
+ build_mapper_strategy(method: :db_load, dto: Hash)
14
14
  ]
15
15
  )
16
16
  }
@@ -7,10 +7,10 @@ RSpec.describe 'ReeMapper::DateTime' do
7
7
  let(:mapper_factory) {
8
8
  build_mapper_factory(
9
9
  strategies: [
10
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
11
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
12
- build_mapper_strategy(method: :db_dump, output: :symbol_key_hash),
13
- build_mapper_strategy(method: :db_load, output: :symbol_key_hash)
10
+ build_mapper_strategy(method: :cast, dto: Hash),
11
+ build_mapper_strategy(method: :serialize, dto: Hash),
12
+ build_mapper_strategy(method: :db_dump, dto: Hash),
13
+ build_mapper_strategy(method: :db_load, dto: Hash)
14
14
  ]
15
15
  )
16
16
  }
@@ -7,10 +7,10 @@ RSpec.describe 'ReeMapper::Float' do
7
7
  let(:mapper_factory) {
8
8
  build_mapper_factory(
9
9
  strategies: [
10
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
11
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
12
- build_mapper_strategy(method: :db_dump, output: :symbol_key_hash),
13
- build_mapper_strategy(method: :db_load, output: :symbol_key_hash)
10
+ build_mapper_strategy(method: :cast, dto: Hash),
11
+ build_mapper_strategy(method: :serialize, dto: Hash),
12
+ build_mapper_strategy(method: :db_dump, dto: Hash),
13
+ build_mapper_strategy(method: :db_load, dto: Hash)
14
14
  ]
15
15
  )
16
16
  }
@@ -7,7 +7,7 @@ RSpec.describe 'Mapper Hash' do
7
7
  let(:mapper_factory) {
8
8
  build_mapper_factory(
9
9
  strategies: [
10
- build_mapper_strategy(method: :cast, output: :symbol_key_hash)
10
+ build_mapper_strategy(method: :cast, dto: Hash)
11
11
  ]
12
12
  )
13
13
  }
@@ -35,38 +35,16 @@ RSpec.describe 'Mapper Hash' do
35
35
  }
36
36
  end
37
37
 
38
- describe 'key: option' do
38
+ describe 'dto: option' do
39
39
  it {
40
40
  expect(
41
41
  mapper_factory.call.use(:cast) {
42
- hash :point, key: :string do
42
+ hash :point, dto: OpenStruct do
43
43
  integer :x
44
44
  integer :y
45
45
  end
46
46
  }.cast({ point: { x: 1, y: 1 } })
47
- ).to eq({ point: { 'x' => 1, 'y' => 1 } })
48
- }
49
-
50
- it {
51
- expect(
52
- mapper_factory.call.use(:cast) {
53
- hash :point, key: :symbol do
54
- integer :x
55
- integer :y
56
- end
57
- }.cast({ point: { x: 1, y: 1 } })
58
- ).to eq({ point: { x: 1, y: 1 } })
59
- }
60
-
61
- it {
62
- expect {
63
- mapper_factory.call.use(:cast) {
64
- hash :point, key: :whatever do
65
- integer :x
66
- integer :y
67
- end
68
- }
69
- }.to raise_error(ArgumentError)
47
+ ).to eq({ point: OpenStruct.new({ x: 1, y: 1 }) })
70
48
  }
71
49
  end
72
50
 
@@ -74,7 +52,7 @@ RSpec.describe 'Mapper Hash' do
74
52
  let(:mapper_factory) {
75
53
  build_mapper_factory(
76
54
  strategies: [
77
- build_mapper_strategy(method: :cast, output: :object)
55
+ build_mapper_strategy(method: :cast, dto: Object)
78
56
  ]
79
57
  )
80
58
  }
@@ -7,10 +7,10 @@ RSpec.describe 'ReeMapper::Integer' do
7
7
  let(:mapper_factory) {
8
8
  build_mapper_factory(
9
9
  strategies: [
10
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
11
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
12
- build_mapper_strategy(method: :db_dump, output: :symbol_key_hash),
13
- build_mapper_strategy(method: :db_load, output: :symbol_key_hash)
10
+ build_mapper_strategy(method: :cast, dto: Hash),
11
+ build_mapper_strategy(method: :serialize, dto: Hash),
12
+ build_mapper_strategy(method: :db_dump, dto: Hash),
13
+ build_mapper_strategy(method: :db_load, dto: Hash)
14
14
  ]
15
15
  )
16
16
  }
@@ -7,10 +7,10 @@ RSpec.describe 'ReeMapper::String' do
7
7
  let(:mapper_factory) {
8
8
  build_mapper_factory(
9
9
  strategies: [
10
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
11
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
12
- build_mapper_strategy(method: :db_dump, output: :symbol_key_hash),
13
- build_mapper_strategy(method: :db_load, output: :symbol_key_hash)
10
+ build_mapper_strategy(method: :cast, dto: Hash),
11
+ build_mapper_strategy(method: :serialize, dto: Hash),
12
+ build_mapper_strategy(method: :db_dump, dto: Hash),
13
+ build_mapper_strategy(method: :db_load, dto: Hash)
14
14
  ]
15
15
  )
16
16
  }
@@ -7,10 +7,10 @@ RSpec.describe 'ReeMapper::Time' do
7
7
  let(:mapper_factory) {
8
8
  build_mapper_factory(
9
9
  strategies: [
10
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
11
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
12
- build_mapper_strategy(method: :db_dump, output: :symbol_key_hash),
13
- build_mapper_strategy(method: :db_load, output: :symbol_key_hash)
10
+ build_mapper_strategy(method: :cast, dto: Hash),
11
+ build_mapper_strategy(method: :serialize, dto: Hash),
12
+ build_mapper_strategy(method: :db_dump, dto: Hash),
13
+ build_mapper_strategy(method: :db_load, dto: Hash)
14
14
  ]
15
15
  )
16
16
  }
@@ -7,10 +7,10 @@ RSpec.describe 'ReeMapper::MapperFactory type options' do
7
7
  let(:mapper_factory) {
8
8
  build_mapper_factory(
9
9
  strategies: [
10
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
11
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
12
- build_mapper_strategy(method: :db_dump, output: :symbol_key_hash),
13
- build_mapper_strategy(method: :db_load, output: :symbol_key_hash)
10
+ build_mapper_strategy(method: :cast, dto: Hash),
11
+ build_mapper_strategy(method: :serialize, dto: Hash),
12
+ build_mapper_strategy(method: :db_dump, dto: Hash),
13
+ build_mapper_strategy(method: :db_load, dto: Hash)
14
14
  ]
15
15
  )
16
16
  }
@@ -13,8 +13,8 @@ RSpec.describe :build_endpoint_schema do
13
13
 
14
14
  let(:mapper_factory) {
15
15
  strategies = [
16
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
17
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
16
+ build_mapper_strategy(method: :serialize, dto: Hash),
17
+ build_mapper_strategy(method: :cast, dto: Hash),
18
18
  ]
19
19
 
20
20
  build_mapper_factory(
@@ -1,11 +1,11 @@
1
1
  RSpec.describe :build_parameters_spec do
2
- link :build_parameters, from: :ree_swagger
3
2
  link :build_mapper_factory, from: :ree_mapper
4
3
  link :build_mapper_strategy, from: :ree_mapper
4
+ link :build_parameters, from: :ree_swagger
5
5
 
6
6
  let(:mapper_factory) {
7
7
  strategies = [
8
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
8
+ build_mapper_strategy(method: :cast, dto: Hash),
9
9
  ]
10
10
 
11
11
  build_mapper_factory(
@@ -1,11 +1,11 @@
1
1
  RSpec.describe :build_request_body_schema_spec do
2
- link :build_request_body_schema, from: :ree_swagger
3
2
  link :build_mapper_factory, from: :ree_mapper
4
3
  link :build_mapper_strategy, from: :ree_mapper
4
+ link :build_request_body_schema, from: :ree_swagger
5
5
 
6
6
  let(:mapper_factory) {
7
7
  strategies = [
8
- build_mapper_strategy(method: :cast, output: :symbol_key_hash),
8
+ build_mapper_strategy(method: :cast, dto: Hash),
9
9
  ]
10
10
 
11
11
  build_mapper_factory(
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
  RSpec.describe :build_serializer_schema do
3
- link :build_serializer_schema, from: :ree_swagger
4
3
  link :build_mapper_factory, from: :ree_mapper
5
4
  link :build_mapper_strategy, from: :ree_mapper
5
+ link :build_serializer_schema, from: :ree_swagger
6
6
 
7
7
  let(:mapper_factory) {
8
8
  strategies = [
9
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
9
+ build_mapper_strategy(method: :serialize, dto: Hash),
10
10
  ]
11
11
 
12
12
  build_mapper_factory(
@@ -1,8 +1,8 @@
1
1
  RSpec.describe :register_type do
2
- link :register_type, from: :ree_swagger
3
- link :build_serializer_schema, from: :ree_swagger
4
2
  link :build_mapper_factory, from: :ree_mapper
5
3
  link :build_mapper_strategy, from: :ree_mapper
4
+ link :build_serializer_schema, from: :ree_swagger
5
+ link :register_type, from: :ree_swagger
6
6
 
7
7
  class ReeSwagger::MyType < ReeMapper::AbstractType
8
8
  def serialize(obj, role: nil)
@@ -12,7 +12,7 @@ RSpec.describe :register_type do
12
12
 
13
13
  let(:mapper_factory) {
14
14
  strategies = [
15
- build_mapper_strategy(method: :serialize, output: :symbol_key_hash),
15
+ build_mapper_strategy(method: :serialize, dto: Hash),
16
16
  ]
17
17
 
18
18
  build_mapper_factory(strategies: strategies).register(
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ReeLib
4
- VERSION = "1.0.30"
4
+ VERSION = "1.0.31"
5
5
  end
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.30
4
+ version: 1.0.31
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-02-07 00:00:00.000000000 Z
11
+ date: 2023-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ree
@@ -921,10 +921,10 @@ files:
921
921
  - lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory.rb
922
922
  - lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_factory_proxy.rb
923
923
  - lib/ree_lib/packages/ree_mapper/package/ree_mapper/mapper_strategy.rb
924
+ - lib/ree_lib/packages/ree_mapper/package/ree_mapper/strategy_outputs/hash_output.rb
924
925
  - lib/ree_lib/packages/ree_mapper/package/ree_mapper/strategy_outputs/object_output.rb
925
926
  - lib/ree_lib/packages/ree_mapper/package/ree_mapper/strategy_outputs/strategy_output.rb
926
- - lib/ree_lib/packages/ree_mapper/package/ree_mapper/strategy_outputs/string_key_hash_output.rb
927
- - lib/ree_lib/packages/ree_mapper/package/ree_mapper/strategy_outputs/symbol_key_hash_output.rb
927
+ - lib/ree_lib/packages/ree_mapper/package/ree_mapper/strategy_outputs/struct_output.rb
928
928
  - lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/abstract_type.rb
929
929
  - lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/array.rb
930
930
  - lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class ReeMapper::StringKeyHashOutput < ReeMapper::StrategyOutput
4
- contract(Hash)
5
- def build_object
6
- Hash.new
7
- end
8
-
9
- contract(Object, ReeMapper::Field, Any => nil)
10
- def assign_value(object, field, value)
11
- object[field.name_as_str] = value
12
- nil
13
- end
14
- end