realize 1.0.0.pre.alpha

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 77bcd7b4fedcf6a51318375889b4ab11c4222015ddc7da3ada67315d5e6817dc
4
+ data.tar.gz: eb9d90c4d49a0ab19da44543b99e355c557012c1155740efb9613ac5e095e5b5
5
+ SHA512:
6
+ metadata.gz: 48875ff1d61c486f648955f2e62b9d1527dd40b2be7454a531dce21df3186017f06d2275d80587653867dc10a249666effb2e11f85ad10b5d31ca5e51a27d4d1
7
+ data.tar.gz: e3cb298a91c99011883d3763b01585548679388cc2b3c93269571d2c96e7eca002727035cd156a6cc8799d854930de2aa637cec6e005385ef810cd11aa976572
@@ -0,0 +1,8 @@
1
+ # See http://editorconfig.org/
2
+
3
+ [*]
4
+ trim_trailing_whitespace = true
5
+ indent_style = space
6
+ indent_size = 2
7
+ insert_final_newline = true
8
+ end_of_line = lf
@@ -0,0 +1,7 @@
1
+ .DS_Store
2
+ *.gem
3
+ /tmp
4
+ /coverage
5
+ Gemfile.lock
6
+ /pkg
7
+
@@ -0,0 +1,31 @@
1
+ Layout/LineLength:
2
+ Max: 100
3
+ Exclude:
4
+ - realize.gemspec
5
+
6
+ Metrics/BlockLength:
7
+ ExcludedMethods:
8
+ - let
9
+ - it
10
+ - describe
11
+ - context
12
+ - specify
13
+ - define
14
+
15
+ Metrics/MethodLength:
16
+ Max: 30
17
+
18
+ AllCops:
19
+ TargetRubyVersion: 2.5
20
+
21
+ Metrics/AbcSize:
22
+ Max: 16
23
+
24
+ Metrics/ClassLength:
25
+ Max: 125
26
+
27
+ Style/TrailingCommaInHashLiteral:
28
+ Enabled: false
29
+
30
+ Style/TrailingCommaInArrayLiteral:
31
+ Enabled: false
@@ -0,0 +1 @@
1
+ 2.6.6
@@ -0,0 +1,26 @@
1
+ env:
2
+ global:
3
+ - CC_TEST_REPORTER_ID=f66c5e9edc96c1e1f0d1ecd7b8836a8ce9f1d69cef678d4c9a3abc0af490d001
4
+ language: ruby
5
+ services:
6
+ - mysql
7
+ rvm:
8
+ # Build on the latest stable of all supported Rubies (https://www.ruby-lang.org/en/downloads/):
9
+ - 2.5.8
10
+ - 2.6.6
11
+ - 2.7.1
12
+ cache: bundler
13
+ before_script:
14
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
15
+ - chmod +x ./cc-test-reporter
16
+ - ./cc-test-reporter before-build
17
+ script:
18
+ - bundle exec rubocop
19
+ - bundle exec rspec spec --format documentation
20
+ after_script:
21
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
22
+ addons:
23
+ # https://docs.travis-ci.com/user/uploading-artifacts/
24
+ artifacts:
25
+ paths:
26
+ - Gemfile.lock
@@ -0,0 +1,3 @@
1
+ # 1.0.0 (June 8th, 2020)
2
+
3
+ Initial Release, includes 15 initial transformers.
@@ -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 oss@bluemarblepayroll.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,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'DISABLE_SIMPLECOV=true bundle exec rspec --format=documentation' do
4
+ require 'guard/rspec/dsl'
5
+ dsl = Guard::RSpec::Dsl.new(self)
6
+
7
+ # RSpec files
8
+ rspec = dsl.rspec
9
+ watch(rspec.spec_helper) { rspec.spec_dir }
10
+ watch(rspec.spec_support) { rspec.spec_dir }
11
+ watch(rspec.spec_files)
12
+
13
+ # Ruby files
14
+ ruby = dsl.ruby
15
+ dsl.watch_spec_files_for(ruby.lib_files)
16
+ end
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2020 Blue Marble Payroll, LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ # Realize
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/realize.svg)](https://badge.fury.io/rb/realize) [![Build Status](https://travis-ci.org/bluemarblepayroll/realize.svg?branch=master)](https://travis-ci.org/bluemarblepayroll/realize) [![Maintainability](https://api.codeclimate.com/v1/badges/115f0c5a1d0a4cce7230/maintainability)](https://codeclimate.com/github/bluemarblepayroll/realize/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/115f0c5a1d0a4cce7230/test_coverage)](https://codeclimate.com/github/bluemarblepayroll/realize/test_coverage) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
+
5
+ TODO
6
+
7
+ ## Installation
8
+
9
+ To install through Rubygems:
10
+
11
+ ````
12
+ gem install install realize
13
+ ````
14
+
15
+ You can also add this to your Gemfile:
16
+
17
+ ````
18
+ bundle add realize
19
+ ````
20
+
21
+ ## Examples
22
+
23
+ TODO
24
+
25
+ ## Contributing
26
+
27
+ ### Development Environment Configuration
28
+
29
+ Basic steps to take to get this repository compiling:
30
+
31
+ 1. Install [Ruby](https://www.ruby-lang.org/en/documentation/installation/) (check realize.gemspec for versions supported)
32
+ 2. Install bundler (gem install bundler)
33
+ 3. Clone the repository (git clone git@github.com:bluemarblepayroll/realize.git)
34
+ 4. Navigate to the root folder (cd realize)
35
+ 5. Install dependencies (bundle)
36
+
37
+ ### Running Tests
38
+
39
+ To execute the test suite run:
40
+
41
+ ````
42
+ bundle exec rspec spec --format documentation
43
+ ````
44
+
45
+ Alternatively, you can have Guard watch for changes:
46
+
47
+ ````
48
+ bundle exec guard
49
+ ````
50
+
51
+ Also, do not forget to run Rubocop:
52
+
53
+ ````
54
+ bundle exec rubocop
55
+ ````
56
+
57
+ ### Publishing
58
+
59
+ Note: ensure you have proper authorization before trying to publish new versions.
60
+
61
+ After code changes have successfully gone through the Pull Request review process then the following steps should be followed for publishing new versions:
62
+
63
+ 1. Merge Pull Request into master
64
+ 2. Update `lib/realize/version.rb` using [semantic versioning](https://semver.org/)
65
+ 3. Install dependencies: `bundle`
66
+ 4. Update `CHANGELOG.md` with release notes
67
+ 5. Commit & push master to remote and ensure CI builds master successfully
68
+ 6. 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).
69
+
70
+ ## Code of Conduct
71
+
72
+ Everyone interacting in this codebase, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bluemarblepayroll/realize/blob/master/CODE_OF_CONDUCT.md).
73
+
74
+ ## License
75
+
76
+ This project is MIT Licensed.
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2020-present, Blue Marble Payroll, LLC
5
+ #
6
+ # This source code is licensed under the MIT license found in the
7
+ # LICENSE file in the root directory of this source tree.
8
+ #
9
+
10
+ require 'bundler/gem_tasks'
11
+ require 'rspec/core/rake_task'
12
+ require 'rubocop/rake_task'
13
+
14
+ RSpec::Core::RakeTask.new(:spec)
15
+ RuboCop::RakeTask.new
16
+
17
+ task default: %i[rubocop spec]
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # Copyright (c) 2018-present, Blue Marble Payroll, LLC
6
+ #
7
+ # This source code is licensed under the MIT license found in the
8
+ # LICENSE file in the root directory of this source tree.
9
+ #
10
+
11
+ require 'bundler/setup'
12
+ require 'realize'
13
+
14
+ # You can add fixtures and/or initialization code here to make experimenting
15
+ # with your gem easier. You can also use a different console, if you like.
16
+
17
+ require 'pry'
18
+ Pry.start
File without changes
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2020-present, Blue Marble Payroll, LLC
5
+ #
6
+ # This source code is licensed under the MIT license found in the
7
+ # LICENSE file in the root directory of this source tree.
8
+ #
9
+
10
+ require 'acts_as_hashable'
11
+ require 'objectable'
12
+ require 'time'
13
+
14
+ require_relative 'realize/arrays'
15
+ require_relative 'realize/transformers'
16
+ require_relative 'realize/pipeline'
17
+
18
+ # Top-level syntactic sugar.
19
+ module Realize
20
+ class << self
21
+ def pipeline(resolver: Objectable.resolver, transformers: [])
22
+ Pipeline.new(
23
+ resolver: resolver,
24
+ transformers: transformers
25
+ )
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ # Mix-in that adds some nice common array helpers.
5
+ module Arrays
6
+ def array(value)
7
+ value.is_a?(Hash) ? [value] : Array(value)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Collection
5
+ # Transformer to take an array of oibjects and sort by the given key
6
+ # and by the given sort direction. Defaulting to ascending.
7
+ class Sort
8
+ module Direction
9
+ ASCENDING = 'ASC'
10
+ ASC = 'ASC'
11
+ DESCENDING = 'DESC'
12
+ DESC = 'DESC'
13
+ end
14
+ include Arrays
15
+ include Direction
16
+ acts_as_hashable
17
+
18
+ DEFAULT_ORDER = ASCENDING
19
+
20
+ attr_reader :key, :order
21
+
22
+ def initialize(key:, order: DEFAULT_ORDER)
23
+ raise ArgumentError, 'key is required' if key.to_s.empty?
24
+
25
+ @key = key
26
+ @order = Direction.const_get(order.to_s.upcase.to_sym)
27
+
28
+ freeze
29
+ end
30
+
31
+ def transform(_resolver, value, _time, _record)
32
+ records = array(value)
33
+
34
+ sorted = records.sort_by { |hsh| hsh[key.to_sym] }
35
+
36
+ order == DESCENDING ? sorted.reverse : sorted
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'sort/direction'
4
+
5
+ module Realize
6
+ class Collection
7
+ # Transformer to take an array of oibjects and sort by the given key
8
+ # and by the given sort direction. Defaulting to ascending.
9
+ class Sort
10
+ include Arrays
11
+ include Direction
12
+ acts_as_hashable
13
+
14
+ DEFAULT_ORDER = ASCENDING
15
+
16
+ attr_reader :key, :order
17
+
18
+ def initialize(key:, order: DEFAULT_ORDER)
19
+ raise ArgumentError, 'key is required' if key.to_s.empty?
20
+
21
+ @key = key
22
+ @order = Direction.const_get(order.to_s.upcase.to_sym)
23
+
24
+ freeze
25
+ end
26
+
27
+ def transform(_resolver, value, _time, _record)
28
+ records = array(value)
29
+
30
+ sorted = records.sort_by { |hsh| hsh[key.to_sym] }
31
+
32
+ order == DESCENDING ? sorted.reverse : sorted
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Filter
5
+ # This transformer can take an array or a hash (put in array) and it understands how to
6
+ # select only the records where a key's value looked up in the original record equates
7
+ # to the transformer's value.
8
+ class ByKeyRecordValue
9
+ include Arrays
10
+ acts_as_hashable
11
+
12
+ attr_reader :key, :value
13
+
14
+ # value is also passed into #transform so we need to alias it so its not shadowed by
15
+ # the argument.
16
+ alias desired_value value
17
+
18
+ def initialize(key:, value:)
19
+ raise ArgumentError, 'key is required' if key.to_s.empty?
20
+
21
+ @key = key
22
+ @value = value
23
+
24
+ freeze
25
+ end
26
+
27
+ def transform(resolver, value, _time, original_record)
28
+ records = array(value)
29
+
30
+ records.select do |record|
31
+ record_value = resolver.get(record, key)
32
+ record_value == resolver.get(original_record, desired_value)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Filter
5
+ # This transformer can take an array or a hash (put in array) and it understands how to
6
+ # select only the records where a key's value statically equates to the transformer's
7
+ # value.
8
+ class ByKeyValue
9
+ include Arrays
10
+ acts_as_hashable
11
+
12
+ attr_reader :key, :value
13
+
14
+ # value is also passed into #transform so we need to alias it so its not shadowed by
15
+ # the argument.
16
+ alias desired_value value
17
+
18
+ def initialize(key:, value:)
19
+ raise ArgumentError, 'key is required' if key.to_s.empty?
20
+
21
+ @key = key
22
+ @value = value
23
+
24
+ freeze
25
+ end
26
+
27
+ def transform(resolver, value, _time, _record)
28
+ records = array(value)
29
+
30
+ records.select do |record|
31
+ record_value = resolver.get(record, key)
32
+
33
+ record_value == desired_value
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Filter
5
+ # This transformer can take an object (will be converted to array) or array and
6
+ # will go through each child object and see if the child record has a value for the
7
+ # specified key. If it does then it will select that record.
8
+ class ByKeyValuePresence
9
+ include Arrays
10
+ acts_as_hashable
11
+
12
+ attr_reader :key
13
+
14
+ def initialize(key:)
15
+ raise ArgumentError, 'key is required' if key.to_s.empty?
16
+
17
+ @key = key
18
+
19
+ freeze
20
+ end
21
+
22
+ def transform(resolver, value, _time, _record)
23
+ records = array(value)
24
+
25
+ records.reject do |record|
26
+ resolver.get(record, key).to_s.empty?
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Filter
5
+ # This transformer can take in an array or hash (put in an array) and filters out
6
+ # the objects who have a start date greater than today or end date less than today.
7
+ # If a start or end date is null then it is assumed to be infinity.
8
+ class Inactive
9
+ include Arrays
10
+ acts_as_hashable
11
+
12
+ DEFAULT_START_DATE_KEY = 'start_date'
13
+ DEFAULT_END_DATE_KEY = 'end_date'
14
+
15
+ attr_reader :start_date_key, :end_date_key
16
+
17
+ def initialize(
18
+ start_date_key: DEFAULT_START_DATE_KEY,
19
+ end_date_key: DEFAULT_END_DATE_KEY
20
+ )
21
+ raise ArgumentError, 'start_date_key is required' if start_date_key.to_s.empty?
22
+ raise ArgumentError, 'end_date_key is required' if end_date_key.to_s.empty?
23
+
24
+ @start_date_key = start_date_key
25
+ @end_date_key = end_date_key
26
+
27
+ freeze
28
+ end
29
+
30
+ def transform(resolver, value, time, _record)
31
+ current_time = time.utc
32
+ records = array(value)
33
+
34
+ records.select do |record|
35
+ start_time = parse_date(resolver.get(record, start_date_key))
36
+ end_time = parse_date(resolver.get(record, end_date_key))
37
+
38
+ valid?(start_time, end_time, current_time)
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def valid?(start_time, end_time, current_time)
45
+ valid_start?(start_time, current_time) && valid_end?(end_time, current_time)
46
+ end
47
+
48
+ def valid_start?(start_time, current_time)
49
+ start_time.nil? || start_time <= current_time
50
+ end
51
+
52
+ def valid_end?(end_time, current_time)
53
+ end_time.nil? || end_time >= current_time
54
+ end
55
+
56
+ def parse_date(value)
57
+ value.to_s.empty? ? nil : Time.parse(value.to_s).utc
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Format
5
+ # Formats a date/time value. If the input is nil, it will output nil. If an input_format
6
+ # is configured then it will use that to explicitly parse it. If an input_format is not
7
+ # specified then it will use Ruby's DateTime#parse heuristics for parsing. By default,
8
+ # the output format is ISO-8601 standard (YYYY-MM-DD). It is also possible to have
9
+ # this output formatted time as well, but since it is named 'format_date', it is assumed
10
+ # you want something date-only.
11
+ #
12
+ # See this link for more information about Ruby's DateTime parsing and formatting
13
+ # details and also for valid format tokens:
14
+ # https://ruby-doc.org/stdlib-2.6.6/libdoc/date/rdoc/DateTime.html#method-i-strftime
15
+ class Date
16
+ acts_as_hashable
17
+
18
+ OUTPUT_FORMAT = '%F'
19
+
20
+ attr_reader :input_format, :output_format
21
+
22
+ def initialize(input_format: '', output_format: OUTPUT_FORMAT)
23
+ @input_format = input_format.to_s
24
+ @output_format = output_format.to_s
25
+
26
+ freeze
27
+ end
28
+
29
+ def transform(_resolver, value, _time, _record)
30
+ return nil if value.to_s.empty?
31
+
32
+ date_time =
33
+ if value.respond_to?(:to_datetime)
34
+ value.to_datetime
35
+ elsif input_format?
36
+ DateTime.strptime(value, input_format)
37
+ else
38
+ DateTime.parse(value)
39
+ end
40
+
41
+ date_time.strftime(output_format)
42
+ end
43
+
44
+ private
45
+
46
+ def input_format?
47
+ input_format.length.positive?
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Format
5
+ # This transformer takes in a value and replaces any whitespace characters (\t\r\n\f\v)
6
+ # with a blank space.
7
+ class RemoveWhitespace
8
+ acts_as_hashable
9
+
10
+ def initialize(_opts = {}); end
11
+
12
+ def transform(_resolver, value, _time, _record)
13
+ value.to_s.gsub(/\s+/, ' ')
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Format
5
+ # This transformer takes in a value and replaces all occurrences of the given
6
+ # original pattern with the replacement pattern.
7
+ class StringReplace
8
+ acts_as_hashable
9
+
10
+ attr_reader :original, :replacement
11
+
12
+ def initialize(original:, replacement:)
13
+ raise ArgumentError, 'original is required' if original.to_s.empty?
14
+
15
+ @original = original
16
+ @replacement = replacement.to_s
17
+
18
+ freeze
19
+ end
20
+
21
+ def transform(_resolver, value, _time, _record)
22
+ value.to_s.gsub(original, replacement)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'switch/case'
4
+
5
+ module Realize
6
+ class Logical
7
+ # This type of transformer can be categorized as a "comparator", meaning its intended
8
+ # desire is not a value resolution or value formatting, but a branching resolution.
9
+ # Specifically, you can define cases where, if the value matches, the transformers for the
10
+ # case would be executed. If no case is matched then the default_transformers will be
11
+ # executed.
12
+ class Switch
13
+ acts_as_hashable
14
+
15
+ attr_reader :cases, :default_transformers, :key
16
+
17
+ def initialize(cases: [], default_transformers: [], key:)
18
+ raise ArgumentError, 'key is required' if key.to_s.empty?
19
+
20
+ @cases = Case.array(cases)
21
+ @default_transformers = Transformers.array(default_transformers)
22
+ @key = key
23
+
24
+ freeze
25
+ end
26
+
27
+ def transform(resolver, value, time, record)
28
+ resolved_value = resolver.get(record, key)
29
+ matched_case = cases.find { |c| c.match?(resolved_value) }
30
+ transformers = matched_case ? matched_case.transformers : default_transformers
31
+
32
+ return value if transformers.empty?
33
+
34
+ transformers.inject(value) do |memo, transformer|
35
+ transformer.transform(resolver, memo, time, record)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Logical
5
+ class Switch
6
+ # This class encapsulates and defines what a switch case statement is. The values
7
+ # attribute is a list of values to test. The transformers are the transformers to
8
+ # execute if the value matches.
9
+ class Case
10
+ include Arrays
11
+ acts_as_hashable
12
+
13
+ attr_reader :values, :transformers
14
+
15
+ def initialize(values: [], transformers: [])
16
+ @values = array(values).map(&:to_s).to_set
17
+ @transformers = Transformers.array(transformers)
18
+ end
19
+
20
+ def match?(value)
21
+ values.include?(value.to_s)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ # Main runner that encapsulates a collection of transformers and knows how to kick off the
5
+ # transformation process.
6
+ class Pipeline
7
+ attr_reader :resolver, :transformers
8
+
9
+ def initialize(resolver: Objectable.resolver, transformers: [])
10
+ raise ArgumentError, 'resolver is required' unless resolver
11
+
12
+ @resolver = resolver
13
+ @transformers = Transformers.array(transformers)
14
+
15
+ freeze
16
+ end
17
+
18
+ def transform(record, time)
19
+ transformers.inject(record) do |memo, transformer|
20
+ transformer.transform(resolver, memo, time, record)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'collection/sort'
4
+ require_relative 'filter/by_key_record_value'
5
+ require_relative 'filter/by_key_value'
6
+ require_relative 'filter/by_key_value_presence'
7
+ require_relative 'filter/inactive'
8
+ require_relative 'format/date'
9
+ require_relative 'format/remove_whitespace'
10
+ require_relative 'format/string_replace'
11
+ require_relative 'logical/switch'
12
+ require_relative 'value/blank'
13
+ require_relative 'value/map'
14
+ require_relative 'value/null'
15
+ require_relative 'value/resolve'
16
+ require_relative 'value/static'
17
+ require_relative 'value/verbatim'
18
+
19
+ module Realize
20
+ # Transformers for building individual transformation step objects. Used like this:
21
+ # Transformers.make(type: '')
22
+ # Transformers.make(type: 'map')
23
+ # etc...
24
+ class Transformers
25
+ acts_as_hashable_factory
26
+
27
+ register '', Value::Verbatim
28
+ register 'r/collection/sort', Collection::Sort
29
+ register 'r/filter/by_key_record_value', Filter::ByKeyRecordValue
30
+ register 'r/filter/by_key_value', Filter::ByKeyValue
31
+ register 'r/filter/by_key_value_presence', Filter::ByKeyValuePresence
32
+ register 'r/filter/inactive', Filter::Inactive
33
+ register 'r/format/date', Format::Date
34
+ register 'r/format/remove_whitespace', Format::RemoveWhitespace
35
+ register 'r/format/string_replace', Format::StringReplace
36
+ register 'r/logical/switch', Logical::Switch
37
+ register 'r/value/blank', Value::Blank
38
+ register 'r/value/map', Value::Map
39
+ register 'r/value/null', Value::Null
40
+ register 'r/value/resolve', Value::Resolve
41
+ register 'r/value/static', Value::Static
42
+ register 'r/value/verbatim', Value::Verbatim
43
+ end
44
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Value
5
+ # Always returns blank string
6
+ class Blank
7
+ acts_as_hashable
8
+
9
+ def initialize(_opts = {}); end
10
+
11
+ def transform(_resolver, _value, _time, _record)
12
+ ''
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Value
5
+ # This transformer can take in a hash of: "value" -> "new value". It's basically a
6
+ # simple way for doing simple if X = Y, then Z replacements.
7
+ # This can be extended to include and customize case and type sensitivies, but right now
8
+ # it is type and case-sensitive so be careful!
9
+ class Map
10
+ acts_as_hashable
11
+
12
+ attr_reader :values
13
+
14
+ def initialize(values: {})
15
+ @values = values || {}
16
+
17
+ freeze
18
+ end
19
+
20
+ def transform(_resolver, value, _time, _record)
21
+ values.fetch(value, value)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Value
5
+ # Transformer that always returns nil
6
+ class Null
7
+ acts_as_hashable
8
+
9
+ def initialize(_opts = {}); end
10
+
11
+ def transform(_resolver, _value, _time, _record)
12
+ nil
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Value
5
+ # Basic transformer that can take an object and extract a value based off the transformer's
6
+ # key. If the value passed in is an array then it will select the first record.
7
+ class Resolve
8
+ acts_as_hashable
9
+
10
+ attr_reader :key
11
+
12
+ def initialize(key:)
13
+ raise ArgumentError, 'key is required' if key.to_s.empty?
14
+
15
+ @key = key
16
+
17
+ freeze
18
+ end
19
+
20
+ def transform(resolver, value, _time, _record)
21
+ record = first(value)
22
+
23
+ resolver.get(record, key)
24
+ end
25
+
26
+ private
27
+
28
+ def first(value)
29
+ value.is_a?(Array) ? value.first : value
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Value
5
+ # Transformer that always returns a static value
6
+ class Static
7
+ acts_as_hashable
8
+
9
+ attr_reader :value
10
+
11
+ def initialize(value: nil)
12
+ @value = value
13
+
14
+ freeze
15
+ end
16
+
17
+ def transform(_resolver, _value, _time, _record)
18
+ value
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Realize
4
+ class Value
5
+ # Default transformer that does nothing.
6
+ class Verbatim
7
+ acts_as_hashable
8
+
9
+ # This is here to satisfy an underlying issue in acts_as_hashable.
10
+ # The #make calls in the factory and hashable module should be calling #new with no
11
+ # args if no keys are detected.
12
+ def initialize(_opts = {}); end
13
+
14
+ def transform(_resolver, value, _time, _record)
15
+ value
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2020-present, Blue Marble Payroll, LLC
5
+ #
6
+ # This source code is licensed under the MIT license found in the
7
+ # LICENSE file in the root directory of this source tree.
8
+ #
9
+
10
+ module Realize
11
+ VERSION = '1.0.0-alpha'
12
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './lib/realize/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'realize'
7
+ s.version = Realize::VERSION
8
+ s.summary = 'Configurable Data Transformation Pipeline'
9
+
10
+ s.description = <<-DESCRIPTION
11
+ Derive and transform a value using a configuration-first pipeline.
12
+ DESCRIPTION
13
+
14
+ s.authors = ['Matthew Ruggio']
15
+ s.email = ['mruggio@bluemarblepayroll.com']
16
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ s.bindir = 'exe'
18
+ s.executables = []
19
+ s.homepage = 'https://github.com/bluemarblepayroll/realize'
20
+ s.license = 'MIT'
21
+ s.metadata = {
22
+ 'bug_tracker_uri' => 'https://github.com/bluemarblepayroll/realize/issues',
23
+ 'changelog_uri' => 'https://github.com/bluemarblepayroll/realize/blob/master/CHANGELOG.md',
24
+ 'documentation_uri' => 'https://www.rubydoc.info/gems/realize',
25
+ 'homepage_uri' => s.homepage,
26
+ 'source_code_uri' => s.homepage
27
+ }
28
+
29
+ s.required_ruby_version = '>= 2.5'
30
+
31
+ s.add_dependency('acts_as_hashable', '~>1')
32
+ s.add_dependency('objectable', '~>1')
33
+
34
+ s.add_development_dependency('guard-rspec', '~>4.7')
35
+ s.add_development_dependency('pry', '~>0')
36
+ s.add_development_dependency('rake', '~> 13')
37
+ s.add_development_dependency('rspec')
38
+ s.add_development_dependency('rubocop', '~>0.79.0')
39
+ s.add_development_dependency('simplecov', '~>0.17.0')
40
+ s.add_development_dependency('simplecov-console', '~>0.6.0')
41
+ end
metadata ADDED
@@ -0,0 +1,211 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: realize
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Ruggio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: acts_as_hashable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: objectable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '13'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '13'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.79.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.79.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.17.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.17.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov-console
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.6.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.6.0
139
+ description: " Derive and transform a value using a configuration-first pipeline.\n"
140
+ email:
141
+ - mruggio@bluemarblepayroll.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".editorconfig"
147
+ - ".gitignore"
148
+ - ".rubocop.yml"
149
+ - ".ruby-version"
150
+ - ".travis.yml"
151
+ - CHANGELOG.md
152
+ - CODE_OF_CONDUCT.md
153
+ - Gemfile
154
+ - Guardfile
155
+ - LICENSE
156
+ - README.md
157
+ - Rakefile
158
+ - bin/console
159
+ - exe/.gitkeep
160
+ - lib/realize.rb
161
+ - lib/realize/arrays.rb
162
+ - lib/realize/collection/sort.rb
163
+ - lib/realize/collection/sort/direction.rb
164
+ - lib/realize/filter/by_key_record_value.rb
165
+ - lib/realize/filter/by_key_value.rb
166
+ - lib/realize/filter/by_key_value_presence.rb
167
+ - lib/realize/filter/inactive.rb
168
+ - lib/realize/format/date.rb
169
+ - lib/realize/format/remove_whitespace.rb
170
+ - lib/realize/format/string_replace.rb
171
+ - lib/realize/logical/switch.rb
172
+ - lib/realize/logical/switch/case.rb
173
+ - lib/realize/pipeline.rb
174
+ - lib/realize/transformers.rb
175
+ - lib/realize/value/blank.rb
176
+ - lib/realize/value/map.rb
177
+ - lib/realize/value/null.rb
178
+ - lib/realize/value/resolve.rb
179
+ - lib/realize/value/static.rb
180
+ - lib/realize/value/verbatim.rb
181
+ - lib/realize/version.rb
182
+ - realize.gemspec
183
+ homepage: https://github.com/bluemarblepayroll/realize
184
+ licenses:
185
+ - MIT
186
+ metadata:
187
+ bug_tracker_uri: https://github.com/bluemarblepayroll/realize/issues
188
+ changelog_uri: https://github.com/bluemarblepayroll/realize/blob/master/CHANGELOG.md
189
+ documentation_uri: https://www.rubydoc.info/gems/realize
190
+ homepage_uri: https://github.com/bluemarblepayroll/realize
191
+ source_code_uri: https://github.com/bluemarblepayroll/realize
192
+ post_install_message:
193
+ rdoc_options: []
194
+ require_paths:
195
+ - lib
196
+ required_ruby_version: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - ">="
199
+ - !ruby/object:Gem::Version
200
+ version: '2.5'
201
+ required_rubygems_version: !ruby/object:Gem::Requirement
202
+ requirements:
203
+ - - ">"
204
+ - !ruby/object:Gem::Version
205
+ version: 1.3.1
206
+ requirements: []
207
+ rubygems_version: 3.0.3
208
+ signing_key:
209
+ specification_version: 4
210
+ summary: Configurable Data Transformation Pipeline
211
+ test_files: []