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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 643196795ab69a5f654e5dd52afc5356b51dee42
4
- data.tar.gz: 015ca510f83e179a815839609a3687192f23b51d
3
+ metadata.gz: 31f1479fd17f9303641a356e7db5697e2b5b113c
4
+ data.tar.gz: a65c103541374642713b8a27fbeb7b0e4b09518e
5
5
  SHA512:
6
- metadata.gz: 7e8244ed3ea19a1fa7c43fbb974603e803b02abd0c74a193a2b49f5206b61b139c2c30041b1b92d2c1efc5e973d4298d791ac43c3f086aadcdd9967d1bab9e30
7
- data.tar.gz: 684f8413a2f8fe98c3df7e6a0de599d9547a562592a23652ce46ad93383885127ae605cd5625e10d8e94394d639ac5fb607918f412ccabcd72671debdf493959
6
+ metadata.gz: e089a327e4e5ce48a62c38b47889e5f696e1cf4641da3c69f04ffa9a69bc93ae1cdc98a6c0a36258207f0d4e3ed190bdc9c7d3e80eb562436cbc11a40e8ecdcc
7
+ data.tar.gz: 1c9781eaa496a291f361ca10e3e3ee44e6adf66d6353c00b8810d15b0962aeef0270241a3edf71fd9f2f5538f861757c13cfd7fffd6f9967593c37d748cf897f
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+ *.gem
@@ -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'
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative './structure_path'
4
+
3
5
  class BaseAdapter
4
- def initialize(key, value)
5
- @key = key
6
+ def initialize(keypath, value)
7
+ @structure_path = StructurePath.new(keypath)
6
8
  @value = value
7
9
  end
8
10
  end
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'patches/core_ext/hash/flatten_keys'
4
+
3
5
  class BaseProcessor
4
6
  def initialize(set, structure)
5
7
  @set = set
6
- @structure = structure
8
+ @structure = structure.flatten_keys
7
9
  end
8
10
  end
@@ -7,7 +7,11 @@ class ActiveSet
7
7
  class EnumerableAdapter < BaseAdapter
8
8
  def process(set)
9
9
  @set = set
10
- @set.select { |item| item.send(@key) == @value }
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, (key, value)|
12
- adapter.new(key, value).process(set)
13
+ .reduce(@set) do |set, (keypath, value)|
14
+ adapter.new(keypath, value).process(set)
13
15
  end
14
16
  end
15
17
 
@@ -17,7 +17,7 @@ class ActiveSet
17
17
  end
18
18
 
19
19
  def page_number
20
- @key
20
+ @structure_path.attribute.to_i
21
21
  end
22
22
  end
23
23
  end
@@ -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(@structure[:page], pagesize).process(@set)
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 { |item| item.send(@key) }
11
- .tap { |c| c.reverse! if @value.to_s == 'desc' }
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, (key, value)|
12
- adapter.new(key, value).process(set)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ActiveSet
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -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.1.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