array_transform 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bca98ed72596ed8c419fbeddb3a8a2519cd9254a
4
+ data.tar.gz: fd9186d92671ca0af507d226f21c8e37c928da64
5
+ SHA512:
6
+ metadata.gz: 5e288388692d65fb498b70e6aee8eb1963394b80d108bd0df7df827778314b045663f7ebd58323f64e63298b6970f33132bae28881ff161c54af8bedd8b20146
7
+ data.tar.gz: 5fe282f824b6b4cee0ba1158ce3e33c25a23d315b82d852a6094754aa6aab2af27e72bbd1cd7b91525f343943b3a74dd5e38fa9d4642b2cfe3943097f9dde677
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.byebug_history
2
+ /*.swp
3
+ /.bundle/
4
+ /.yardoc
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.2.10
7
+ - 2.3.7
8
+ - 2.4.5
9
+ - 2.5.3
10
+ before_install: gem install bundler -v 1.17.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at code-of-conduct@wescrimmage.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in array_transform.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ array_transform (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (10.0.2)
10
+ diff-lcs (1.3)
11
+ rake (10.5.0)
12
+ rspec (3.6.0)
13
+ rspec-core (~> 3.6.0)
14
+ rspec-expectations (~> 3.6.0)
15
+ rspec-mocks (~> 3.6.0)
16
+ rspec-core (3.6.0)
17
+ rspec-support (~> 3.6.0)
18
+ rspec-expectations (3.6.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.6.0)
21
+ rspec-mocks (3.6.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.6.0)
24
+ rspec-support (3.6.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ array_transform!
31
+ bundler (~> 1.17)
32
+ byebug (~> 10.0)
33
+ rake (~> 10.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 1.17.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Scrimmage
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # TODO before commit
2
+
3
+ * update assertions to match error messages
4
+ * change copy column to leverage add column (?)
5
+ * Error handling strategy
6
+
7
+ # TODO
8
+
9
+ * Add Code Climate badge
10
+ * Add CI badge
11
+ * Add DSL
12
+ * Update readme
13
+ * Add merge columns operation
14
+ * Add split column operation
15
+ * Add copy rows operation
16
+ * Add append rows operation
17
+
18
+ # ArrayTransform
19
+
20
+ [![Build Status](https://travis-ci.com/Scrimmage/array_transform.svg?token=4X3FUNyi2mcNDmPpLxLJ&branch=master)](https://travis-ci.com/Scrimmage/array_transform)
21
+
22
+ [![Maintainability](https://api.codeclimate.com/v1/badges/b41c1328058c53253ef7/maintainability)](https://codeclimate.com/repos/5bec61c049116227ca00c52e/maintainability)
23
+
24
+ ## Requirements
25
+
26
+ Ruby 2.3.0 or higher.
27
+
28
+ ## Installation
29
+
30
+ Add this line to your application's Gemfile:
31
+
32
+ ```ruby
33
+ gem 'array_transform'
34
+ ```
35
+
36
+ And then execute:
37
+
38
+ $ bundle
39
+
40
+ Or install it yourself as:
41
+
42
+ $ gem install array_transform
43
+
44
+ ## Usage
45
+
46
+ Usage information will be provide in the future. Current implmentation is not stable. For those that continue regardless, try:
47
+
48
+ ```ruby
49
+ include ArrayTransform::Operations
50
+
51
+ add_column(
52
+ cell_operation: -> (i) { 1 },
53
+ data: [[]]
54
+ ).data
55
+ ```
56
+
57
+ and see the operation specs.
58
+
59
+ ## Development
60
+
61
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
62
+
63
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at https://github.com/scrimmage/array_transform. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
68
+
69
+ ## License
70
+
71
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
72
+
73
+ ## Code of Conduct
74
+
75
+ Everyone interacting in the ArrayTransform project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/scrimmage/array_transform/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "array_transform/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "array_transform"
8
+ spec.version = ArrayTransform::VERSION
9
+ spec.authors = ["Matthew Chadwick"]
10
+ spec.email = ["matthew@wescrimmage.com"]
11
+
12
+ spec.summary = %q{Provides a library of transformations for arrays.}
13
+ spec.description = %q{Consists of operations to manipulate arrays; the T of ETL for datasets that fit in-memory. Operations include adding/copying/removing/updating columns, adding/removing/updating rows and joining external tables.}
14
+ spec.homepage = "https://github.com/scrimmage/array_transform"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.17"
27
+ spec.add_development_dependency "byebug", "~> 10.0"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "array_transform"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,15 @@
1
+ require "array_transform/version"
2
+ require "array_transform/concerns/callable"
3
+ require "array_transform/operations"
4
+ require "array_transform/operations/add_column"
5
+ require "array_transform/operations/add_index_column"
6
+ require "array_transform/operations/copy_column"
7
+ require "array_transform/operations/join_with_lookup"
8
+ require "array_transform/operations/remove_column"
9
+ require "array_transform/operations/remove_rows"
10
+ require "array_transform/operations/update_row"
11
+
12
+ module ArrayTransform
13
+ # class Error < StandardError; end
14
+ # Your code goes here...
15
+ end
@@ -0,0 +1,132 @@
1
+ module ArrayTransform::Callable
2
+
3
+ def self.included(base)
4
+ base.class_eval do
5
+ prepend(Callable)
6
+ extend(ClassMethods)
7
+ end
8
+ end
9
+
10
+ module Callable
11
+ def call
12
+ super
13
+ # catch :halt do
14
+ # super
15
+ # super.tap do
16
+ # success unless result_specified?
17
+ # end
18
+ # end
19
+ end
20
+ end
21
+
22
+ module ClassMethods
23
+ def call(*args, &block)
24
+ new(*args, &block).tap do |callable|
25
+ callable.call
26
+ end
27
+ end
28
+ end
29
+
30
+ # # Indicates whether the call failed
31
+ # def failed?
32
+ # !success?
33
+ # end
34
+
35
+ # # Indicates whether the call called success or failure
36
+ # def result_specified?
37
+ # defined?(@success)
38
+ # end
39
+
40
+ # # Mark the call as successful.
41
+ # def success
42
+ # @success = true
43
+ # end
44
+
45
+ # # Indicates if the call was successful
46
+ # def success?
47
+ # !!@success
48
+ # end
49
+
50
+ end
51
+
52
+
53
+
54
+
55
+ # module UseCase
56
+
57
+ # def self.included(base)
58
+ # base.class_eval do
59
+ # prepend Perform
60
+ # extend ClassMethods
61
+ # include Virtus.model(strict: true, required: false)
62
+ # include ActiveModel::Validations
63
+ # end
64
+ # end
65
+
66
+ # module Perform
67
+ # # Executes use case logic
68
+ # #
69
+ # # Use cases must implement this method. Assumes success if failure is not called.
70
+ # def perform
71
+ # catch :halt do
72
+ # super.tap do
73
+ # success unless result_specified?
74
+ # end
75
+ # end
76
+ # end
77
+ # end
78
+
79
+ # module ClassMethods
80
+ # # Executes and returns the use case
81
+ # #
82
+ # # A use case object is instantiated with the supplied
83
+ # # arguments, perform is called and the object is returned.
84
+ # def perform(*args, &block)
85
+ # new(*args, &block).tap do |use_case|
86
+ # use_case.perform
87
+ # end
88
+ # end
89
+ # end
90
+
91
+ # attr_accessor :error_category, :error_key, :upstream_use_case
92
+
93
+ # # Mark the use case as failed.
94
+ # def failure
95
+ # @success = false
96
+ # end
97
+
98
+ # # Mark the use case as failed and exits the use case.
99
+ # def failure!(message = nil)
100
+ # failure
101
+ # throw :halt
102
+ # end
103
+
104
+ # # Block invoked only if use case failed.
105
+ # def on_failure(&block)
106
+ # yield(self) if failed?
107
+ # self
108
+ # end
109
+
110
+ # # Block invoked only if use case succeeded.
111
+ # def on_success(&block)
112
+ # yield(self) if success?
113
+ # self
114
+ # end
115
+
116
+ # # Add errors to the provided use case and mark it as failed.
117
+ # def propagate_failure!(use_case)
118
+ # if failed?
119
+ # use_case.upstream_use_case = self
120
+ # use_case.errors.add(:base, "UseCase #{self.class.name} failed: #{system_error_messages_string}")
121
+ # use_case.error_key = error_key
122
+ # use_case.failure!
123
+ # end
124
+ # self
125
+ # end
126
+
127
+ # # Halts execution of the use case if validation fails.
128
+ # def validate!
129
+ # failure! unless valid?
130
+ # end
131
+
132
+ # end
@@ -0,0 +1,31 @@
1
+ module ArrayTransform::Operations
2
+
3
+ def add_column(*args, &block)
4
+ AddColumn.call(*args, &block)
5
+ end
6
+
7
+ def add_index_column(*args, &block)
8
+ AddIndexColumn.call(*args, &block)
9
+ end
10
+
11
+ def copy_column(*args, &block)
12
+ CopyColumn.call(*args, &block)
13
+ end
14
+
15
+ def join_lookup(*args, &block)
16
+ JoinWithLoookup.call(*args, &block)
17
+ end
18
+
19
+ def remove_column(*args, &block)
20
+ RemoveColumn.call(*args, &block)
21
+ end
22
+
23
+ def remove_rows(*args, &block)
24
+ RemoveRows.call(*args, &block)
25
+ end
26
+
27
+ def update_row(*args, &block)
28
+ UpdateRow.call(*args, &block)
29
+ end
30
+
31
+ end
@@ -0,0 +1,34 @@
1
+ module ArrayTransform::Operations
2
+
3
+ class AddColumn
4
+ include ArrayTransform::Callable
5
+
6
+ def initialize(
7
+ cell_operation:,
8
+ data:,
9
+ new_column_header: nil
10
+ )
11
+ @cell_operation = cell_operation
12
+ @data = data
13
+ @new_column_header = new_column_header
14
+ end
15
+
16
+ attr_reader :data
17
+
18
+ def call
19
+ add_column
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :cell_operation, :new_column_header
25
+
26
+ def add_column
27
+ data.each.with_index do |row, row_index|
28
+ row << cell_operation.call(row_index)
29
+ end
30
+ new_column_header && data[0][-1] = new_column_header
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,36 @@
1
+ module ArrayTransform::Operations
2
+
3
+ class AddIndexColumn
4
+ include ArrayTransform::Callable
5
+
6
+ # TODO: take a custom number generator?
7
+ # TODO: support before/after (before_column_header, after_column_header)
8
+ # [1, 2, 3].insert(1, 4)
9
+ # => [1, 4, 2, 3]
10
+
11
+ def initialize(
12
+ new_column_header: nil,
13
+ data:,
14
+ index_offset: 0
15
+ )
16
+ @new_column_header = new_column_header
17
+ @data = data
18
+ @index_offset = index_offset
19
+ end
20
+
21
+ attr_reader :data
22
+
23
+ def call
24
+ AddColumn.call(
25
+ cell_operation: -> (row_index) { row_index + index_offset },
26
+ data: data,
27
+ new_column_header: new_column_header
28
+ )
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :new_column_header, :index_offset
34
+ end
35
+
36
+ end
@@ -0,0 +1,59 @@
1
+ module ArrayTransform::Operations
2
+
3
+ class CopyColumn
4
+ include ArrayTransform::Callable
5
+
6
+ def initialize(
7
+ column_header: nil,
8
+ column_index: nil,
9
+ data:,
10
+ new_column_header: nil
11
+ )
12
+ @column_header = column_header
13
+ @column_index = column_index
14
+ @data = data
15
+ @new_column_header = new_column_header
16
+ end
17
+
18
+ attr_reader :data
19
+
20
+ def call
21
+ validate!
22
+ copy_column
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :column_header, :new_column_header
28
+
29
+ def column_index
30
+ @column_index ||= column_header_index
31
+ end
32
+
33
+ def column_header_index
34
+ data[0] && data[0].index(column_header)
35
+ end
36
+
37
+ def copy_column
38
+ # compare with performance of:
39
+ #
40
+ # column_index && AddColumn.call(
41
+ # cell_operation: -> (row_index) { data[row_index][column_index] },
42
+ # data: data,
43
+ # new_column_header: new_column_header
44
+ # )
45
+
46
+ column_index && data.each do |row|
47
+ row << row[column_index]
48
+ end
49
+ new_column_header && data[0][-1] = new_column_header
50
+ end
51
+
52
+ def validate!
53
+ if @column_index && @column_header
54
+ raise(ArgumentError, "can only specify one of :column_index or :column_header")
55
+ end
56
+ end
57
+ end
58
+
59
+ end
@@ -0,0 +1,88 @@
1
+ module ArrayTransform::Operations
2
+
3
+ class JoinWithLoookup
4
+ include ArrayTransform::Callable
5
+
6
+ def initialize(
7
+ column_header: nil,
8
+ column_index: nil,
9
+ data:,
10
+ lookup_data:,
11
+ lookup_column_header: nil,
12
+ lookup_column_index: nil, # default to 0? smart join on first column match?
13
+ lookup_default: nil
14
+ )
15
+ @column_header = column_header
16
+ @column_index = column_index
17
+ @data = data
18
+ @lookup_column_header = lookup_column_header
19
+ @lookup_column_index = lookup_column_index
20
+ @lookup_data = lookup_data
21
+ @lookup_default = lookup_default
22
+ end
23
+
24
+ attr_reader :data
25
+
26
+ def call
27
+ validate!
28
+ data.size > 0 && index_lookup_data
29
+ join_lookup_data
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :column_header, :lookup_by_key, :lookup_column_header, :lookup_data
35
+
36
+ def column_index
37
+ @column_index ||= column_header_index
38
+ end
39
+
40
+ def column_header_index
41
+ data[0] && data[0].index(column_header)
42
+ end
43
+
44
+ def index_lookup_data
45
+ # TODO: check for dup keys
46
+ @lookup_by_key = lookup_data.inject({}) { |map, row|
47
+ map[row[lookup_column_index]] = row[1..-1]
48
+ map
49
+ }
50
+ end
51
+
52
+ def join_lookup_data
53
+ data.each_with_index do |row, row_index|
54
+ row_key = data[row_index][column_index]
55
+ data[row_index] = data[row_index] + (lookup_by_key[row_key] || lookup_default)
56
+ end
57
+ end
58
+
59
+ def lookup_column_index
60
+ @lookup_column_index ||= lookup_column_header_index
61
+ end
62
+
63
+ def lookup_column_header_index
64
+ data[0] && data[0].index(lookup_column_header)
65
+ end
66
+
67
+ def lookup_default
68
+ @lookup_default ||= [nil] * (lookup_data[0].count - 1)
69
+ end
70
+
71
+ def validate!
72
+ if @column_index && @column_header
73
+ raise(ArgumentError, "can only specify one of :column_index or :column_header")
74
+ end
75
+
76
+ if @lookup_column_index && @lookup_column_header
77
+ raise(ArgumentError, "can only specify one of :lookup_column_index or :lookup_column_header")
78
+ end
79
+
80
+ # TODO: lookup data must have at least two columns?
81
+
82
+ if @lookup_default && lookup_data[0] && @lookup_default.count != lookup_data[0].count - 1
83
+ raise(ArgumentError, "lookup_default must have exactly #{lookup_data[0].count - 1} columns")
84
+ end
85
+ end
86
+ end
87
+
88
+ end
@@ -0,0 +1,48 @@
1
+ module ArrayTransform::Operations
2
+
3
+ class RemoveColumn
4
+ include ArrayTransform::Callable
5
+
6
+ def initialize(
7
+ column_header: nil,
8
+ column_index: nil,
9
+ data:
10
+ )
11
+ @column_header = column_header
12
+ @column_index = column_index
13
+ @data = data
14
+ end
15
+
16
+ attr_reader :data
17
+
18
+ def call
19
+ validate!
20
+ remove_column
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :column_header
26
+
27
+ def column_index
28
+ @column_index ||= column_header_index
29
+ end
30
+
31
+ def column_header_index
32
+ data[0] && data[0].index(column_header)
33
+ end
34
+
35
+ def remove_column
36
+ column_index && data.each do |row|
37
+ row.delete_at(column_index)
38
+ end
39
+ end
40
+
41
+ def validate!
42
+ if @column_index && @column_header
43
+ raise(ArgumentError, "can only specify one of :column_index or :column_header")
44
+ end
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,52 @@
1
+ module ArrayTransform::Operations
2
+
3
+ class RemoveRows
4
+ include ArrayTransform::Callable
5
+
6
+ def initialize(
7
+ cell_test:,
8
+ column_header: nil,
9
+ column_index: nil,
10
+ data:
11
+ )
12
+ @cell_test = cell_test
13
+ @column_header = column_header
14
+ @column_index = column_index
15
+ @data = data
16
+ end
17
+
18
+ attr_reader :data
19
+
20
+ def call
21
+ validate!
22
+ remove_rows
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :cell_test, :column_header
28
+
29
+ def column_index
30
+ @column_index ||= column_header_index
31
+ end
32
+
33
+ def column_header_index
34
+ data[0] && data[0].index(column_header)
35
+ end
36
+
37
+ def remove_rows
38
+ data.each_with_index.map { |row, row_index|
39
+ row_index if cell_test.call(column_index && row[column_index])
40
+ }.compact.sort.reverse.each { |row_index|
41
+ data.delete_at(row_index)
42
+ }
43
+ end
44
+
45
+ def validate!
46
+ if @column_index && @column_header
47
+ raise(ArgumentError, "can only specify one of :column_index or :column_header")
48
+ end
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,33 @@
1
+ module ArrayTransform::Operations
2
+
3
+ class UpdateRow
4
+ include ArrayTransform::Callable
5
+
6
+ def initialize(
7
+ cell_operation:,
8
+ data:,
9
+ row_offset: 0
10
+ )
11
+ @cell_operation = cell_operation
12
+ @data = data
13
+ @row_offset = row_offset
14
+ end
15
+
16
+ attr_reader :data
17
+
18
+ def call
19
+ modify_row
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :cell_operation, :row_offset
25
+
26
+ def modify_row
27
+ data[row_offset] && data[row_offset].each_with_index do |value, index|
28
+ data[row_offset][index] = cell_operation.call(value)
29
+ end
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,3 @@
1
+ module ArrayTransform
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array_transform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Chadwick
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Consists of operations to manipulate arrays; the T of ETL for datasets
70
+ that fit in-memory. Operations include adding/copying/removing/updating columns,
71
+ adding/removing/updating rows and joining external tables.
72
+ email:
73
+ - matthew@wescrimmage.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - array_transform.gemspec
88
+ - bin/console
89
+ - bin/setup
90
+ - lib/array_transform.rb
91
+ - lib/array_transform/concerns/callable.rb
92
+ - lib/array_transform/operations.rb
93
+ - lib/array_transform/operations/add_column.rb
94
+ - lib/array_transform/operations/add_index_column.rb
95
+ - lib/array_transform/operations/copy_column.rb
96
+ - lib/array_transform/operations/join_with_lookup.rb
97
+ - lib/array_transform/operations/remove_column.rb
98
+ - lib/array_transform/operations/remove_rows.rb
99
+ - lib/array_transform/operations/update_row.rb
100
+ - lib/array_transform/version.rb
101
+ homepage: https://github.com/scrimmage/array_transform
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.4.8
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Provides a library of transformations for arrays.
125
+ test_files: []