trailblazer-finder 0.1.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop-https---raw-githubusercontent-com-trailblazer-meta-master-rubocop-yml +101 -0
  4. data/.rubocop.yml +3 -43
  5. data/.rubocop_todo.yml +42 -196
  6. data/.travis.yml +5 -8
  7. data/Gemfile +2 -2
  8. data/LICENSE.txt +7 -21
  9. data/README.md +177 -292
  10. data/Rakefile +12 -12
  11. data/lib/trailblazer/finder.rb +48 -23
  12. data/lib/trailblazer/finder/activity/find.rb +57 -0
  13. data/lib/trailblazer/finder/activity/prepare.rb +29 -0
  14. data/lib/trailblazer/finder/activity/prepare/adapters.rb +66 -0
  15. data/lib/trailblazer/finder/activity/prepare/entity.rb +32 -0
  16. data/lib/trailblazer/finder/activity/prepare/filters.rb +36 -0
  17. data/lib/trailblazer/finder/activity/prepare/paging.rb +49 -0
  18. data/lib/trailblazer/finder/activity/prepare/params.rb +33 -0
  19. data/lib/trailblazer/finder/activity/prepare/properties.rb +47 -0
  20. data/lib/trailblazer/finder/activity/prepare/sorting.rb +57 -0
  21. data/lib/trailblazer/finder/activity/process.rb +18 -0
  22. data/lib/trailblazer/finder/activity/process/adapters.rb +48 -0
  23. data/lib/trailblazer/finder/activity/process/filters.rb +28 -0
  24. data/lib/trailblazer/finder/activity/process/paging.rb +26 -0
  25. data/lib/trailblazer/finder/activity/process/predicates.rb +30 -0
  26. data/lib/trailblazer/finder/activity/process/sorting.rb +26 -0
  27. data/lib/trailblazer/finder/adapters/active_record/paging.rb +9 -8
  28. data/lib/trailblazer/finder/adapters/active_record/predicates.rb +78 -17
  29. data/lib/trailblazer/finder/adapters/active_record/sorting.rb +14 -12
  30. data/lib/trailblazer/finder/adapters/basic/paging.rb +21 -0
  31. data/lib/trailblazer/finder/adapters/basic/predicates.rb +115 -0
  32. data/lib/trailblazer/finder/adapters/basic/sorting.rb +40 -0
  33. data/lib/trailblazer/finder/adapters/kaminari/paging.rb +21 -0
  34. data/lib/trailblazer/finder/adapters/sequel/paging.rb +9 -8
  35. data/lib/trailblazer/finder/adapters/sequel/predicates.rb +78 -17
  36. data/lib/trailblazer/finder/adapters/sequel/sorting.rb +17 -15
  37. data/lib/trailblazer/finder/adapters/will_paginate/paging.rb +21 -0
  38. data/lib/trailblazer/finder/base.rb +20 -83
  39. data/lib/trailblazer/finder/dsl.rb +56 -0
  40. data/lib/trailblazer/finder/find.rb +28 -15
  41. data/lib/trailblazer/finder/helpers/basic.rb +44 -0
  42. data/lib/trailblazer/finder/helpers/sorting.rb +54 -0
  43. data/lib/trailblazer/finder/utils/array.rb +20 -0
  44. data/lib/trailblazer/finder/utils/extra.rb +10 -20
  45. data/lib/trailblazer/finder/utils/hash.rb +36 -0
  46. data/lib/trailblazer/finder/utils/splitter.rb +16 -17
  47. data/lib/trailblazer/finder/utils/string.rb +20 -10
  48. data/lib/trailblazer/finder/version.rb +3 -1
  49. data/lib/trailblazer/operation/finder.rb +20 -19
  50. data/spec/spec_helper.rb +5 -11
  51. data/spec/spec_helper_active_record.rb +11 -12
  52. data/spec/spec_helper_sequel.rb +4 -4
  53. data/spec/spec_helper_will_paginate.rb +13 -0
  54. data/spec/trailblazer/finder/adapters/active_record_spec.rb +267 -0
  55. data/spec/trailblazer/finder/adapters/basic_spec.rb +251 -0
  56. data/spec/trailblazer/finder/adapters/kaminari_spec.rb +78 -32
  57. data/spec/trailblazer/finder/adapters/sequel_spec.rb +267 -0
  58. data/spec/trailblazer/finder/adapters/will_paginate_spec.rb +81 -40
  59. data/spec/trailblazer/finder/base_spec.rb +292 -204
  60. data/spec/trailblazer/finder/dsl_spec.rb +213 -0
  61. data/spec/trailblazer/finder/utils/hash_spec.rb +26 -0
  62. data/spec/trailblazer/finder/utils/splitter_spec.rb +39 -0
  63. data/spec/trailblazer/finder/utils/string_spec.rb +52 -9
  64. data/spec/trailblazer/operation/finder_spec.rb +35 -32
  65. data/trailblazer-finder.gemspec +31 -30
  66. metadata +119 -137
  67. data/lib/trailblazer/finder/adapters.rb +0 -26
  68. data/lib/trailblazer/finder/adapters/active_record.rb +0 -32
  69. data/lib/trailblazer/finder/adapters/data_mapper.rb +0 -32
  70. data/lib/trailblazer/finder/adapters/data_mapper/paging.rb +0 -20
  71. data/lib/trailblazer/finder/adapters/data_mapper/predicates.rb +0 -42
  72. data/lib/trailblazer/finder/adapters/data_mapper/sorting.rb +0 -38
  73. data/lib/trailblazer/finder/adapters/friendly_id.rb +0 -31
  74. data/lib/trailblazer/finder/adapters/kaminari.rb +0 -18
  75. data/lib/trailblazer/finder/adapters/sequel.rb +0 -32
  76. data/lib/trailblazer/finder/adapters/will_paginate.rb +0 -18
  77. data/lib/trailblazer/finder/errors/block_ignored.rb +0 -11
  78. data/lib/trailblazer/finder/errors/invalid_defined_by_value.rb +0 -11
  79. data/lib/trailblazer/finder/errors/invalid_number.rb +0 -16
  80. data/lib/trailblazer/finder/errors/missing_entity_type.rb +0 -11
  81. data/lib/trailblazer/finder/errors/with_ignored.rb +0 -11
  82. data/lib/trailblazer/finder/features.rb +0 -23
  83. data/lib/trailblazer/finder/features/paging.rb +0 -55
  84. data/lib/trailblazer/finder/features/predicate.rb +0 -32
  85. data/lib/trailblazer/finder/features/sorting.rb +0 -108
  86. data/lib/trailblazer/finder/filter.rb +0 -66
  87. data/lib/trailblazer/finder/predicates.rb +0 -39
  88. data/lib/trailblazer/finder/utils/deep_locate.rb +0 -30
  89. data/lib/trailblazer/finder/utils/params.rb +0 -28
  90. data/lib/trailblazer/finder/utils/parse.rb +0 -25
  91. data/spec/spec_helper_data_mapper.rb +0 -35
  92. data/spec/support/paging_shared_example.rb +0 -65
  93. data/spec/support/sorting_shared_example.rb +0 -125
  94. data/spec/trailblazer/finder/adapters/active_record/base_spec.rb +0 -112
  95. data/spec/trailblazer/finder/adapters/active_record/paging_spec.rb +0 -64
  96. data/spec/trailblazer/finder/adapters/active_record/predicates_spec.rb +0 -101
  97. data/spec/trailblazer/finder/adapters/active_record/sorting_spec.rb +0 -99
  98. data/spec/trailblazer/finder/adapters/data_mapper/base_spec.rb +0 -112
  99. data/spec/trailblazer/finder/adapters/data_mapper/paging_spec.rb +0 -64
  100. data/spec/trailblazer/finder/adapters/data_mapper/predicates_spec.rb +0 -100
  101. data/spec/trailblazer/finder/adapters/data_mapper/sorting_spec.rb +0 -97
  102. data/spec/trailblazer/finder/adapters/friendly_id_spec.rb +0 -46
  103. data/spec/trailblazer/finder/adapters/sequel/base_spec.rb +0 -125
  104. data/spec/trailblazer/finder/adapters/sequel/paging_spec.rb +0 -64
  105. data/spec/trailblazer/finder/adapters/sequel/predicates_spec.rb +0 -89
  106. data/spec/trailblazer/finder/adapters/sequel/sorting_spec.rb +0 -110
  107. data/spec/trailblazer/finder/adapters_spec.rb +0 -110
  108. data/spec/trailblazer/finder/features/paging_spec.rb +0 -104
  109. data/spec/trailblazer/finder/features/predicates_spec.rb +0 -99
  110. data/spec/trailblazer/finder/features/sorting_spec.rb +0 -96
  111. data/spec/trailblazer/finder/features_spec.rb +0 -55
  112. data/spec/trailblazer/finder/filter_spec.rb +0 -133
  113. data/spec/trailblazer/finder/find_spec.rb +0 -72
  114. data/spec/trailblazer/finder/utils/extra_spec.rb +0 -41
  115. data/spec/trailblazer/finder/utils/params_spec.rb +0 -39
  116. data/spec/trailblazer/finder/utils/parse_spec.rb +0 -33
  117. data/spec/trailblazer/operation/paging_spec.rb +0 -68
  118. data/spec/trailblazer/operation/predicates_spec.rb +0 -127
  119. data/spec/trailblazer/operation/sorting_spec.rb +0 -80
  120. data/spec/trailblazer/test_spec.rb +0 -41
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trailblazer
4
+ class Finder
5
+ module Adapters
6
+ # ActiveRecord Adapter
7
+ module Basic
8
+ # Basic Predicates Adapter
9
+ module Predicates
10
+ module_function
11
+
12
+ def set_eq_handler
13
+ lambda do |entity, attribute, value|
14
+ return if Utils::String.blank?(value.to_s) || Utils::String.blank?(attribute.to_s)
15
+
16
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && v.to_s == value.to_s && !v.nil? }, entity
17
+ end
18
+ end
19
+
20
+ def set_not_eq_handler
21
+ lambda do |entity, attribute, value|
22
+ return if Utils::String.blank?(value.to_s)
23
+
24
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && v.to_s != value.to_s && !v.nil? }, entity
25
+ end
26
+ end
27
+
28
+ def set_blank_handler
29
+ lambda do |entity, attribute, _value|
30
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && Utils::String.blank?(v.to_s) }, entity
31
+ end
32
+ end
33
+
34
+ def set_not_blank_handler
35
+ lambda do |entity, attribute, _value|
36
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && !Utils::String.blank?(v.to_s) }, entity
37
+ end
38
+ end
39
+
40
+ def set_gt_handler
41
+ lambda do |entity, attribute, value|
42
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && v.to_f > value.to_f }, entity
43
+ end
44
+ end
45
+
46
+ def set_gte_handler
47
+ lambda do |entity, attribute, value|
48
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && v.to_f >= value.to_f }, entity
49
+ end
50
+ end
51
+
52
+ def set_lt_handler
53
+ lambda do |entity, attribute, value|
54
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && v.to_f < value.to_f }, entity
55
+ end
56
+ end
57
+
58
+ def set_lte_handler
59
+ lambda do |entity, attribute, value|
60
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && v.to_i <= value.to_f }, entity
61
+ end
62
+ end
63
+
64
+ def set_cont_handler
65
+ lambda do |entity, attribute, value|
66
+ return if Utils::String.blank?(value.to_s) || Utils::String.blank?(attribute.to_s)
67
+
68
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && v.to_s.include?(value.to_s) && !v.nil? }, entity
69
+ end
70
+ end
71
+
72
+ def set_not_cont_handler
73
+ lambda do |entity, attribute, value|
74
+ return if Utils::String.blank?(value.to_s) || Utils::String.blank?(attribute.to_s)
75
+
76
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && !v.to_s.include?(value.to_s) && !v.nil? }, entity
77
+ end
78
+ end
79
+
80
+ def set_sw_handler
81
+ lambda do |entity, attribute, value|
82
+ return if Utils::String.blank?(value.to_s) || Utils::String.blank?(attribute.to_s)
83
+
84
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && v.to_s.start_with?(value.to_s) && !v.nil? }, entity
85
+ end
86
+ end
87
+
88
+ def set_not_sw_handler
89
+ lambda do |entity, attribute, value|
90
+ return if Utils::String.blank?(value.to_s) || Utils::String.blank?(attribute.to_s)
91
+
92
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && !v.to_s.start_with?(value.to_s) && !v.nil? }, entity
93
+ end
94
+ end
95
+
96
+ def set_ew_handler
97
+ lambda do |entity, attribute, value|
98
+ return if Utils::String.blank?(value.to_s) || Utils::String.blank?(attribute.to_s)
99
+
100
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && v.to_s.end_with?(value.to_s) && !v.nil? }, entity
101
+ end
102
+ end
103
+
104
+ def set_not_ew_handler
105
+ lambda do |entity, attribute, value|
106
+ return if Utils::String.blank?(value.to_s) || Utils::String.blank?(attribute.to_s)
107
+
108
+ Utils::Hash.deep_locate ->(k, v, _) { k == attribute.to_sym && !v.to_s.end_with?(value.to_s) && !v.nil? }, entity
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trailblazer
4
+ class Finder
5
+ module Adapters
6
+ # ActiveRecord Adapter
7
+ module Basic
8
+ # Basic Paging Adapter
9
+ module Sorting
10
+ module_function
11
+
12
+ def set_sorting_handler
13
+ lambda do |sort_attributes, entity|
14
+ sort_attributes.delete(:handler)
15
+ attributes = []
16
+ sort_attributes.each do |attr|
17
+ attributes << [attr[0].to_sym, (attr[1] == :asc ? 1 : -1)]
18
+ end
19
+ entity.sort do |this, that|
20
+ attributes.reduce(0) do |diff, order|
21
+ next diff if diff != 0 # this and that have differed at an earlier order entry
22
+
23
+ key, direction = order
24
+ # deal with nil cases
25
+ next 0 if this[key].nil? && that[key].nil?
26
+ next 1 if this[key].nil?
27
+ next -1 if that[key].nil?
28
+
29
+ # do the actual comparison
30
+ comparison = this[key] <=> that[key]
31
+ comparison * direction
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trailblazer
4
+ class Finder
5
+ module Adapters
6
+ # ActiveRecord Adapter
7
+ module Kaminari
8
+ # Kaminari Paging Adapter
9
+ module Paging
10
+ module_function
11
+
12
+ def set_paging_handler
13
+ lambda do |current_page, per_page, entity|
14
+ entity.page(current_page).per(per_page)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,17 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Trailblazer
2
4
  class Finder
3
5
  module Adapters
6
+ # ActiveRecord Adapter
4
7
  module Sequel
5
- # Sequel - Paging Adapter
8
+ # ActiveRecord Paging Adapter
6
9
  module Paging
7
- def self.included(base)
8
- base.extend Features::Paging::ClassMethods
9
- end
10
-
11
- private
10
+ module_function
12
11
 
13
- def apply_paging(entity_type)
14
- entity_type.limit(per_page).offset(([page, 1].max - 1) * per_page)
12
+ def set_paging_handler
13
+ lambda do |current_page, per_page, entity|
14
+ entity.limit(per_page).offset(([current_page, 1].max - 1) * per_page)
15
+ end
15
16
  end
16
17
  end
17
18
  end
@@ -1,40 +1,101 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Trailblazer
2
4
  class Finder
3
5
  module Adapters
6
+ # ActiveRecord Adapter
4
7
  module Sequel
5
- # Sequel - Predicates Adapter
8
+ # ActiveRecord Predicates Adapter
6
9
  module Predicates
10
+ module_function
11
+
12
+ def set_eq_handler
13
+ lambda do |entity, attribute, value|
14
+ return if Utils::String.blank?(value.to_s)
15
+
16
+ entity.where { ::Sequel[attribute.to_sym] =~ value }
17
+ end
18
+ end
19
+
20
+ def set_not_eq_handler
21
+ lambda do |entity, attribute, value|
22
+ return if Utils::String.blank?(value.to_s)
23
+
24
+ entity.where { ::Sequel[attribute.to_sym] !~ value }
25
+ end
26
+ end
27
+
28
+ def set_blank_handler
29
+ lambda do |entity, attribute, _value|
30
+ entity.where { ::Sequel.|({::Sequel[attribute.to_sym] => nil}, {::Sequel[attribute.to_sym] => ""}) }
31
+ end
32
+ end
33
+
34
+ def set_not_blank_handler
35
+ lambda do |entity, attribute, _value|
36
+ entity.exclude(::Sequel[attribute.to_sym] => nil, ::Sequel[attribute.to_sym] => "")
37
+ # entity.exclude(attribute.to_sym => nil).exclude(attribute.to_sym => "")
38
+ end
39
+ end
40
+
41
+ def set_gt_handler
42
+ lambda do |entity, attribute, value|
43
+ entity.where { ::Sequel[attribute.to_sym] > value.to_f }
44
+ end
45
+ end
46
+
47
+ def set_gte_handler
48
+ lambda do |entity, attribute, value|
49
+ entity.where { ::Sequel[attribute.to_sym] >= value.to_f }
50
+ end
51
+ end
7
52
 
8
- def not_eq(attribute, value, entity_type)
9
- entity_type.exclude(attribute.to_sym => value.to_s)
53
+ def set_lt_handler
54
+ lambda do |entity, attribute, value|
55
+ entity.where { ::Sequel[attribute.to_sym] < value.to_f }
56
+ end
10
57
  end
11
58
 
12
- def eq(attribute, value, entity_type)
13
- entity_type.where(attribute.to_sym => value.to_s)
59
+ def set_lte_handler
60
+ lambda do |entity, attribute, value|
61
+ entity.where { ::Sequel[attribute.to_sym] <= value.to_f }
62
+ end
14
63
  end
15
64
 
16
- def blank(attribute, value, entity_type)
17
- entity_type.where(attribute.to_sym => nil).or(attribute.to_sym => "")
65
+ def set_cont_handler
66
+ lambda do |entity, attribute, value|
67
+ entity.where(::Sequel.like(attribute.to_sym, "%#{value}%"))
68
+ end
18
69
  end
19
70
 
20
- def not_blank(attribute, value, entity_type)
21
- entity_type.exclude(attribute.to_sym => nil).exclude(attribute.to_sym => "")
71
+ def set_not_cont_handler
72
+ lambda do |entity, attribute, value|
73
+ entity.where(~::Sequel.like(attribute.to_sym, "%#{value}%"))
74
+ end
22
75
  end
23
76
 
24
- def gt(attribute, value, entity_type)
25
- entity_type.where{::Sequel[attribute.to_sym] > value.to_f}
77
+ def set_sw_handler
78
+ lambda do |entity, attribute, value|
79
+ entity.where(::Sequel.like(attribute.to_sym, "#{value}%"))
80
+ end
26
81
  end
27
82
 
28
- def gte(attribute, value, entity_type)
29
- entity_type.where{::Sequel[attribute.to_sym] >= value.to_f}
83
+ def set_not_sw_handler
84
+ lambda do |entity, attribute, value|
85
+ entity.where(~::Sequel.like(attribute.to_sym, "#{value}%"))
86
+ end
30
87
  end
31
88
 
32
- def lt(attribute, value, entity_type)
33
- entity_type.where{::Sequel[attribute.to_sym] < value.to_f}
89
+ def set_ew_handler
90
+ lambda do |entity, attribute, value|
91
+ entity.where(::Sequel.like(attribute.to_sym, "%#{value}"))
92
+ end
34
93
  end
35
94
 
36
- def lte(attribute, value, entity_type)
37
- entity_type.where{::Sequel[attribute.to_sym] <= value.to_f}
95
+ def set_not_ew_handler
96
+ lambda do |entity, attribute, value|
97
+ entity.where(~::Sequel.like(attribute.to_sym, "%#{value}"))
98
+ end
38
99
  end
39
100
  end
40
101
  end
@@ -1,24 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Trailblazer
2
4
  class Finder
3
5
  module Adapters
6
+ # ActiveRecord Adapter
4
7
  module Sequel
5
- # Sequel - Sorting Adapter
8
+ # ActiveRecord Paging Adapter
6
9
  module Sorting
7
- def self.included(base)
8
- base.extend Features::Sorting::ClassMethods
9
- end
10
-
11
- private
12
-
13
- def sort_orders(sort_attr, sort_dir)
14
- ::Sequel.send sort_dir, sort_attr.to_sym
15
- end
10
+ module_function
16
11
 
17
- def sort_it(entity_type, sort_attributes)
18
- result = []
19
- result << [:order, sort_attributes.first] if sort_attributes.is_a? Array
20
- sort_attributes.drop(1).each { |x| result << [:order_append, x] }
21
- result.inject(entity_type) { |obj, method_and_args| obj.send(*method_and_args) }
12
+ def set_sorting_handler
13
+ lambda do |sort_attributes, entity|
14
+ sort_attributes.delete(:handler)
15
+ attributes = []
16
+ sort_attributes.each do |attr|
17
+ attributes << (::Sequel.send attr[1], attr[0].to_sym)
18
+ end
19
+ result = []
20
+ result << [:order, attributes.first] if attributes.is_a? Array
21
+ attributes.drop(1).each { |x| result << [:order_append, x] }
22
+ result.reduce(entity) { |obj, method_and_args| obj.send(*method_and_args) }
23
+ end
22
24
  end
23
25
  end
24
26
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trailblazer
4
+ class Finder
5
+ module Adapters
6
+ # ActiveRecord Adapter
7
+ module WillPaginate
8
+ # Kaminari Paging Adapter
9
+ module Paging
10
+ module_function
11
+
12
+ def set_paging_handler
13
+ lambda do |current_page, per_page, entity|
14
+ entity.paginate per_page: per_page, page: current_page.zero? ? nil : current_page
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,95 +1,32 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Trailblazer
2
4
  class Finder
3
- # Base module
5
+ ORM_ADAPTERS = %w[ActiveRecord Sequel].freeze
6
+ PAGING_ADAPTERS = %w[Kaminari WillPaginate].freeze
7
+
4
8
  module Base
5
9
  def self.included(base)
6
- base.extend ClassMethods
7
- base.instance_eval do
8
- @config = {
9
- defaults: {},
10
- actions: {},
11
- entity_type: nil
12
- }
13
- end
14
- end
15
-
16
- def initialize(options = {})
17
- config = self.class.config
18
- entity_type = options[:entity_type] || (config[:entity_type] && instance_eval(&config[:entity_type]))
19
- actions = config[:actions] || {}
20
- params = Utils::Params.normalize_params(config[:defaults], options[:filter], actions.keys)
21
-
22
- raise Error::MissingEntityType unless entity_type
23
-
24
- @find = Find.new(entity_type, params, actions)
25
- end
26
-
27
- def results
28
- @results ||= fetch_results
29
- end
30
-
31
- def results?
32
- results.any?
33
- end
34
-
35
- def count
36
- @count ||= @find.count self
37
- end
38
-
39
- def params(additions = {})
40
- if additions.empty?
41
- @find.params
42
- else
43
- @find.params.merge Utils::Params.stringify_keys(additions)
44
- end
10
+ base.include Helpers::Basic
11
+ base.include Helpers::Sorting
12
+ base.extend Finder::Dsl
45
13
  end
46
14
 
47
- private
15
+ attr_reader :signal, :errors
48
16
 
49
- def fetch_results
50
- @find.query self
17
+ def initialize(options = {}) # rubocop:disable Style/OptionHash
18
+ config = self.class.config
19
+ ctx = {config: config, options: options}
20
+ @signal, (ctx, *) = Activity::Find.call([ctx, {}])
21
+ @options = options
22
+ @errors = ctx[:errors] || {}
23
+ @find = ctx[:finder]
51
24
  end
52
25
 
53
- # ClassMethods
54
- module ClassMethods
55
- attr_reader :config
56
-
57
- def inherited(base)
58
- base.instance_variable_set '@config', Utils::Extra.deep_copy(config)
59
- end
60
-
61
- def entity_type(&block)
62
- config[:entity_type] = block
63
- end
64
-
65
- def filter_by(name, options = nil, &block)
66
- options = { default: options } unless options.is_a?(Hash)
67
-
68
- name = name.to_s
69
- default = options[:default]
70
- handler = options[:with] || block
71
-
72
- config[:defaults][name] = default unless default.nil?
73
- config[:actions][name] = normalize_find_handler(handler, name)
74
-
75
- define_method(name) { @find.param name }
76
- end
77
-
78
- def results(*args)
79
- new(*args).results
80
- end
81
-
82
- def normalize_find_handler(handler, key)
83
- case handler
84
- when Symbol then ->(entity_type, value) { method(handler).call entity_type, value }
85
- when Proc then handler
86
- else
87
- lambda do |entity_type, value|
88
- return if Utils::String.blank?(value)
89
- Utils::DeepLocate.deep_locate ->(k, v, _object) { k == key.to_sym && v == value }, entity_type
90
- end
91
- end
92
- end
26
+ def fetch_result
27
+ result = @find.query self
28
+ result = Utils::Array.convert_hashes_in_array_to_struct(result) if result.first.is_a?(Hash)
29
+ result
93
30
  end
94
31
  end
95
32
  end