activeset 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +5 -0
- data/lib/active_set.rb +13 -5
- data/lib/active_set/processors/paginate/active_record_adapter.rb +39 -0
- data/lib/active_set/processors/paginate/enumerable_adapter.rb +16 -4
- data/lib/active_set/processors/paginate_processor.rb +1 -0
- data/lib/active_set/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7cdef49d5066bfce1716a02c22c6944be6e78b5
|
4
|
+
data.tar.gz: 2e24745f57ba9312dce23a5d8d9ce3ad95f035fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afd9e636ca59c097baca307dd47e1bb25f8c18745a09f5665e5d118d2fca5e604f3a1bdafe73a997fea6d49941ae6bb6c556432c5289aa04cf0060de58955c05
|
7
|
+
data.tar.gz: 40f35bb0ec2f2b83c1b7b1b511fd0641d4a45e962b4058b59565fc70bc2609eec616ab8aa9eb8425b78025eede79982472ba09580d38f9723e9366269c0d74cd
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
v 0.5.2
|
2
|
+
- Add an ActiveRecord adapter for the PaginateProcessor
|
3
|
+
- Update the EnumerableAdapter for the PaginateProcessor to more consistently handle edgecases
|
4
|
+
When asking for a page number that is outside of the meaningful range (e.g. page 10 of a collection that would only have 5 meaningful pages), should return an empty collection.
|
5
|
+
- Ensure that the built up intstruction and the base size of the original set are readable from any chained instance of ActiveSet
|
1
6
|
v 0.5.1
|
2
7
|
- Fix bug where `transform` method tried to return a new instance of ActiveSet. It should simply returned the transformed value.
|
3
8
|
- Fail if tranformer can’t handle passed format
|
data/lib/active_set.rb
CHANGED
@@ -10,10 +10,12 @@ require 'active_set/processors/transform_processor'
|
|
10
10
|
class ActiveSet
|
11
11
|
include Enumerable
|
12
12
|
|
13
|
-
attr_reader :set
|
13
|
+
attr_reader :set, :instructions, :total_count
|
14
14
|
|
15
|
-
def initialize(set)
|
15
|
+
def initialize(set, instructions: {}, total_count: nil)
|
16
16
|
@set = set
|
17
|
+
@instructions = instructions
|
18
|
+
@total_count = total_count || @set.count
|
17
19
|
end
|
18
20
|
|
19
21
|
def each(&block)
|
@@ -35,17 +37,23 @@ class ActiveSet
|
|
35
37
|
|
36
38
|
def filter(instructions)
|
37
39
|
filterer = FilterProcessor.new(@set, instructions)
|
38
|
-
self.class.new(filterer.process
|
40
|
+
self.class.new(filterer.process,
|
41
|
+
instructions: @instructions.merge(filter: instructions),
|
42
|
+
total_count: @total_count)
|
39
43
|
end
|
40
44
|
|
41
45
|
def sort(instructions)
|
42
46
|
sorter = SortProcessor.new(@set, instructions)
|
43
|
-
self.class.new(sorter.process
|
47
|
+
self.class.new(sorter.process,
|
48
|
+
instructions: @instructions.merge(sort: instructions),
|
49
|
+
total_count: @total_count)
|
44
50
|
end
|
45
51
|
|
46
52
|
def paginate(instructions)
|
47
53
|
paginater = PaginateProcessor.new(@set, instructions)
|
48
|
-
self.class.new(paginater.process
|
54
|
+
self.class.new(paginater.process,
|
55
|
+
instructions: @instructions.merge(paginate: instructions),
|
56
|
+
total_count: @total_count)
|
49
57
|
end
|
50
58
|
|
51
59
|
def transform(instructions)
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../base_adapter'
|
4
|
+
require_relative '../base_processor'
|
5
|
+
|
6
|
+
class ActiveSet
|
7
|
+
class PaginateProcessor < BaseProcessor
|
8
|
+
class ActiveRecordAdapter < BaseAdapter
|
9
|
+
def process
|
10
|
+
return return_set unless @set.respond_to? :to_sql
|
11
|
+
|
12
|
+
return_set(paginated_set)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def paginated_set
|
18
|
+
return @set.none if @set.count <= page_size && page_number > 1
|
19
|
+
@set.limit(page_size).offset(page_offset)
|
20
|
+
end
|
21
|
+
|
22
|
+
def page_offset
|
23
|
+
return 0 if page_number == 1
|
24
|
+
page_size * page_number
|
25
|
+
end
|
26
|
+
|
27
|
+
def page_size
|
28
|
+
@instruction.value
|
29
|
+
end
|
30
|
+
|
31
|
+
def page_number
|
32
|
+
num = @instruction.attribute.to_i
|
33
|
+
return 1 if (num - 1).negative?
|
34
|
+
return 1 if (num - 1).zero?
|
35
|
+
num
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -7,22 +7,34 @@ class ActiveSet
|
|
7
7
|
class PaginateProcessor < BaseProcessor
|
8
8
|
class EnumerableAdapter < BaseAdapter
|
9
9
|
def process
|
10
|
-
return return_set if @set.count < pagesize
|
11
10
|
return_set(paginated_set)
|
12
11
|
end
|
13
12
|
|
14
13
|
private
|
15
14
|
|
16
15
|
def paginated_set
|
17
|
-
@set.
|
16
|
+
return [] if @set.count <= page_size && page_number > 1
|
17
|
+
|
18
|
+
@set[page_start..page_end] || []
|
19
|
+
end
|
20
|
+
|
21
|
+
def page_start
|
22
|
+
return 0 if page_number == 1
|
23
|
+
page_size * (page_number - 1)
|
24
|
+
end
|
25
|
+
|
26
|
+
def page_end
|
27
|
+
return page_start if page_size == 1
|
28
|
+
page_start + page_size - 1
|
18
29
|
end
|
19
30
|
|
20
|
-
def
|
31
|
+
def page_size
|
21
32
|
@instruction.value
|
22
33
|
end
|
23
34
|
|
24
35
|
def page_number
|
25
|
-
@instruction.attribute.to_i
|
36
|
+
num = @instruction.attribute.to_i
|
37
|
+
(num.to_i - 1).negative? ? 0 : num
|
26
38
|
end
|
27
39
|
end
|
28
40
|
end
|
data/lib/active_set/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeset
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Margheim
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -181,6 +181,7 @@ files:
|
|
181
181
|
- lib/active_set/processors/filter/active_record_adapter.rb
|
182
182
|
- lib/active_set/processors/filter/enumerable_adapter.rb
|
183
183
|
- lib/active_set/processors/filter_processor.rb
|
184
|
+
- lib/active_set/processors/paginate/active_record_adapter.rb
|
184
185
|
- lib/active_set/processors/paginate/enumerable_adapter.rb
|
185
186
|
- lib/active_set/processors/paginate_processor.rb
|
186
187
|
- lib/active_set/processors/sort/active_record_adapter.rb
|