activeset 0.1.0 → 0.2.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/lib/active_set.rb +0 -3
- data/lib/active_set/base_adapter.rb +4 -2
- data/lib/active_set/base_processor.rb +3 -1
- data/lib/active_set/filter/enumerable_adapter.rb +5 -1
- data/lib/active_set/filter/processor.rb +4 -2
- data/lib/active_set/paginate/enumerable_adapter.rb +1 -1
- data/lib/active_set/paginate/processor.rb +6 -2
- data/lib/active_set/sort/enumerable_adapter.rb +4 -2
- data/lib/active_set/sort/processor.rb +4 -2
- data/lib/active_set/structure_path.rb +46 -0
- data/lib/active_set/version.rb +1 -1
- data/lib/patches/core_ext/hash/flatten_keys.rb +30 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31f1479fd17f9303641a356e7db5697e2b5b113c
|
4
|
+
data.tar.gz: a65c103541374642713b8a27fbeb7b0e4b09518e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e089a327e4e5ce48a62c38b47889e5f696e1cf4641da3c69f04ffa9a69bc93ae1cdc98a6c0a36258207f0d4e3ed190bdc9c7d3e80eb562436cbc11a40e8ecdcc
|
7
|
+
data.tar.gz: 1c9781eaa496a291f361ca10e3e3ee44e6adf66d6353c00b8810d15b0962aeef0270241a3edf71fd9f2f5538f861757c13cfd7fffd6f9967593c37d748cf897f
|
data/.gitignore
CHANGED
data/lib/active_set.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'active_set/version'
|
4
|
-
require 'active_support/core_ext/object/blank'
|
5
|
-
require 'active_support/core_ext/hash/slice'
|
6
|
-
require 'patches/core_ext/hash/flatten_keys'
|
7
4
|
|
8
5
|
require 'active_set/filter/processor'
|
9
6
|
require 'active_set/sort/processor'
|
@@ -7,7 +7,11 @@ class ActiveSet
|
|
7
7
|
class EnumerableAdapter < BaseAdapter
|
8
8
|
def process(set)
|
9
9
|
@set = set
|
10
|
-
@set.select
|
10
|
+
@set.select do |item|
|
11
|
+
attribute_value = @structure_path.value_for(item: item)
|
12
|
+
attribute_value.send(@structure_path.operator,
|
13
|
+
@value)
|
14
|
+
end
|
11
15
|
end
|
12
16
|
end
|
13
17
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'active_support/core_ext/object/blank'
|
4
|
+
|
3
5
|
require_relative '../base_processor'
|
4
6
|
require_relative './enumerable_adapter'
|
5
7
|
|
@@ -8,8 +10,8 @@ class ActiveSet
|
|
8
10
|
class Processor < BaseProcessor
|
9
11
|
def process
|
10
12
|
@structure.reject { |_, v| v.blank? }
|
11
|
-
.reduce(@set) do |set, (
|
12
|
-
adapter.new(
|
13
|
+
.reduce(@set) do |set, (keypath, value)|
|
14
|
+
adapter.new(keypath, value).process(set)
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
@@ -8,7 +8,7 @@ class ActiveSet
|
|
8
8
|
class Processor < BaseProcessor
|
9
9
|
def process
|
10
10
|
return @set if @set.count < pagesize
|
11
|
-
adapter.new(
|
11
|
+
adapter.new(page_number, pagesize).process(@set)
|
12
12
|
end
|
13
13
|
|
14
14
|
private
|
@@ -17,8 +17,12 @@ class ActiveSet
|
|
17
17
|
EnumerableAdapter
|
18
18
|
end
|
19
19
|
|
20
|
+
def page_number
|
21
|
+
@structure[[:page]] || 1
|
22
|
+
end
|
23
|
+
|
20
24
|
def pagesize
|
21
|
-
@structure[:size] || 25
|
25
|
+
@structure[[:size]] || 25
|
22
26
|
end
|
23
27
|
end
|
24
28
|
end
|
@@ -7,8 +7,10 @@ class ActiveSet
|
|
7
7
|
class EnumerableAdapter < BaseAdapter
|
8
8
|
def process(set)
|
9
9
|
@set = set
|
10
|
-
@set.sort_by
|
11
|
-
|
10
|
+
@set.sort_by do |item|
|
11
|
+
attribute_value = @structure_path.value_for(item: item)
|
12
|
+
attribute_value.is_a?(String) ? attribute_value.downcase : attribute_value
|
13
|
+
end.tap { |c| c.reverse! if @value.to_s == 'desc' }
|
12
14
|
end
|
13
15
|
end
|
14
16
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'active_support/core_ext/object/blank'
|
4
|
+
|
3
5
|
require_relative '../base_processor'
|
4
6
|
require_relative './enumerable_adapter'
|
5
7
|
|
@@ -8,8 +10,8 @@ class ActiveSet
|
|
8
10
|
class Processor < BaseProcessor
|
9
11
|
def process
|
10
12
|
@structure.reject { |_, value| value.blank? }
|
11
|
-
.reduce(@set) do |set, (
|
12
|
-
adapter.new(
|
13
|
+
.reduce(@set) do |set, (keypath, value)|
|
14
|
+
adapter.new(keypath, value).process(set)
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext/array/wrap'
|
4
|
+
require 'active_support/core_ext/object/try'
|
5
|
+
|
6
|
+
class StructurePath
|
7
|
+
def initialize(path)
|
8
|
+
# `path` can be an Array (e.g. [:parent, :child, :grandchild])
|
9
|
+
# or a String (e.g. 'parent.child.grandchild')
|
10
|
+
@path = path.is_a?(String) ? path.split('.') : Array.wrap(path).map(&:to_s)
|
11
|
+
end
|
12
|
+
|
13
|
+
def attribute
|
14
|
+
attribute = @path.last
|
15
|
+
return attribute.sub(operator_regex, '') if attribute.match operator_regex
|
16
|
+
attribute
|
17
|
+
end
|
18
|
+
|
19
|
+
def operator
|
20
|
+
attribute = @path.last
|
21
|
+
return attribute[operator_regex, 1] if attribute.match operator_regex
|
22
|
+
'=='
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_a
|
26
|
+
@path.slice(0, @path.length - 1)
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_h
|
30
|
+
to_a.reverse.inject({}) { |a, e| { e => a } }
|
31
|
+
end
|
32
|
+
|
33
|
+
def value_for(item:)
|
34
|
+
@path.inject(item, :try)
|
35
|
+
end
|
36
|
+
|
37
|
+
def resource_for(item:)
|
38
|
+
to_a.inject(item, :try)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def operator_regex
|
44
|
+
%r{\((.*?)\)}
|
45
|
+
end
|
46
|
+
end
|
data/lib/active_set/version.rb
CHANGED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
# Returns a flat hash where all nested keys are collapsed into a an array of keys.
|
5
|
+
#
|
6
|
+
# hash = { person: { name: { first: 'Rob' }, age: '28' } }
|
7
|
+
# hash.flatten_keys # => {[:person, :name, :first]=>"Rob", [:person, :age]=>"28"}
|
8
|
+
# hash # => { person: { name: { first: 'Rob' }, age: '28' } }
|
9
|
+
def flatten_keys
|
10
|
+
_flatten_keys(self)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Replaces current hash with a flat hash where all nested keys are collapsed into a an array of keys.
|
14
|
+
# Returns +nil+ if no changes were made, otherwise returns the hash.
|
15
|
+
#
|
16
|
+
# hash = { person: { name: { first: 'Rob' }, age: '28' } }
|
17
|
+
# hash.flatten_keys! # => {[:person, :name, :first]=>"Rob", [:person, :age]=>"28"}
|
18
|
+
# hash # => {[:person, :name, :first]=>"Rob", [:person, :age]=>"28"}
|
19
|
+
def flatten_keys!
|
20
|
+
replace(_flatten_keys(self))
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def _flatten_keys(h, keys = [], res = {})
|
26
|
+
return res.merge!(keys => h) unless h.is_a? Hash
|
27
|
+
h.each { |k, r| _flatten_keys(r, keys + [k], res) }
|
28
|
+
res
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeset
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Margheim
|
@@ -93,7 +93,9 @@ files:
|
|
93
93
|
- lib/active_set/paginate/processor.rb
|
94
94
|
- lib/active_set/sort/enumerable_adapter.rb
|
95
95
|
- lib/active_set/sort/processor.rb
|
96
|
+
- lib/active_set/structure_path.rb
|
96
97
|
- lib/active_set/version.rb
|
98
|
+
- lib/patches/core_ext/hash/flatten_keys.rb
|
97
99
|
homepage: https://github.com/fractaledmind/activeset
|
98
100
|
licenses:
|
99
101
|
- MIT
|