yaoc 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +5 -1
  5. data/Guardfile +5 -0
  6. data/Rakefile +3 -0
  7. data/examples/01_hash_enabled_constructors.rb +8 -8
  8. data/examples/02_procs_as_constructors.rb +11 -13
  9. data/examples/03_positional_constructors.rb +7 -13
  10. data/examples/04_compositions.rb +3 -8
  11. data/examples/05_fill_existing_objects.rb +2 -8
  12. data/examples/06_lazy_loading.rb +0 -7
  13. data/examples/all_examples.rb +2 -2
  14. data/lib/yaoc.rb +2 -3
  15. data/lib/yaoc/converter_builder.rb +14 -21
  16. data/lib/yaoc/helper/struct_hash_constructor.rb +3 -4
  17. data/lib/yaoc/helper/to_proc_delegator.rb +4 -6
  18. data/lib/yaoc/many_to_one_mapper_chain.rb +11 -13
  19. data/lib/yaoc/mapper_registry.rb +3 -4
  20. data/lib/yaoc/mapping_base.rb +11 -12
  21. data/lib/yaoc/mapping_to_class.rb +7 -9
  22. data/lib/yaoc/object_mapper.rb +10 -13
  23. data/lib/yaoc/one_to_many_mapper_chain.rb +4 -6
  24. data/lib/yaoc/strategies/to_array_mapping.rb +1 -3
  25. data/lib/yaoc/strategies/to_hash_mapping.rb +1 -3
  26. data/lib/yaoc/transformation_command.rb +27 -8
  27. data/lib/yaoc/transformation_deferred_command.rb +3 -5
  28. data/lib/yaoc/version.rb +1 -1
  29. data/rubocop-todo.yml +96 -0
  30. data/spec/acceptance/fill_existing_objects_spec.rb +30 -30
  31. data/spec/acceptance/map_multiple_objects_to_one_in_a_chain_spec.rb +15 -15
  32. data/spec/acceptance/map_objects_spec.rb +22 -22
  33. data/spec/acceptance/map_one_object_to_many_in_a_chain_spec.rb +15 -15
  34. data/spec/acceptance/map_to_objects_using_other_converters_spec.rb +53 -55
  35. data/spec/acceptance/map_to_objects_with_lazy_loading_spec.rb +17 -17
  36. data/spec/acceptance/map_to_objects_with_positional_constructors_spec.rb +19 -19
  37. data/spec/integration/lib/yaoc/converter_builder_spec.rb +21 -21
  38. data/spec/spec_helper.rb +7 -7
  39. data/spec/support/feature.rb +1 -2
  40. data/spec/unit/lib/yaoc/converter_builder_spec.rb +42 -44
  41. data/spec/unit/lib/yaoc/helper/struct_hash_constructor_spec.rb +15 -15
  42. data/spec/unit/lib/yaoc/helper/to_proc_delegator_spec.rb +11 -11
  43. data/spec/unit/lib/yaoc/many_to_one_mapper_chain_spec.rb +19 -20
  44. data/spec/unit/lib/yaoc/mapper_registry_spec.rb +6 -6
  45. data/spec/unit/lib/yaoc/mapping_base_spec.rb +36 -36
  46. data/spec/unit/lib/yaoc/mapping_to_class_spec.rb +19 -21
  47. data/spec/unit/lib/yaoc/object_mapper_spec.rb +45 -49
  48. data/spec/unit/lib/yaoc/one_to_many_mapper_chain_spec.rb +17 -18
  49. data/spec/unit/lib/yaoc/strategies/to_array_mapping_spec.rb +23 -24
  50. data/spec/unit/lib/yaoc/strategies/to_hash_mapping_spec.rb +25 -25
  51. data/spec/unit/lib/yaoc/transformation_command_spec.rb +13 -13
  52. data/spec/unit/lib/yaoc/transformation_deferred_command_spec.rb +9 -9
  53. data/yaoc.gemspec +1 -0
  54. metadata +18 -2
@@ -6,11 +6,11 @@ module Yaoc
6
6
  end
7
7
 
8
8
  module Initializer
9
- def initialize(params={})
9
+ def initialize(params = {})
10
10
  super()
11
11
 
12
12
  params.each do |attr, value|
13
- self.public_send("#{attr}=", value)
13
+ public_send("#{attr}=", value)
14
14
  end if params
15
15
  end
16
16
  end
@@ -28,6 +28,5 @@ module Yaoc
28
28
  include Equalizer.new(*args)
29
29
  end
30
30
  end
31
-
32
31
  end
33
- end
32
+ end
@@ -12,11 +12,10 @@ module Yaoc
12
12
  end
13
13
  end
14
14
 
15
-
16
15
  def __getobj__
17
- unless self._initialisation_proc_loaded
16
+ unless _initialisation_proc_loaded
18
17
  self._initialisation_proc_loaded = true
19
- __setobj__(self._initialisation_proc.call())
18
+ __setobj__(_initialisation_proc.call)
20
19
  end
21
20
 
22
21
  super
@@ -35,13 +34,12 @@ module Yaoc
35
34
  end
36
35
 
37
36
  def _initialisation_proc_loaded?
38
- self._initialisation_proc_loaded
37
+ _initialisation_proc_loaded
39
38
  end
40
39
 
41
40
  def _needs_conversion?
42
41
  _initialisation_proc_loaded? && ! nil?
43
42
  end
44
-
45
43
  end
46
44
  end
47
- end
45
+ end
@@ -10,18 +10,18 @@ module Yaoc
10
10
  self.converter = converter
11
11
  end
12
12
 
13
- def load_first(input_object, object_to_fill=nil)
13
+ def load_first(input_object, object_to_fill = nil)
14
14
  converter_iterator.rewind
15
15
  self.next_result = converter_iterator.next.load(input_object, object_to_fill)
16
16
  end
17
17
 
18
18
  def load_next(input_object)
19
- self.next_result = converter_iterator.next.load(input_object, self.next_result)
19
+ self.next_result = converter_iterator.next.load(input_object, next_result)
20
20
  rescue StopIteration
21
- raise "ToManyInputObjects"
21
+ raise 'ToManyInputObjects'
22
22
  end
23
23
 
24
- def load_all(input_objects, object_to_fill=nil)
24
+ def load_all(input_objects, object_to_fill = nil)
25
25
  each_object_with_converter(input_objects) do |converter, input_object|
26
26
  object_to_fill = converter.load(input_object, object_to_fill)
27
27
  end
@@ -29,17 +29,16 @@ module Yaoc
29
29
  self.last_result = object_to_fill
30
30
  end
31
31
 
32
- def dump_first(input_object, object_to_fill=nil)
32
+ def dump_first(input_object, object_to_fill = nil)
33
33
  converter_iterator.rewind
34
34
  self.next_result = converter_iterator.next.dump(input_object, object_to_fill)
35
35
  end
36
36
 
37
37
  def dump_next(input_object)
38
- self.next_result = converter_iterator.next.dump(input_object, self.next_result)
38
+ self.next_result = converter_iterator.next.dump(input_object, next_result)
39
39
  end
40
40
 
41
- def dump_all(input_objects, object_to_fill=nil)
42
-
41
+ def dump_all(input_objects, object_to_fill = nil)
43
42
  each_object_with_converter(input_objects) do |converter, input_object|
44
43
  object_to_fill = converter.dump(input_object, object_to_fill)
45
44
  end
@@ -50,16 +49,16 @@ module Yaoc
50
49
  protected
51
50
 
52
51
  def converter=(new_converter)
53
- @converter = new_converter.map{|converter| converter.is_a?(Symbol) ? ManyToOneMapperChain.registry.for(converter) : converter}
52
+ @converter = new_converter.map { |converter| converter.is_a?(Symbol) ? ManyToOneMapperChain.registry.for(converter) : converter }
54
53
  end
55
54
 
56
55
  def converter_iterator
57
- @converter_iterator ||= self.converter.each
56
+ @converter_iterator ||= converter.each
58
57
  end
59
58
 
60
59
  def each_object_with_converter(input_objects)
61
60
  it_input_objects = input_objects.each
62
- it_converters = self.converter.each
61
+ it_converters = converter.each
63
62
 
64
63
  loop do
65
64
  begin
@@ -73,6 +72,5 @@ module Yaoc
73
72
  end
74
73
  end
75
74
  end
76
-
77
75
  end
78
- end
76
+ end
@@ -3,11 +3,11 @@ module Yaoc
3
3
  module_function
4
4
 
5
5
  def scope_storage(new_storage)
6
- @scope = ScopedStorage::Scope.new("mappings", new_storage)
6
+ @scope = ScopedStorage::Scope.new('mappings', new_storage)
7
7
  end
8
8
 
9
9
  def scope
10
- @scope ||= ScopedStorage::Scope.new("mappings")
10
+ @scope ||= ScopedStorage::Scope.new('mappings')
11
11
  end
12
12
 
13
13
  def add(key, mapper)
@@ -17,6 +17,5 @@ module Yaoc
17
17
  def for(key)
18
18
  scope[key.to_sym]
19
19
  end
20
-
21
20
  end
22
- end
21
+ end
@@ -1,12 +1,11 @@
1
1
  module Yaoc
2
2
  module MappingBase
3
-
4
3
  def self.included(other)
5
4
  other.extend(ClassMethods)
6
5
  end
7
6
 
8
7
  def to_proc
9
- ->(to_convert){
8
+ ->(to_convert)do
10
9
  old_to_convert = self.to_convert
11
10
  begin
12
11
  self.to_convert = to_convert
@@ -14,7 +13,7 @@ module Yaoc
14
13
  ensure
15
14
  self.to_convert = old_to_convert
16
15
  end
17
- }
16
+ end
18
17
  end
19
18
 
20
19
  def call
@@ -26,7 +25,6 @@ module Yaoc
26
25
  end
27
26
  alias_method :to_hash_or_array, :call
28
27
 
29
-
30
28
  def converter_methods
31
29
  self.class.converter_methods
32
30
  end
@@ -36,7 +34,6 @@ module Yaoc
36
34
  end
37
35
 
38
36
  module ClassMethods
39
-
40
37
  def mapping_strategy=(new_strat)
41
38
  @mapping_strategy = new_strat
42
39
  end
@@ -49,18 +46,22 @@ module Yaoc
49
46
  class_private_module(:Mapping).tap do |mod|
50
47
  method_implementation = TransformationCommand.create(to: to, from: from, deferred: lazy_loading, conversion_proc: converter)
51
48
 
52
- mod.send :define_method, "map_#{"%04d" %[converter_methods.count]}_#{from}_to_#{to}".to_sym, method_implementation
49
+ method_name = "map_#{"%04d" % [converter_methods.count]}_#{from}_to_#{to}".to_sym
50
+ converter_methods << method_name
51
+
52
+ converter_methods.sort!.uniq!
53
+
54
+ mod.send :define_method, method_name, method_implementation
53
55
  include mod
54
56
  end
55
57
  end
56
58
 
57
59
  def converter_methods
58
- class_private_module(:Mapping).instance_methods(false).sort
60
+ @converter_methods ||= []
59
61
  end
60
62
 
61
63
  # inspired by Avdi Grimm, rubytapas.com 028-macros-and-modules
62
- def class_private_module(name=:Mapping)
63
-
64
+ def class_private_module(name = :Mapping)
64
65
  if const_defined?(name, false)
65
66
  const_get(name)
66
67
  else
@@ -76,8 +77,6 @@ module Yaoc
76
77
  const_set(name, new_mod)
77
78
  end
78
79
  end
79
-
80
80
  end
81
-
82
81
  end
83
- end
82
+ end
@@ -1,14 +1,13 @@
1
1
  module Yaoc
2
2
  module MappingToClass
3
-
4
3
  def self.included(other)
5
4
  other.send(:include, MappingBase)
6
5
  other.send(:include, InstanceMethods)
7
6
  end
8
7
 
9
8
  module InstanceMethods
10
- def call(pre_created_object=nil)
11
- source_converted_to_hash_or_array = to_hash_or_array()
9
+ def call(pre_created_object = nil)
10
+ source_converted_to_hash_or_array = to_hash_or_array
12
11
  unless source_converted_to_hash_or_array.nil?
13
12
  if pre_created_object.nil?
14
13
  create_target_from_class(source_converted_to_hash_or_array)
@@ -22,21 +21,21 @@ module Yaoc
22
21
  alias_method :to_object, :call
23
22
 
24
23
  def source_method
25
- self.target_source.respond_to?(:call) ? :call : :new
24
+ target_source.respond_to?(:call) ? :call : :new
26
25
  end
27
26
 
28
27
  def create_target_from_class(args)
29
28
  array_based_constructor = args.is_a? Array
30
29
 
31
30
  if array_based_constructor
32
- self.target_source.send(source_method, *args)
31
+ target_source.send(source_method, *args)
33
32
  else
34
- self.target_source.send(source_method, args)
33
+ target_source.send(source_method, args)
35
34
  end
36
35
  end
37
36
 
38
37
  def fill_target_object(attribute_hash, pre_created_object)
39
- raise "UnexpectedStrategy" unless attribute_hash.respond_to? :each_pair
38
+ fail 'UnexpectedStrategy' unless attribute_hash.respond_to? :each_pair
40
39
 
41
40
  attribute_hash.each_pair do |key, value|
42
41
  pre_created_object.send("#{key}=", value)
@@ -49,6 +48,5 @@ module Yaoc
49
48
  [self]
50
49
  end
51
50
  end
52
-
53
51
  end
54
- end
52
+ end
@@ -1,5 +1,4 @@
1
1
  module Yaoc
2
-
3
2
  module MapperDSLMethods
4
3
  def add_mapping(&block)
5
4
  instance_eval &block
@@ -18,8 +17,8 @@ module Yaoc
18
17
  lazy_loading: nil,
19
18
  reverse_lazy_loading: lazy_loading)
20
19
 
21
- object_converter = Array(object_converter).map{|converter| converter.is_a?(Symbol) ? registry.for(converter) : converter}
22
- reverse_object_converter = Array(reverse_object_converter).map{|converter| converter.is_a?(Symbol) ? registry.for(converter) : converter}
20
+ object_converter = Array(object_converter).map { |converter| converter.is_a?(Symbol) ? registry.for(converter) : converter }
21
+ reverse_object_converter = Array(reverse_object_converter).map { |converter| converter.is_a?(Symbol) ? registry.for(converter) : converter }
23
22
 
24
23
  converter_builder.rule(
25
24
  to: to,
@@ -57,13 +56,12 @@ module Yaoc
57
56
  end
58
57
 
59
58
  def noop
60
- ->(_, result){ result }
59
+ ->(_, result) { result }
61
60
  end
62
61
 
63
62
  def register_as(name)
64
63
  registry.add(name, self) unless name.nil?
65
64
  end
66
-
67
65
  end
68
66
 
69
67
  class ObjectMapper
@@ -71,25 +69,25 @@ module Yaoc
71
69
 
72
70
  attr_accessor :load_result_source, :dump_result_source, :registry
73
71
 
74
- def initialize(load_result_source, dump_result_source=nil, registry=Yaoc::MapperRegistry)
72
+ def initialize(load_result_source, dump_result_source = nil, registry = Yaoc::MapperRegistry)
75
73
  self.load_result_source = load_result_source
76
74
  self.dump_result_source = dump_result_source
77
75
  self.registry = registry
78
76
  end
79
77
 
80
- def load(fetch_able, object_to_fill=nil)
78
+ def load(fetch_able, object_to_fill = nil)
81
79
  converter(fetch_able).call(object_to_fill)
82
80
  end
83
81
 
84
- def dump(object, object_to_fill=nil)
82
+ def dump(object, object_to_fill = nil)
85
83
  reverse_converter(object).call(object_to_fill)
86
84
  end
87
85
 
88
- def converter(fetch_able=nil)
86
+ def converter(fetch_able = nil)
89
87
  converter_builder.converter(fetch_able, load_result_source)
90
88
  end
91
89
 
92
- def reverse_converter(fetch_able=nil)
90
+ def reverse_converter(fetch_able = nil)
93
91
  reverse_converter_builder.converter(fetch_able, dump_result_source)
94
92
  end
95
93
 
@@ -113,12 +111,11 @@ module Yaoc
113
111
  end
114
112
 
115
113
  def converter_builder
116
- @converter_builder ||= Yaoc::ConverterBuilder.new()
114
+ @converter_builder ||= Yaoc::ConverterBuilder.new
117
115
  end
118
116
 
119
117
  def reverse_converter_builder
120
118
  @reverse_converter_builder ||= Yaoc::ConverterBuilder.new(:reverse_order, :public_send)
121
119
  end
122
-
123
120
  end
124
- end
121
+ end
@@ -10,7 +10,7 @@ module Yaoc
10
10
  self.converter = converter
11
11
  end
12
12
 
13
- def load_all(input_object, objects_to_fill=[])
13
+ def load_all(input_object, objects_to_fill = [])
14
14
  objects_to_fill = Array(objects_to_fill)
15
15
  results = []
16
16
 
@@ -21,7 +21,7 @@ module Yaoc
21
21
  self.last_result = results
22
22
  end
23
23
 
24
- def dump_all(input_object, objects_to_fill=nil)
24
+ def dump_all(input_object, objects_to_fill = nil)
25
25
  objects_to_fill = Array(objects_to_fill)
26
26
  results = []
27
27
 
@@ -35,17 +35,15 @@ module Yaoc
35
35
  protected
36
36
 
37
37
  def converter=(new_converter)
38
- @converter = new_converter.map{|converter| converter.is_a?(Symbol) ? OneToManyMapperChain.registry.for(converter) : converter}
38
+ @converter = new_converter.map { |converter| converter.is_a?(Symbol) ? OneToManyMapperChain.registry.for(converter) : converter }
39
39
  end
40
40
 
41
41
  def each_object_with_converter(objects_to_fill)
42
-
43
42
  converter.each_with_index do |converter, index|
44
43
  object_to_fill = objects_to_fill[index]
45
44
 
46
45
  yield converter, object_to_fill
47
46
  end
48
47
  end
49
-
50
48
  end
51
- end
49
+ end
@@ -1,7 +1,6 @@
1
1
  module Yaoc
2
2
  module Strategies
3
3
  module ToArrayMapping
4
-
5
4
  def self.call(conv_object)
6
5
  [].tap do |result|
7
6
  conv_object.converter_methods.each do |method_name|
@@ -9,7 +8,6 @@ module Yaoc
9
8
  end
10
9
  end
11
10
  end
12
-
13
11
  end
14
12
  end
15
- end
13
+ end
@@ -1,13 +1,11 @@
1
1
  module Yaoc
2
2
  module Strategies
3
3
  module ToHashMapping
4
-
5
4
  def self.call(conv_object)
6
5
  conv_object.converter_methods.reduce({}) do |result, method_name|
7
6
  conv_object.public_send(method_name, conv_object.to_convert, result)
8
7
  end
9
8
  end
10
-
11
9
  end
12
10
  end
13
- end
11
+ end
@@ -1,7 +1,19 @@
1
1
  module Yaoc
2
2
  class TransformationCommand
3
+ module WithProc
4
+ def call(to_convert, result)
5
+ instance_exec(to_convert, result, &proc)
6
+ end
7
+ end
8
+
9
+ module WithoutProc
10
+ def call(to_convert, result)
11
+ TransformationCommand.fill_result_with_value(result, to, value(to_convert))
12
+ end
13
+ end
3
14
 
4
15
  protected
16
+
5
17
  attr_accessor :to, :from, :fetcher , :proc, :value_fetcher_proc
6
18
 
7
19
  public
@@ -18,13 +30,13 @@ module Yaoc
18
30
  # will be executed in mapper object instance context later through :define_method
19
31
  tc_source = deferred ? deferred_source : default_source
20
32
 
21
- -> (to_convert, result){
33
+ -> (to_convert, result)do
22
34
  tc_source.new(to: to, from: from, fetch_method: fetcher, conversion_proc: conversion_proc, fetcher_proc: fetcher_proc).call(to_convert, result)
23
- }
35
+ end
24
36
  end
25
37
 
26
38
  def self.fill_result_with_value(result, key, value)
27
- result.tap{|taped_result| taped_result[key] = value}
39
+ result.tap { |taped_result| taped_result[key] = value }
28
40
  end
29
41
 
30
42
  def initialize(to: nil, from: nil, fetch_method: nil, conversion_proc: nil, fetcher_proc: nil)
@@ -32,22 +44,29 @@ module Yaoc
32
44
  self.from = from
33
45
  self.proc = conversion_proc
34
46
  self.fetcher = fetch_method
35
- self.value_fetcher_proc = fetcher_proc || ->(to_convert, fetcher, from){ to_convert.public_send(fetcher, from)}
47
+ self.value_fetcher_proc = fetcher_proc || ->(to_convert, fetcher, from) { to_convert.public_send(fetcher, from) }
36
48
  end
37
49
 
38
- def call(to_convert, result)
50
+ def proc=(new_proc)
51
+ @proc = new_proc
52
+
53
+ if new_proc.nil?
54
+ extend WithoutProc
55
+ else
56
+ extend WithProc
57
+ end
58
+ end
39
59
 
60
+ def call(to_convert, result)
40
61
  unless proc.nil?
41
62
  instance_exec(to_convert, result, &proc)
42
63
  else
43
64
  TransformationCommand.fill_result_with_value(result, to, value(to_convert))
44
65
  end
45
-
46
66
  end
47
67
 
48
68
  def value(to_convert)
49
69
  value_fetcher_proc.call(to_convert, fetcher, from)
50
70
  end
51
-
52
71
  end
53
- end
72
+ end