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,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trailblazer
4
+ class Finder
5
+ module Activity
6
+ module Prepare
7
+ # Paging Activity
8
+ module Sorting
9
+ extend Trailblazer::Activity::Railway()
10
+
11
+ module_function
12
+
13
+ def check_sorting(ctx, **)
14
+ sorting = ctx[:config][:sorting] || nil
15
+ return true unless ctx[:config][:sorting].empty? || sorting.nil?
16
+ end
17
+
18
+ def set_sorting(ctx, **)
19
+ return true if ctx[:params][:sort].nil?
20
+
21
+ sorting = ctx[:params][:sort]
22
+ config = ctx[:config][:sorting]
23
+ ctx[:sorting] = ctx[:sorting] || {}
24
+ sorting.split(",").each do |sorter|
25
+ spt = sorter.split
26
+ ctx[:sorting][spt[0]] = fetch_sort_direction(config[spt[0].to_sym], spt[1]) if config.include?(spt[0].to_sym)
27
+ end
28
+ end
29
+
30
+ def fetch_sort_direction(config_direction, params_direction = nil)
31
+ return config_direction == :asc ? :asc : :desc if params_direction.nil?
32
+
33
+ case params_direction
34
+ when ":asc", :asc, "asc" then :asc
35
+ else :desc
36
+ end
37
+ end
38
+
39
+ def clear_sorting(ctx, **)
40
+ ctx[:params].delete(:sort) unless ctx[:params][:sort].nil?
41
+ true
42
+ end
43
+
44
+ step method(:check_sorting), id: :check_sorting,
45
+ Output(:success) => Track(:paging),
46
+ Output(:failure) => Track(:end_sorting)
47
+ step method(:set_sorting), id: :set_sorting, magnetic_to: [:paging],
48
+ Output(:success) => Track(:end_sorting),
49
+ Output(:failure) => Track(:failure)
50
+ step method(:clear_sorting), id: :clear_sorting, magnetic_to: [:end_sorting],
51
+ Output(:success) => Track(:success),
52
+ Output(:failure) => Track(:failure)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trailblazer
4
+ class Finder
5
+ module Activity
6
+ # Process Activity
7
+ module Process
8
+ extend Trailblazer::Activity::Railway()
9
+
10
+ step Subprocess(Process::Adapters), id: :process_adapters
11
+ step Subprocess(Process::Predicates), id: :process_predicates
12
+ step Subprocess(Process::Filters), id: :process_filters
13
+ step Subprocess(Process::Paging), id: :process_paging
14
+ step Subprocess(Process::Sorting), id: :process_sorting
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trailblazer
4
+ class Finder
5
+ module Activity
6
+ module Process
7
+ # Process Adapters Activity
8
+ module Adapters
9
+ extend Trailblazer::Activity::Railway()
10
+
11
+ module_function
12
+
13
+ def set_orm_adapters(ctx, **)
14
+ ctx[:adapters].each do |adapter|
15
+ next unless (ORM_ADAPTERS + ["Basic"]).include?(adapter)
16
+
17
+ ctx[:orm] = {}
18
+ ctx[:orm][:adapter] = adapter
19
+ ctx[:orm][:predicates] = "Trailblazer::Finder::Adapters::#{adapter}::Predicates"
20
+ ctx[:orm][:paging] = "Trailblazer::Finder::Adapters::#{adapter}::Paging"
21
+ ctx[:orm][:sorting] = "Trailblazer::Finder::Adapters::#{adapter}::Sorting"
22
+ return true
23
+ end
24
+ end
25
+
26
+ def set_paging_adapters(ctx, **)
27
+ ctx[:adapters].each do |adapter|
28
+ next unless PAGING_ADAPTERS.include?(adapter)
29
+ return false if ctx[:adapters].include?("Basic")
30
+
31
+ ctx[:orm][:paging] = "Trailblazer::Finder::Adapters::#{adapter}::Paging"
32
+ return true
33
+ end
34
+ true
35
+ end
36
+
37
+ def invalid_paging_adapter_error(ctx, **)
38
+ (ctx[:errors] ||= []) << {adapters: "Can't use paging adapters like Kaminari without using an ORM like ActiveRecord or Sequel"}
39
+ end
40
+
41
+ step method(:set_orm_adapters)
42
+ step method(:set_paging_adapters)
43
+ fail method(:invalid_paging_adapter_error)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trailblazer
4
+ class Finder
5
+ module Activity
6
+ module Process
7
+ # Process Filters Activity
8
+ module Filters
9
+ extend Trailblazer::Activity::Railway()
10
+
11
+ module_function
12
+
13
+ def set_filter_handlers(ctx, **)
14
+ return true unless ctx[:process]
15
+
16
+ ctx[:process].each do |key, value|
17
+ next if ctx[:process][key][:filter].nil?
18
+
19
+ ctx[:process][key][:handler] = Utils::Extra.apply_handler(value[:filter] ? value[:filter][:handler] : "none")
20
+ end
21
+ end
22
+
23
+ step method(:set_filter_handlers), id: :set_filter_handlers
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trailblazer
4
+ class Finder
5
+ module Activity
6
+ module Process
7
+ # Process Paging Activity
8
+ module Paging
9
+ extend Trailblazer::Activity::Railway()
10
+
11
+ module_function
12
+
13
+ def set_paging_handler(ctx, **)
14
+ return true if ctx[:paging].nil?
15
+
16
+ ctx[:paging][:handler] = Utils::Extra.apply_handler(
17
+ (Object.const_get(ctx[:orm][:paging]).__send__ :set_paging_handler)
18
+ )
19
+ end
20
+
21
+ step method(:set_paging_handler), id: :set_paging_handler
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trailblazer
4
+ class Finder
5
+ module Activity
6
+ module Process
7
+ # Process Predicates Activity
8
+ module Predicates
9
+ extend Trailblazer::Activity::Railway()
10
+
11
+ module_function
12
+
13
+ def set_properties_handler(ctx, **)
14
+ return true if ctx[:process].nil?
15
+
16
+ ctx[:process].each do |key, _value|
17
+ next if ctx[:process][key][:predicate].nil?
18
+
19
+ ctx[:process][key][:handler] = Utils::Extra.apply_handler(
20
+ (Object.const_get(ctx[:orm][:predicates]).__send__ "set_#{ctx[:process][key][:predicate]}_handler".to_sym)
21
+ )
22
+ end
23
+ end
24
+
25
+ step method(:set_properties_handler), id: :set_properties_handler
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trailblazer
4
+ class Finder
5
+ module Activity
6
+ module Process
7
+ # Process sorting Activity
8
+ module Sorting
9
+ extend Trailblazer::Activity::Railway()
10
+
11
+ module_function
12
+
13
+ def set_sorting_handler(ctx, **)
14
+ return true if ctx[:sorting].nil?
15
+
16
+ ctx[:sorting][:handler] = Utils::Extra.apply_handler(
17
+ (Object.const_get(ctx[:orm][:sorting]).__send__ :set_sorting_handler)
18
+ )
19
+ end
20
+
21
+ step method(:set_sorting_handler), id: :set_sorting_handler
22
+ end
23
+ end
24
+ end
25
+ end
26
+ 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 ActiveRecord
5
- # ActiveRecord - 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,39 +1,100 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Trailblazer
2
4
  class Finder
3
5
  module Adapters
6
+ # ActiveRecord Adapter
4
7
  module ActiveRecord
5
- # ActiveRecord - Predicate Adapter
8
+ # ActiveRecord Predicates Adapter
6
9
  module Predicates
7
- def not_eq(attribute, value, entity_type)
8
- entity_type.where.not(attribute => value)
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(attribute => 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.not(attribute => value)
25
+ end
26
+ end
27
+
28
+ def set_blank_handler
29
+ lambda do |entity, attribute, _value|
30
+ entity.where(attribute.to_sym => [nil, ""])
31
+ end
32
+ end
33
+
34
+ def set_not_blank_handler
35
+ lambda do |entity, attribute, _value|
36
+ entity.where.not(attribute.to_sym => [nil, ""])
37
+ end
38
+ end
39
+
40
+ def set_gt_handler
41
+ lambda do |entity, attribute, value|
42
+ entity.where("#{attribute} > ?", value.to_f)
43
+ end
44
+ end
45
+
46
+ def set_gte_handler
47
+ lambda do |entity, attribute, value|
48
+ entity.where("#{attribute} >= ?", value.to_f)
49
+ end
50
+ end
51
+
52
+ def set_lt_handler
53
+ lambda do |entity, attribute, value|
54
+ entity.where("#{attribute} < ?", value.to_f)
55
+ end
9
56
  end
10
57
 
11
- def eq(attribute, value, entity_type)
12
- entity_type.where(attribute => value)
58
+ def set_lte_handler
59
+ lambda do |entity, attribute, value|
60
+ entity.where("#{attribute} <= ?", value.to_f)
61
+ end
13
62
  end
14
63
 
15
- def blank(attribute, _value, entity_type)
16
- entity_type.where(attribute.to_sym => [nil, ''])
64
+ def set_cont_handler
65
+ lambda do |entity, attribute, value|
66
+ entity.where("#{attribute} LIKE ?", "%#{value}%")
67
+ end
17
68
  end
18
69
 
19
- def not_blank(attribute, _value, entity_type)
20
- entity_type.where.not(attribute.to_sym => [nil, ''])
70
+ def set_not_cont_handler
71
+ lambda do |entity, attribute, value|
72
+ entity.where("#{attribute} NOT LIKE ?", "%#{value}%")
73
+ end
21
74
  end
22
75
 
23
- def gt(attribute, value, entity_type)
24
- entity_type.where(["#{attribute} > ?", value.to_f])
76
+ def set_sw_handler
77
+ lambda do |entity, attribute, value|
78
+ entity.where("#{attribute} LIKE ?", "#{value}%")
79
+ end
25
80
  end
26
81
 
27
- def gte(attribute, value, entity_type)
28
- entity_type.where(["#{attribute} >= ?", value.to_f])
82
+ def set_not_sw_handler
83
+ lambda do |entity, attribute, value|
84
+ entity.where("#{attribute} NOT LIKE ?", "#{value}%")
85
+ end
29
86
  end
30
87
 
31
- def lt(attribute, value, entity_type)
32
- entity_type.where(["#{attribute} < ?", value.to_f])
88
+ def set_ew_handler
89
+ lambda do |entity, attribute, value|
90
+ entity.where("#{attribute} LIKE ?", "%#{value}")
91
+ end
33
92
  end
34
93
 
35
- def lte(attribute, value, entity_type)
36
- entity_type.where(["#{attribute} <= ?", value.to_f])
94
+ def set_not_ew_handler
95
+ lambda do |entity, attribute, value|
96
+ entity.where("#{attribute} NOT LIKE ?", "%#{value}")
97
+ end
37
98
  end
38
99
  end
39
100
  end
@@ -1,21 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Trailblazer
2
4
  class Finder
3
5
  module Adapters
6
+ # ActiveRecord Adapter
4
7
  module ActiveRecord
5
- # ActiveRecord - 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_it(entity_type, sort_attributes)
14
- entity_type.order(sort_attributes)
15
- end
10
+ module_function
16
11
 
17
- def sort_orders(sort_attr, sort_dir)
18
- { sort_attr.to_s => sort_dir }
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_s => attr[1]}
18
+ end
19
+ entity.order(attributes)
20
+ end
19
21
  end
20
22
  end
21
23
  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 Basic
8
+ # Basic Paging Adapter
9
+ module Paging
10
+ module_function
11
+
12
+ def set_paging_handler
13
+ lambda do |current_page, per_page, entity|
14
+ entity.drop(([current_page, 1].max - 1) * per_page).first(per_page)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end