activeset 0.6.5 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +0 -6
- data/CHANGELOG +5 -0
- data/README.md +58 -6
- data/activeset.gemspec +8 -8
- data/bin/console +14 -0
- data/lib/.DS_Store +0 -0
- data/lib/active_set.rb +28 -21
- data/lib/active_set/{instruction.rb → attribute_instruction.rb} +12 -8
- data/lib/active_set/column_instruction.rb +44 -0
- data/lib/active_set/exporting/operation.rb +66 -0
- data/lib/active_set/filtering/operation.rb +190 -0
- data/lib/active_set/paginating/operation.rb +102 -0
- data/lib/active_set/sorting/operation.rb +144 -0
- data/lib/helpers/throws.rb +2 -2
- data/lib/helpers/transform_to_sortable_numeric.rb +36 -0
- data/lib/patches/core_ext/hash/flatten_keys.rb +43 -14
- metadata +55 -39
- data/lib/active_set/.DS_Store +0 -0
- data/lib/active_set/adapter_activerecord.rb +0 -58
- data/lib/active_set/adapter_base.rb +0 -14
- data/lib/active_set/instructions.rb +0 -42
- data/lib/active_set/processor_base.rb +0 -28
- data/lib/active_set/processor_filter.rb +0 -21
- data/lib/active_set/processor_filter/active_record_adapter.rb +0 -50
- data/lib/active_set/processor_filter/enumerable_adapter.rb +0 -21
- data/lib/active_set/processor_paginate.rb +0 -35
- data/lib/active_set/processor_paginate/active_record_adapter.rb +0 -37
- data/lib/active_set/processor_paginate/enumerable_adapter.rb +0 -39
- data/lib/active_set/processor_sort.rb +0 -19
- data/lib/active_set/processor_sort/active_record_adapter.rb +0 -27
- data/lib/active_set/processor_sort/active_record_operation.rb +0 -55
- data/lib/active_set/processor_sort/enumerable_adapter.rb +0 -58
- data/lib/active_set/processor_transform.rb +0 -30
- data/lib/active_set/processor_transform/csv_adapter.rb +0 -77
- data/lib/active_set/version.rb +0 -5
@@ -1,58 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../adapter_base'
|
4
|
-
require_relative '../processor_base'
|
5
|
-
|
6
|
-
class ActiveSet
|
7
|
-
class Processor::Sort < Processor::Base
|
8
|
-
class EnumerableAdapter < Adapter::Base
|
9
|
-
def process
|
10
|
-
@set.sort do |left, right|
|
11
|
-
@instructions.reduce(0) do |diff, instruction|
|
12
|
-
# set Adapter::Base#instruction, which many methods depend on
|
13
|
-
self.instruction = instruction
|
14
|
-
|
15
|
-
# `left` and `right` differed at an earlier order entry
|
16
|
-
next diff if diff != 0
|
17
|
-
|
18
|
-
left_value = sortable_attribute_for(left)
|
19
|
-
right_value = sortable_attribute_for(right)
|
20
|
-
|
21
|
-
# handle `nil` values
|
22
|
-
next 0 if left_value.nil? && right_value.nil?
|
23
|
-
next 1 if left_value.nil?
|
24
|
-
next -1 if right_value.nil?
|
25
|
-
|
26
|
-
# do the actual comparison
|
27
|
-
comparison = left_value <=> right_value
|
28
|
-
next comparison * direction_multiplier(instruction.value)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
|
35
|
-
def sortable_attribute_for(item)
|
36
|
-
value = instruction.value_for(item: item)
|
37
|
-
|
38
|
-
return value.to_s.downcase if case_insensitive?(value)
|
39
|
-
|
40
|
-
value
|
41
|
-
end
|
42
|
-
|
43
|
-
def case_insensitive?(value)
|
44
|
-
# Cannot sort pure Booleans or Nils, so we _must_ cast to Strings
|
45
|
-
return true if value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
46
|
-
return true if value.is_a?(NilClass)
|
47
|
-
|
48
|
-
instruction.operator.to_s.downcase == 'i'
|
49
|
-
end
|
50
|
-
|
51
|
-
def direction_multiplier(direction)
|
52
|
-
return -1 if direction.to_s.downcase.start_with? 'desc'
|
53
|
-
|
54
|
-
1
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative './processor_base'
|
4
|
-
require_relative './processor_transform/csv_adapter'
|
5
|
-
|
6
|
-
class ActiveSet
|
7
|
-
class Processor::Transform < Processor::Base
|
8
|
-
def process
|
9
|
-
fail "Format #{format} is not currently supported as option for tranform" unless %w[csv].include?(format)
|
10
|
-
|
11
|
-
adapter_for(format: format)
|
12
|
-
.new(@set, columns)
|
13
|
-
.process
|
14
|
-
end
|
15
|
-
|
16
|
-
private
|
17
|
-
|
18
|
-
def adapter_for(format:)
|
19
|
-
return CSVAdapter if format == 'csv'
|
20
|
-
end
|
21
|
-
|
22
|
-
def format
|
23
|
-
@instructions.get(:format).to_s.downcase
|
24
|
-
end
|
25
|
-
|
26
|
-
def columns
|
27
|
-
@instructions.get(:columns)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,77 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'csv'
|
4
|
-
require 'active_support/core_ext/hash/indifferent_access'
|
5
|
-
require 'active_support/core_ext/string/inflections'
|
6
|
-
|
7
|
-
require_relative '../adapter_base'
|
8
|
-
require_relative '../processor_base'
|
9
|
-
|
10
|
-
class ActiveSet
|
11
|
-
class Processor::Transform < Processor::Base
|
12
|
-
class CSVAdapter < Adapter::Base
|
13
|
-
def process
|
14
|
-
::CSV.generate do |output|
|
15
|
-
output << column_keys_for(item: @set.first)
|
16
|
-
@set.each do |item|
|
17
|
-
output << column_values_for(item: item)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def column_keys_for(item:)
|
25
|
-
columns.map do |column|
|
26
|
-
ColumnInstruction.new(column, item).key
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def column_values_for(item:)
|
31
|
-
columns.map do |column|
|
32
|
-
ColumnInstruction.new(column, item).value
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def columns
|
37
|
-
@instructions.compact
|
38
|
-
end
|
39
|
-
|
40
|
-
class ColumnInstruction
|
41
|
-
def initialize(hash, item)
|
42
|
-
@hash = hash.with_indifferent_access
|
43
|
-
@item = item
|
44
|
-
end
|
45
|
-
|
46
|
-
def key
|
47
|
-
return @hash[:key] if @hash.key? :key
|
48
|
-
return attribute_model.human_attribute_name(instruction_entry.attribute) if attribute_model.respond_to? :human_attribute_name
|
49
|
-
instruction_entry.keypath.map(&:titleize).join(' ')
|
50
|
-
end
|
51
|
-
|
52
|
-
def value
|
53
|
-
return blank unless @hash.key?(:value)
|
54
|
-
return @hash[:value].call(@item) if @hash[:value]&.respond_to? :call
|
55
|
-
instruction_entry.value_for(item: @item)
|
56
|
-
end
|
57
|
-
|
58
|
-
private
|
59
|
-
|
60
|
-
def instruction_entry
|
61
|
-
Instruction.new(@hash[:value], nil)
|
62
|
-
end
|
63
|
-
|
64
|
-
def attribute_model
|
65
|
-
instruction_entry.associations_array
|
66
|
-
.reduce(@item) do |obj, assoc|
|
67
|
-
obj&.send(assoc)
|
68
|
-
end.class
|
69
|
-
end
|
70
|
-
|
71
|
-
def blank
|
72
|
-
'—'
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|