activeset 0.4.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40f42594ad16de88043af834c1fd25c435e35bcd
4
- data.tar.gz: 064a4a104d040a1d28bafabdcf437a69bfe8baa5
3
+ metadata.gz: 940224ee8c773aed3a1ce2e3cb0c93f425aff2c9
4
+ data.tar.gz: ff881b84c798ac46a483ee07b6c29022eec702eb
5
5
  SHA512:
6
- metadata.gz: 071f0b2aa19d7691660693a6cfdca52cb6bbf7fd3c4cbab7deea0d6e5c651e5c2445e4f47932f08f31bb006604489135ed8dc9c50bd008cf9e8d7d0b3d7c6807
7
- data.tar.gz: f77c58fdef21bd82910ffaed576584cb736398170cf4ef1f9d637364b0144034dda06c7c35c48f71edce83102ceabd50c5c90975cc30b701998b9ced2dcaaa51
6
+ metadata.gz: ed967c35778e17b42ba0820c35752ee8e119cf66b6d57a19d40f03effb1b96998715099519b1eb13d5e2eeafeca2c4349c5c6898511ad0604d8378a957e69eb7
7
+ data.tar.gz: f1222be08e6898a21d163494ff2d45c07cac85e0fb3d4206bdfe9554109c70e61c23dc3dc8636ed7b4162e2118c07ed2f2a8ed573af70f34ccd246f3851b1b59
data/CHANGELOG CHANGED
@@ -1,3 +1,16 @@
1
+ v 0.5.0
2
+ - Ensure the titleized method is delegated from the Instructions::Entry class
3
+ - Make the Instructions Keypath class more resilient against nils
4
+ - Add a new Transfrom processor
5
+ This allows users to transform a dataset into (right now only) CSV using hash column definitions of the sort:
6
+
7
+ ```
8
+ [
9
+ { key: ‘Explit Column Header’,
10
+ value: ‘path.to.resource.from.dataset.instance’ },
11
+ { value: ->(item) { mutate(item.attribute(=) } }
12
+ ]
13
+ ```
1
14
  v 0.4.4
2
15
  - Ensure that the ActiveRecord filterer can handle Rails4 AR::Relations not responding to :methods on the underlying class
3
16
  - Test scopes defined using the helper but also as simply class methods
data/lib/active_set.rb CHANGED
@@ -5,6 +5,7 @@ require 'active_set/version'
5
5
  require 'active_set/processors/filter_processor'
6
6
  require 'active_set/processors/sort_processor'
7
7
  require 'active_set/processors/paginate_processor'
8
+ require 'active_set/processors/transform_processor'
8
9
 
9
10
  class ActiveSet
10
11
  include Enumerable
@@ -46,4 +47,9 @@ class ActiveSet
46
47
  paginater = PaginateProcessor.new(@set, instructions)
47
48
  self.class.new(paginater.process)
48
49
  end
50
+
51
+ def transform(instructions)
52
+ transformer = TransformProcessor.new(@set, instructions)
53
+ self.class.new(transformer.process)
54
+ end
49
55
  end
@@ -11,7 +11,7 @@ class ActiveSet
11
11
  attr_reader :keypath
12
12
 
13
13
  delegate :path, :attribute, :operator, :associations_array,
14
- :associations_hash, :value_for, :resource_for,
14
+ :associations_hash, :value_for, :resource_for, :titleized,
15
15
  to: :keypath
16
16
 
17
17
  def initialize(keypath, value)
@@ -17,21 +17,23 @@ class ActiveSet
17
17
 
18
18
  def attribute
19
19
  attribute = @path.last
20
- return attribute.sub(operator_regex, '') if attribute.match operator_regex
20
+ return attribute.sub(operator_regex, '') if attribute&.match operator_regex
21
21
  attribute
22
22
  end
23
23
 
24
24
  def operator
25
25
  attribute = @path.last
26
- return attribute[operator_regex, 1] if attribute.match operator_regex
26
+ return attribute[operator_regex, 1] if attribute&.match operator_regex
27
27
  '=='
28
28
  end
29
29
 
30
30
  def associations_array
31
+ return [] unless @path.any?
31
32
  @path.slice(0, @path.length - 1)
32
33
  end
33
34
 
34
35
  def associations_hash
36
+ return {} unless @path.any?
35
37
  associations_array.reverse.reduce({}) { |a, e| { e => a } }
36
38
  end
37
39
 
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+
6
+ require_relative '../base_adapter'
7
+ require_relative '../base_processor'
8
+
9
+ class ActiveSet
10
+ class TransformProcessor < BaseProcessor
11
+ class CSVAdapter < BaseAdapter
12
+ def process
13
+ return_set(transformed_set)
14
+ end
15
+
16
+ private
17
+
18
+ def transformed_set
19
+ ::CSV.generate do |output|
20
+ output << column_keys_for(item: @set.first)
21
+ @set.each do |item|
22
+ output << column_values_for(item: item)
23
+ end
24
+ end
25
+ end
26
+
27
+ def column_keys_for(item:)
28
+ columns.map do |column|
29
+ ColumnInstruction.new(column, item).key
30
+ end
31
+ end
32
+
33
+ def column_values_for(item:)
34
+ columns.map do |column|
35
+ ColumnInstruction.new(column, item).value
36
+ end
37
+ end
38
+
39
+ def columns
40
+ @instruction.compact
41
+ end
42
+
43
+ class ColumnInstruction
44
+ def initialize(hash, item)
45
+ @hash = hash.with_indifferent_access
46
+ @item = item
47
+ end
48
+
49
+ def key
50
+ return @hash[:key] if @hash.key? :key
51
+ return attribute_model.human_attribute_name(instruction_entry.attribute) if attribute_model.respond_to? :human_attribute_name
52
+ instruction_entry.titleized
53
+ end
54
+
55
+ def value
56
+ return blank unless @hash.key?(:value)
57
+ return @hash[:value].call(@item) if @hash[:value]&.respond_to? :call
58
+ instruction_entry.value_for(item: @item)
59
+ end
60
+
61
+ private
62
+
63
+ def instruction_entry
64
+ Instructions::Entry.new(@hash[:value], nil)
65
+ end
66
+
67
+ def attribute_model
68
+ instruction_entry.associations_array
69
+ .reduce(@item) do |obj, assoc|
70
+ obj&.send(assoc)
71
+ end.class
72
+ end
73
+
74
+ def blank
75
+ '—'
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './base_processor'
4
+ require_relative './transform/csv_adapter'
5
+
6
+ class ActiveSet
7
+ class TransformProcessor < BaseProcessor
8
+ def process
9
+ adapter_for(format: format).new(@set, columns).process
10
+ end
11
+
12
+ private
13
+
14
+ def adapter_for(format:)
15
+ return CSVAdapter if format == 'csv'
16
+ end
17
+
18
+ def format
19
+ @instructions.get(:format).to_s.downcase
20
+ end
21
+
22
+ def columns
23
+ @instructions.get(:columns)
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ActiveSet
4
- VERSION = '0.4.4'
4
+ VERSION = '0.5.0'
5
5
  end
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.4.4
4
+ version: 0.5.0
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-12 00:00:00.000000000 Z
11
+ date: 2017-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -186,6 +186,8 @@ files:
186
186
  - lib/active_set/processors/sort/active_record_adapter.rb
187
187
  - lib/active_set/processors/sort/enumerable_adapter.rb
188
188
  - lib/active_set/processors/sort_processor.rb
189
+ - lib/active_set/processors/transform/csv_adapter.rb
190
+ - lib/active_set/processors/transform_processor.rb
189
191
  - lib/active_set/version.rb
190
192
  - lib/patches/core_ext/hash/flatten_keys.rb
191
193
  homepage: https://github.com/fractaledmind/activeset