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
@@ -1,26 +0,0 @@
1
- require 'trailblazer/finder/adapters/active_record'
2
- require 'trailblazer/finder/adapters/sequel'
3
- require 'trailblazer/finder/adapters/data_mapper'
4
- require 'trailblazer/finder/adapters/kaminari'
5
- require 'trailblazer/finder/adapters/will_paginate'
6
- require 'trailblazer/finder/adapters/friendly_id'
7
-
8
- module Trailblazer
9
- class Finder
10
- # Adapters
11
- module Adapters
12
- def self.included(base)
13
- base.extend ClassMethods
14
- end
15
-
16
- # ClassMethods
17
- module ClassMethods
18
- def adapters(*mods)
19
- mods.each do |mod|
20
- include mod
21
- end
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,32 +0,0 @@
1
- require 'trailblazer/finder/adapters/active_record/paging'
2
- require 'trailblazer/finder/adapters/active_record/sorting'
3
- require 'trailblazer/finder/adapters/active_record/predicates'
4
-
5
- module Trailblazer
6
- class Finder
7
- module Adapters
8
- # ActiveRecord Adapter
9
- module ActiveRecord
10
- def self.included(base)
11
- base.extend ClassMethods
12
- base.instance_eval do
13
- include ActiveRecord::Predicates if defined?(Features::Predicate::ClassMethods)
14
- end
15
- end
16
-
17
- module ClassMethods
18
- def normalize_find_handler(handler, key)
19
- case handler
20
- when Symbol then ->(entity_type, value) { method(handler).call entity_type, value }
21
- when Proc then handler
22
- else ->(entity_type, value) { entity_type.where(key.to_sym => value) unless Utils::String.blank?(value) }
23
- end
24
- end
25
- end
26
-
27
- include Paging if defined?(Features::Paging::ClassMethods)
28
- include Sorting if defined?(Features::Sorting::ClassMethods)
29
- end
30
- end
31
- end
32
- end
@@ -1,32 +0,0 @@
1
- require 'trailblazer/finder/adapters/data_mapper/paging'
2
- require 'trailblazer/finder/adapters/data_mapper/sorting'
3
- require 'trailblazer/finder/adapters/data_mapper/predicates'
4
-
5
- module Trailblazer
6
- class Finder
7
- module Adapters
8
- # ActiveRecord Adapter
9
- module DataMapper
10
- def self.included(base)
11
- base.extend ClassMethods
12
- base.instance_eval do
13
- include DataMapper::Predicates if defined?(Features::Predicate::ClassMethods)
14
- end
15
- end
16
-
17
- module ClassMethods
18
- def normalize_find_handler(handler, key)
19
- case handler
20
- when Symbol then ->(entity_type, value) { method(handler).call entity_type, value }
21
- when Proc then handler
22
- else ->(entity_type, value) { entity_type.all(key.to_sym => value) unless Utils::String.blank?(value) }
23
- end
24
- end
25
- end
26
-
27
- include Paging if defined?(Features::Paging::ClassMethods)
28
- include Sorting if defined?(Features::Sorting::ClassMethods)
29
- end
30
- end
31
- end
32
- end
@@ -1,20 +0,0 @@
1
- module Trailblazer
2
- class Finder
3
- module Adapters
4
- module DataMapper
5
- # Sequel - Paging Adapter
6
- module Paging
7
- def self.included(base)
8
- base.extend Features::Paging::ClassMethods
9
- end
10
-
11
- private
12
-
13
- def apply_paging(entity_type)
14
- entity_type.all(limit: per_page, offset: ([page, 1].max - 1) * per_page)
15
- end
16
- end
17
- end
18
- end
19
- end
20
- end
@@ -1,42 +0,0 @@
1
- module Trailblazer
2
- class Finder
3
- module Adapters
4
- module DataMapper
5
- # ActiveRecord - Predicate Adapter
6
- module Predicates
7
- def not_eq(attribute, value, entity_type)
8
- entity_type.all(attribute.to_sym.not => value)
9
- end
10
-
11
- def eq(attribute, value, entity_type)
12
- entity_type.all(attribute.to_sym => value)
13
- end
14
-
15
- def blank(attribute, _value, entity_type)
16
- entity_type.all(conditions: ["#{attribute} IS NULL OR #{attribute} = ?", ''])
17
- end
18
-
19
- def not_blank(attribute, _value, entity_type)
20
- entity_type.all(conditions: ["#{attribute} IS NOT NULL AND #{attribute} != ?", ''])
21
- end
22
-
23
- def gt(attribute, value, entity_type)
24
- entity_type.all(attribute.to_sym.gt => value)
25
- end
26
-
27
- def gte(attribute, value, entity_type)
28
- entity_type.all(attribute.to_sym.gte => value)
29
- end
30
-
31
- def lt(attribute, value, entity_type)
32
- entity_type.all(attribute.to_sym.lt => value)
33
- end
34
-
35
- def lte(attribute, value, entity_type)
36
- entity_type.all(attribute.to_sym.lte => value)
37
- end
38
- end
39
- end
40
- end
41
- end
42
- end
@@ -1,38 +0,0 @@
1
- module Trailblazer
2
- class Finder
3
- module Adapters
4
- module DataMapper
5
- # Sequel - Sorting Adapter
6
- 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_attribute, sort_direction)
14
- # case sort_direction
15
- # when 'asc', 'ascending'
16
- # entity_type.all(order: [sort_attribute.to_sym.asc])
17
- # when 'desc', 'descending'
18
- # entity_type.all(order: [sort_attribute.to_sym.desc])
19
- # end
20
- # end
21
-
22
- def sort_orders(sort_attr, sort_dir)
23
- case sort_dir
24
- when 'asc', 'ascending'
25
- sort_attr.to_sym.asc
26
- when 'desc', 'descending'
27
- sort_attr.to_sym.desc
28
- end
29
- end
30
-
31
- def sort_it(entity_type, sort_attributes)
32
- entity_type.all(order: sort_attributes.flatten)
33
- end
34
- end
35
- end
36
- end
37
- end
38
- end
@@ -1,31 +0,0 @@
1
- module Trailblazer
2
- class Finder
3
- module Adapters
4
- # FriendlyId Adapter
5
- module FriendlyId
6
- def self.included(base)
7
- base.extend ClassMethods
8
- base.class_eval do
9
- filter_by :id, with: :apply_slug_filter
10
- end
11
- end
12
-
13
- def apply_slug_filter(entity_type, value)
14
- return if value.nil?
15
- return if value == ''
16
- if num?(value)
17
- entity_type.where(id: value)
18
- else
19
- entity_type.where(slug: Utils::String.underscore(value.downcase))
20
- end
21
- end
22
-
23
- def num?(str)
24
- Integer(str)
25
- rescue ArgumentError, TypeError
26
- false
27
- end
28
- end
29
- end
30
- end
31
- end
@@ -1,18 +0,0 @@
1
- module Trailblazer
2
- class Finder
3
- module Adapters
4
- # Kaminari Adapter
5
- module Kaminari
6
- def self.included(base)
7
- base.extend Features::Paging::ClassMethods
8
- end
9
-
10
- private
11
-
12
- def apply_paging(entity_type)
13
- entity_type.page(page).per(per_page)
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,32 +0,0 @@
1
- require 'trailblazer/finder/adapters/sequel/paging'
2
- require 'trailblazer/finder/adapters/sequel/sorting'
3
- require 'trailblazer/finder/adapters/sequel/predicates'
4
-
5
- module Trailblazer
6
- class Finder
7
- module Adapters
8
- # Sequel Adapter
9
- module Sequel
10
- def self.included(base)
11
- base.extend ClassMethods
12
- base.instance_eval do
13
- include Sequel::Predicates if defined?(Features::Predicate::ClassMethods)
14
- end
15
- end
16
-
17
- module ClassMethods
18
- def normalize_find_handler(handler, key)
19
- case handler
20
- when Symbol then ->(entity_type, value) { method(handler).call entity_type, value }
21
- when Proc then handler
22
- else ->(entity_type, value) { entity_type.where(key.to_sym => value) unless Utils::String.blank?(value) }
23
- end
24
- end
25
- end
26
-
27
- include Paging if defined?(Features::Paging::ClassMethods)
28
- include Sorting if defined?(Features::Sorting::ClassMethods)
29
- end
30
- end
31
- end
32
- end
@@ -1,18 +0,0 @@
1
- module Trailblazer
2
- class Finder
3
- module Adapters
4
- # WillPaginate Adapter
5
- module WillPaginate
6
- def self.included(base)
7
- base.extend Features::Paging::ClassMethods
8
- end
9
-
10
- private
11
-
12
- def apply_paging(entity_type)
13
- entity_type.paginate per_page: per_page, page: page.zero? ? nil : page
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,11 +0,0 @@
1
- module Trailblazer
2
- class Finder
3
- module Error
4
- class BlockIgnored < ArgumentError
5
- def initialize(message = "defined_by filter_by's don't accept blocks")
6
- super message
7
- end
8
- end
9
- end
10
- end
11
- end
@@ -1,11 +0,0 @@
1
- module Trailblazer
2
- class Finder
3
- module Error
4
- class InvalidDefinedByValue < ArgumentError
5
- def initialize(filter_by, defined_bys, value)
6
- super "Invalid value '#{value}' used for defined_by #{filter_by} (expected one of #{defined_bys.join(', ')})"
7
- end
8
- end
9
- end
10
- end
11
- end
@@ -1,16 +0,0 @@
1
- module Trailblazer
2
- class Finder
3
- module Error
4
- class InvalidNumber < ArgumentError
5
- attr_reader :field, :number
6
-
7
- def initialize(field, number)
8
- @field = field
9
- @number = number
10
-
11
- super "#{field} should be more than 0. Currently '#{number}' is given."
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,11 +0,0 @@
1
- module Trailblazer
2
- class Finder
3
- module Error
4
- class MissingEntityType < ArgumentError
5
- def initialize(message = 'No Entity Type provided. Entity Type can be defined on a class level or passed as an option.')
6
- super message
7
- end
8
- end
9
- end
10
- end
11
- end
@@ -1,11 +0,0 @@
1
- module Trailblazer
2
- class Finder
3
- module Error
4
- class WithIgnored < ArgumentError
5
- def initialize(message = "defined_by filter_by's don't accept :with")
6
- super message
7
- end
8
- end
9
- end
10
- end
11
- end
@@ -1,23 +0,0 @@
1
- require 'trailblazer/finder/features/paging'
2
- require 'trailblazer/finder/features/sorting'
3
- require 'trailblazer/finder/features/predicate'
4
-
5
- module Trailblazer
6
- class Finder
7
- # Features
8
- module Features
9
- def self.included(base)
10
- base.extend ClassMethods
11
- end
12
-
13
- # ClassMethods
14
- module ClassMethods
15
- def features(*mods)
16
- mods.each do |mod|
17
- include mod
18
- end
19
- end
20
- end
21
- end
22
- end
23
- end
@@ -1,55 +0,0 @@
1
- module Trailblazer
2
- class Finder
3
- module Features
4
- module Paging
5
- def self.included(base)
6
- base.extend ClassMethods
7
- end
8
-
9
- attr_reader :page, :per_page
10
-
11
- def initialize(options = {})
12
- @page = [options[:page].to_i, 0].max
13
- @per_page = self.class.calculate_per_page options[:per_page]
14
-
15
- super options
16
- end
17
-
18
- private
19
-
20
- def fetch_results
21
- apply_paging super
22
- end
23
-
24
- # Adapters will overwite this method
25
- def apply_paging(entity_type)
26
- entity_type.drop(([page, 1].max - 1) * per_page).first(per_page)
27
- end
28
-
29
- module ClassMethods
30
- def per_page(number)
31
- raise Error::InvalidNumber.new('Per page', number) unless number > 0
32
- config[:per_page] = number
33
- end
34
-
35
- def min_per_page(number)
36
- raise Error::InvalidNumber.new('Min per page', number) unless number > 0
37
- config[:min_per_page] = number
38
- end
39
-
40
- def max_per_page(number)
41
- raise Error::InvalidNumber.new('Max per page', number) unless number > 0
42
- config[:max_per_page] = number
43
- end
44
-
45
- def calculate_per_page(given)
46
- per_page = (given || config[:per_page] || 25).to_i.abs
47
- per_page = [per_page, config[:max_per_page]].min if config[:max_per_page]
48
- per_page = [per_page, config[:min_per_page]].max if config[:min_per_page]
49
- per_page
50
- end
51
- end
52
- end
53
- end
54
- end
55
- end