realize 1.1.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -0
  3. data/CHANGELOG.md +34 -0
  4. data/README.md +21 -2
  5. data/lib/realize.rb +3 -0
  6. data/lib/realize/arrays.rb +7 -0
  7. data/lib/realize/collection/at_index.rb +7 -0
  8. data/lib/realize/collection/first.rb +7 -0
  9. data/lib/realize/collection/last.rb +7 -0
  10. data/lib/realize/collection/sort.rb +7 -0
  11. data/lib/realize/collection/sort/direction.rb +7 -0
  12. data/lib/realize/file/basename.rb +32 -0
  13. data/lib/realize/file/extname.rb +22 -0
  14. data/lib/realize/filter/by_key_record_value.rb +7 -0
  15. data/lib/realize/filter/by_key_value.rb +7 -0
  16. data/lib/realize/filter/by_key_value_presence.rb +7 -0
  17. data/lib/realize/filter/inactive.rb +7 -0
  18. data/lib/realize/format/date.rb +7 -0
  19. data/lib/realize/format/lowercase.rb +21 -0
  20. data/lib/realize/format/pad.rb +61 -0
  21. data/lib/realize/format/remove_whitespace.rb +7 -0
  22. data/lib/realize/format/sha256.rb +49 -0
  23. data/lib/realize/format/split.rb +33 -0
  24. data/lib/realize/format/string_replace.rb +7 -0
  25. data/lib/realize/format/string_template.rb +45 -0
  26. data/lib/realize/format/substring.rb +51 -0
  27. data/lib/realize/format/uppercase.rb +21 -0
  28. data/lib/realize/logical/switch.rb +8 -1
  29. data/lib/realize/logical/switch/case.rb +7 -0
  30. data/lib/realize/pipeline.rb +7 -0
  31. data/lib/realize/transformers.rb +48 -2
  32. data/lib/realize/type/array.rb +36 -0
  33. data/lib/realize/type/base.rb +25 -0
  34. data/lib/realize/type/boolean.rb +39 -0
  35. data/lib/realize/type/string.rb +23 -0
  36. data/lib/realize/value/blank.rb +7 -0
  37. data/lib/realize/value/map.rb +7 -0
  38. data/lib/realize/value/now.rb +31 -0
  39. data/lib/realize/value/null.rb +7 -0
  40. data/lib/realize/value/resolve.rb +7 -0
  41. data/lib/realize/value/static.rb +7 -0
  42. data/lib/realize/value/uuid.rb +21 -0
  43. data/lib/realize/value/verbatim.rb +7 -0
  44. data/lib/realize/version.rb +1 -1
  45. data/realize.gemspec +2 -1
  46. metadata +33 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e3bd172f76576e00c4765b1a5dead05e5692a0b79ad5e6e3eb2e3b6bb2c1667c
4
- data.tar.gz: 51c606f8a5d85fe3c5ee893e4acae40f647f60350d0f2a0155af6dee5a0e5eba
3
+ metadata.gz: f6decbc7d163ec927dbb7927ac09aee8d5df5b4de3d44e4ea233959bd1456276
4
+ data.tar.gz: b4c96da12cbc56d66a50f9396a4e66e3f97dbb035a62d31c29532030c7ee3e3b
5
5
  SHA512:
6
- metadata.gz: 9fbd8b77f12fc4054133adb16f1a1e89544ac24fded6bb144d3b2f0e2f20cbb1c87d5261de28d734c8a88a82f8046623b200e836e7983437e0fd506d1baf753d
7
- data.tar.gz: 43fef7ff265071ae18a0ed5ef8d77e270cfcd939d9fed0cc4c825f78dda7f7d59db88c150731ab34f3f061c9522b6f0d54d593292df4ae23447de79e5b152273
6
+ metadata.gz: e24dffe48d39646bb6e631000c03cb0480ac539d368008ca49d38528696b4b8b37f61fdf2524a9b7c308ec10707b16d6d3966948a4ca0b0be4603e1aa0834dca
7
+ data.tar.gz: f1d2105f0d009d0520edcd846e9f0b6e00bed4816f07498495a9a51ad849fd1d4a527af122e7c8cc17a6aadaaf101d22aa7f3db36037b895cf7a3b8fa33a33ff
@@ -17,6 +17,7 @@ Metrics/MethodLength:
17
17
 
18
18
  AllCops:
19
19
  TargetRubyVersion: 2.5
20
+ NewCops: enable
20
21
 
21
22
  Metrics/AbcSize:
22
23
  Max: 16
@@ -1,3 +1,37 @@
1
+ # 1.5.0 (TBD)
2
+
3
+ New Transformers:
4
+
5
+ * r/file/basename
6
+ * r/file/extname
7
+ * r/format/pad
8
+ * r/format/sha256
9
+ * r/format/split
10
+ * r/type/array
11
+
12
+ # 1.4.0 (November 16th, 2020)
13
+
14
+ New Transformers:
15
+
16
+ * r/format/lowercase
17
+ * r/format/uppercase
18
+
19
+ # 1.3.0 (November 4th, 2020)
20
+
21
+ New Transformers:
22
+
23
+ * r/format/string_template
24
+ * r/format/substring
25
+
26
+ # 1.2.0 (October 6th, 2020)
27
+
28
+ New Transformers:
29
+
30
+ * r/type/boolean
31
+ * r/type/string
32
+ * r/value/now
33
+ * r/value/uuid
34
+
1
35
  # 1.1.1 (September 9th, 2020)
2
36
 
3
37
  Fixes:
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Realize
2
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)
3
+ [![Gem Version](https://badge.fury.io/rb/realize.svg)](https://badge.fury.io/rb/realize) [![Build Status](https://travis-ci.com/bluemarblepayroll/realize.svg?branch=master)](https://travis-ci.com/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
4
 
5
5
  This library provides a pluggable and configurable data transformation framework. The general use-case is:
6
6
 
@@ -13,7 +13,7 @@ It is currently used in production at Blue Marble to power the transformation pi
13
13
  To install through Rubygems:
14
14
 
15
15
  ````
16
- gem install install realize
16
+ gem install realize
17
17
  ````
18
18
 
19
19
  You can also add this to your Gemfile:
@@ -67,6 +67,10 @@ Here is a list of each built-in transformer, their options, and what their funct
67
67
  * **r/collection/last** []: Takes an array (or coerces value to an array) and returns the value at the last index position.
68
68
  * **r/collection/sort** [key, direction]: Takes an array (or coerces value to an array) and sort it either ascending or descending by some defined key's value.
69
69
 
70
+ #### File-based Transformers
71
+
72
+ * **r/file/basename** [suffix]: Return the filename and extension. If suffix is provided then it will be stripped from the end. If '.*' is passed as the suffix then any extension will be ignored.
73
+ * **r/file/extname** [suffix]: Return the extension for value.
70
74
  #### Filtering Transformers
71
75
 
72
76
  * **r/filter/by_key_record_value** [key, value]: Takes an array (or coerces value to an array) and selects only the records that match the key's value. In this case the value is derived off of the main record.
@@ -77,20 +81,35 @@ Here is a list of each built-in transformer, their options, and what their funct
77
81
  #### Format-oriented Transformers
78
82
 
79
83
  * **r/format/date** [input_format, output_format]: Parses the incoming value into a Time object using the configured input_format and outputs it as formatted by the configured output_format.
84
+ * **r/format/lowercase** []: Calls `#to_s.downcase` on the value so the returned value is guaranteed to be a lower-cased string.
85
+ * **r/format/pad** [length, side, with]: Pad a string value with a specified 'with' value (defaults to blank space) up until the passed in length is reached. The 'side' option can be used to specify whether the padding should occur to the left or right side of the value.
80
86
  * **r/format/remove_whitespace** []: Removes all whitespace from the incoming value.
87
+ * **r/format/sha256** [encoding_format]: Convert the value into a SHA256 encoded string. The encoding_format will be hex by default but can also be configured to use base64.
88
+ * **r/format/split** [by]: Take a string value and split it on the specified 'by' value. By default, it will be split on a blank space.
81
89
  * **r/format/string_replace** [original, replacement]: Replaces all occurrences of the configured original value with the replacement value.
90
+ * **r/format/string_template** [expression, separator, use_record]: String interpolate an expression using either the record or passed in value. Nested objects can be handled (i.e. key paths like dot-notation) by passing in a separator.
91
+ * **r/format/substring** [start_index, end_index, exclusive]: Cut a string in a given range. All options are optional. If a start_index is not provided, the beginning of the string is used. If no end_index is specified then the end of the string is used. If exclusive is set to true then the last index position will not be included. For example: "hellofriend" with a start_index of 0, an end_index of 5, and exclusive as true would yield: "hello". If exclusive was false then it would yield "hellof"
92
+ * **r/format/uppercase** []: Calls `#to_s.upcase` on the value so the returned value is guaranteed to be an upper-cased string type.
82
93
 
83
94
  #### Logical Transformers
84
95
 
85
96
  * **r/logical/switch** [cases, default_transformers, key]: Provides a value-based logic branching. If a value matches a specific case, the specific cases transformers will be executed. If it does not match any case then the default_transformers will be executed.
86
97
 
98
+ #### Type Transformers
99
+
100
+ * **r/type/array** [nullable]: Ensure the value is an array by calling Kernel#Array on the value.
101
+ * **r/type/boolean** [nullable]: Returns `true` if the input is 'truthy', `false` if not. By default nullable is false, which means a nil input will return false. Changing this to true will return nil if nil is passed in. A 'truthy' value is defined as matching: true, t, yes, y, or 1 (case-insensitive).
102
+ * **r/type/string** [nullable]: Calls `#to_s` on the value so the returned value is guaranteed to be a string type.
103
+
87
104
  #### Value-oriented Transformers
88
105
 
89
106
  * **r/value/blank** []: Always return a blank string.
90
107
  * **r/value/map** [values]: Do a lookup on the value using the `values` hash. If a value is found, then its corresponding value is returned. If it isn't then the input is returned.
108
+ * **r/value/now** [utc_offset]: Returns a Time object, defaulting to UTC offset. You can optionally pass in a different offset in the FORM: "+/-HH:MM"
91
109
  * **r/value/null** []: Always returns null.
92
110
  * **r/value/resolve** [key]: Dynamically resolves a value based on the record's key. It uses the [Objectable](https://github.com/bluemarblepayroll/objectable) library by default, which provides some goodies like dot-notation for nested objects and type-insensitivity (works for Ruby objects, hashes, structs, etc.)
93
111
  * **r/value/static** [value]: Always returns a hard-coded value.
112
+ * **r/value/uuid** []: Returns a new 36 character UUID (i.e. 6967fec6-bbde-4497-82d9-55ccc7b87cd0)
94
113
  * **r/value/verbatim** []: Default transformer, simply echos back the input.
95
114
 
96
115
  ### Plugging in Transformers
@@ -8,7 +8,10 @@
8
8
  #
9
9
 
10
10
  require 'acts_as_hashable'
11
+ require 'digest'
11
12
  require 'objectable'
13
+ require 'securerandom'
14
+ require 'stringento'
12
15
  require 'time'
13
16
 
14
17
  require_relative 'realize/arrays'
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  # Mix-in that adds some nice common array helpers.
5
12
  module Arrays
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Collection
5
12
  # Transformer to get an item of a collection
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Collection
5
12
  # Transformer to get the first item of a collection
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Collection
5
12
  # Transformer to get the last item of a collection
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  require_relative 'sort/direction'
4
11
 
5
12
  module Realize
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Collection
5
12
  class Sort
@@ -0,0 +1,32 @@
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
+ class File
12
+ # Takes the value and strips out the directory. Suffix is provided as an option which strips
13
+ # out the extension (last period and beyond). If '.*' is passed in as the suffix then any
14
+ # extension will be stripped. Note that files with multiple extensions will only have their
15
+ # last extesion removed.
16
+ class Basename
17
+ acts_as_hashable
18
+
19
+ attr_reader :suffix
20
+
21
+ def initialize(suffix: '')
22
+ @suffix = suffix.to_s
23
+
24
+ freeze
25
+ end
26
+
27
+ def transform(_resolver, value, _time, _record)
28
+ ::File.basename(value.to_s, suffix)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,22 @@
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
+ class File
12
+ # Extract the file extension from the value. If the value has multiple extensions then
13
+ # only the last one will be returned.
14
+ class Extname
15
+ acts_as_hashable
16
+
17
+ def transform(_resolver, value, _time, _record)
18
+ ::File.extname(value.to_s)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Filter
5
12
  # This transformer can take an array or a hash (put in array) and it understands how to
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Filter
5
12
  # This transformer can take an array or a hash (put in array) and it understands how to
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Filter
5
12
  # This transformer can take an object (will be converted to array) or array and
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Filter
5
12
  # This transformer can take in an array or hash (put in an array) and filters out
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Format
5
12
  # Formats a date/time value. If the input is nil, it will output nil. If an input_format
@@ -0,0 +1,21 @@
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
+ class Format
12
+ # Call #to_s.downcase on the value and return result.
13
+ class Lowercase
14
+ acts_as_hashable
15
+
16
+ def transform(_resolver, value, _time, _record)
17
+ value.to_s.downcase
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,61 @@
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
+ class Format
12
+ # Pad a string value with a specified 'with' value (defaults to blank space) up until the
13
+ # passed in length is reached. The 'side' option can be used to specify whether the
14
+ # padding should occur to the left or right side of the value.
15
+ # Examples:
16
+ # 'ABC' + [length: 10, side: 'left', with: '123'] => '1231231ABC'
17
+ # 'ABC' + [length: 10, side: 'right, with: '123'] => 'ABC1231231'
18
+ #
19
+ # If length is not specified or is less than the actual value's length then no padding
20
+ # will occur.
21
+ class Pad
22
+ acts_as_hashable
23
+
24
+ module Side
25
+ LEFT = :left
26
+ RIGHT = :right
27
+ end
28
+ include Side
29
+
30
+ DEFAULT_SIDE = LEFT
31
+ DEFAULT_WITH = ' '
32
+
33
+ attr_reader :length, :side, :with
34
+
35
+ def initialize(length: nil, side: LEFT, with: DEFAULT_WITH)
36
+ @length = length ? length.to_i : nil
37
+ @side = Side.const_get(side.to_s.upcase.to_sym)
38
+ @with = with.to_s
39
+
40
+ freeze
41
+ end
42
+
43
+ def transform(_resolver, value, _time, _record)
44
+ return value unless length
45
+
46
+ value.to_s.send(just_method, length, with)
47
+ end
48
+
49
+ private
50
+
51
+ def just_method
52
+ case side
53
+ when LEFT
54
+ :rjust
55
+ when RIGHT
56
+ :ljust
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Format
5
12
  # This transformer takes in a value and replaces any whitespace characters (\t\r\n\f\v)
@@ -0,0 +1,49 @@
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
+ class Format
12
+ # Convert the value into a SHA256 encoded string. There are two main encoding formats:
13
+ # hex or base64. By default, the value will be hexidecimal encoded.
14
+ class Sha256
15
+ acts_as_hashable
16
+
17
+ module EncodingFormat
18
+ HEX = :hex
19
+ BASE64 = :base64
20
+ end
21
+ include EncodingFormat
22
+
23
+ DEFAULT_ENCODING_FORMAT = HEX
24
+
25
+ attr_reader :encoding_format
26
+
27
+ def initialize(encoding_format: DEFAULT_ENCODING_FORMAT)
28
+ @encoding_format = EncodingFormat.const_get(encoding_format.to_s.upcase.to_sym)
29
+
30
+ freeze
31
+ end
32
+
33
+ def transform(_resolver, value, _time, _record)
34
+ Digest::SHA256.send(sha_method, value.to_s)
35
+ end
36
+
37
+ private
38
+
39
+ def sha_method
40
+ case encoding_format
41
+ when HEX
42
+ :hexdigest
43
+ when BASE64
44
+ :base64digest
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,33 @@
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
+ class Format
12
+ # Take a string value and split it on the specified 'by' value. By default, it will be
13
+ # split on a blank space. For example: '123 456 789' would yield ['123', '456', '789'].
14
+ # If the value is not a string then it will be converted to a string by calling #to_s on it.
15
+ class Split
16
+ acts_as_hashable
17
+
18
+ DEFAULT_BY = ' '
19
+
20
+ attr_reader :by
21
+
22
+ def initialize(by: DEFAULT_BY)
23
+ @by = by.to_s
24
+
25
+ freeze
26
+ end
27
+
28
+ def transform(_resolver, value, _time, _record)
29
+ value.to_s.split(by)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Format
5
12
  # This transformer takes in a value and replaces all occurrences of the given
@@ -0,0 +1,45 @@
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
+ class Format
12
+ # Use an expression as a template and string interpolate it using the Stringento library.
13
+ # Stringento also uses Objectable to provide (optional) key-path notation for handling
14
+ # nested objects.
15
+ # For more information see underlying libraries:
16
+ # * Stringento: https://github.com/bluemarblepayroll/stringento
17
+ # * Objectable: https://github.com/bluemarblepayroll/objectable
18
+ class StringTemplate
19
+ acts_as_hashable
20
+
21
+ DEFAULT_SEPARATOR = '.'
22
+
23
+ attr_reader :expression, :resolver, :use_record
24
+
25
+ def initialize(expression: '', separator: DEFAULT_SEPARATOR, use_record: false)
26
+ @expression = expression.to_s
27
+ @resolver = Objectable.resolver(separator: separator)
28
+ @use_record = use_record || false
29
+
30
+ freeze
31
+ end
32
+
33
+ def transform(_resolver, value, _time, record)
34
+ input = use_record ? record : value
35
+
36
+ Stringento.evaluate(expression, input, resolver: self)
37
+ end
38
+
39
+ # For Stringento consumption
40
+ def resolve(value, input)
41
+ resolver.get(input, value)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,51 @@
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
+ class Format
12
+ # Cut a string using a range (start and end index). You can also choose whether you wish the
13
+ # range selection to be inclusive (default) or exclusive.
14
+ #
15
+ # Examples using the string: '012-345-6789'
16
+ #
17
+ # Exclusive vs. Inclusive
18
+ #
19
+ # [start_index: 4, end_index: 6, exclusive: false] => '345'
20
+ # [start_index: 4, end_index: 6, exclusive: true] => '34'
21
+ #
22
+ # Default Values / Omitted Options
23
+ #
24
+ # [end_index: 6] => '012-345'
25
+ # [start_index: 4] => '345-6789'
26
+ class Substring
27
+ acts_as_hashable
28
+
29
+ DEFAULT_END_INDEX = -1
30
+ DEFAULT_START_INDEX = 0
31
+
32
+ attr_reader :end_index, :exclusive, :start_index
33
+
34
+ def initialize(
35
+ end_index: DEFAULT_END_INDEX,
36
+ exclusive: false,
37
+ start_index: DEFAULT_START_INDEX
38
+ )
39
+ @end_index = end_index.to_i
40
+ @exclusive = exclusive || false
41
+ @start_index = start_index.to_i
42
+
43
+ freeze
44
+ end
45
+
46
+ def transform(_resolver, value, _time, _record)
47
+ exclusive ? value.to_s[start_index...end_index] : value.to_s[start_index..end_index]
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,21 @@
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
+ class Format
12
+ # Call #to_s.downcase on the value and return result.
13
+ class Uppercase
14
+ acts_as_hashable
15
+
16
+ def transform(_resolver, value, _time, _record)
17
+ value.to_s.upcase
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  require_relative 'switch/case'
4
11
 
5
12
  module Realize
@@ -14,7 +21,7 @@ module Realize
14
21
 
15
22
  attr_reader :cases, :default_transformers, :key
16
23
 
17
- def initialize(cases: [], default_transformers: [], key:)
24
+ def initialize(key:, cases: [], default_transformers: [])
18
25
  raise ArgumentError, 'key is required' if key.to_s.empty?
19
26
 
20
27
  @cases = Case.array(cases)
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Logical
5
12
  class Switch
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  # Main runner that encapsulates a collection of transformers and knows how to kick off the
5
12
  # transformation process.
@@ -1,22 +1,49 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  require_relative 'collection/at_index'
4
11
  require_relative 'collection/first'
5
12
  require_relative 'collection/last'
6
13
  require_relative 'collection/sort'
14
+
15
+ require_relative 'file/basename'
16
+ require_relative 'file/extname'
17
+
7
18
  require_relative 'filter/by_key_record_value'
8
19
  require_relative 'filter/by_key_value'
9
20
  require_relative 'filter/by_key_value_presence'
10
21
  require_relative 'filter/inactive'
22
+
11
23
  require_relative 'format/date'
24
+ require_relative 'format/lowercase'
25
+ require_relative 'format/pad'
12
26
  require_relative 'format/remove_whitespace'
27
+ require_relative 'format/sha256'
28
+ require_relative 'format/split'
13
29
  require_relative 'format/string_replace'
30
+ require_relative 'format/string_template'
31
+ require_relative 'format/substring'
32
+ require_relative 'format/uppercase'
33
+
14
34
  require_relative 'logical/switch'
35
+
36
+ require_relative 'type/array'
37
+ require_relative 'type/boolean'
38
+ require_relative 'type/string'
39
+
15
40
  require_relative 'value/blank'
16
41
  require_relative 'value/map'
42
+ require_relative 'value/now'
17
43
  require_relative 'value/null'
18
44
  require_relative 'value/resolve'
19
45
  require_relative 'value/static'
46
+ require_relative 'value/uuid'
20
47
  require_relative 'value/verbatim'
21
48
 
22
49
  module Realize
@@ -27,24 +54,43 @@ module Realize
27
54
  class Transformers
28
55
  acts_as_hashable_factory
29
56
 
30
- register '', Value::Verbatim
31
57
  register 'r/collection/at_index', Collection::AtIndex
32
58
  register 'r/collection/first', Collection::First
33
59
  register 'r/collection/last', Collection::Last
34
60
  register 'r/collection/sort', Collection::Sort
61
+
62
+ register 'r/file/basename', File::Basename
63
+ register 'r/file/extname', File::Extname
64
+
35
65
  register 'r/filter/by_key_record_value', Filter::ByKeyRecordValue
36
66
  register 'r/filter/by_key_value', Filter::ByKeyValue
37
67
  register 'r/filter/by_key_value_presence', Filter::ByKeyValuePresence
38
68
  register 'r/filter/inactive', Filter::Inactive
69
+
39
70
  register 'r/format/date', Format::Date
71
+ register 'r/format/lowercase', Format::Lowercase
72
+ register 'r/format/pad', Format::Pad
40
73
  register 'r/format/remove_whitespace', Format::RemoveWhitespace
74
+ register 'r/format/sha256', Format::Sha256
75
+ register 'r/format/split', Format::Split
41
76
  register 'r/format/string_replace', Format::StringReplace
77
+ register 'r/format/string_template', Format::StringTemplate
78
+ register 'r/format/substring', Format::Substring
79
+ register 'r/format/uppercase', Format::Uppercase
80
+
42
81
  register 'r/logical/switch', Logical::Switch
82
+
83
+ register 'r/type/array', Type::Array
84
+ register 'r/type/boolean', Type::Boolean
85
+ register 'r/type/string', Type::String
86
+
43
87
  register 'r/value/blank', Value::Blank
44
88
  register 'r/value/map', Value::Map
89
+ register 'r/value/now', Value::Now
45
90
  register 'r/value/null', Value::Null
46
91
  register 'r/value/resolve', Value::Resolve
47
92
  register 'r/value/static', Value::Static
48
- register 'r/value/verbatim', Value::Verbatim
93
+ register 'r/value/uuid', Value::Uuid
94
+ register 'r/value/verbatim', '', Value::Verbatim
49
95
  end
50
96
  end
@@ -0,0 +1,36 @@
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_relative 'base'
11
+
12
+ module Realize
13
+ class Type
14
+ # Ensure the value is an array by calling Kernel#Array on the value. If the value is a hash
15
+ # then it will ensure the hash data structure is preserved and placed within the array.
16
+ # Normally calling Array(hash) would yield an array of key-value pair arrays. For our
17
+ # pipeline we generally treat hashes as "record-like" objects, so we need to ensure
18
+ # we keep it in tact.
19
+ #
20
+ # This does not mean other data types do not suffer from Kernel#Array
21
+ # converting it to something unexpected. For example, passing in a Time object would yield
22
+ # an array of parts:
23
+ # Array(Time.now) => [2, 59, 10, 21, 11, 2020, 6, 326, false, "CST"]
24
+ #
25
+ # See: https://ruby-doc.org/core-2.7.2/Kernel.html#method-i-Array for more information.
26
+ class Array < Base
27
+ include Arrays
28
+
29
+ def transform(_resolver, value, _time, _record)
30
+ return nil if nullable && value.nil?
31
+
32
+ array(value)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,25 @@
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
+ class Type
12
+ # Common code for all Type Transformer subclasses.
13
+ class Base
14
+ acts_as_hashable
15
+
16
+ attr_reader :nullable
17
+
18
+ def initialize(nullable: false)
19
+ @nullable = nullable || false
20
+
21
+ freeze
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,39 @@
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_relative 'base'
11
+
12
+ module Realize
13
+ class Type
14
+ # Convert input into either true, false, or nil.
15
+ # By default nils will return false unless nullable: true is passed in.
16
+ # This transformer is very liberal in its parsing and will return true for:
17
+ # true, 'true', 't', 'TRUE', 'True', 1, '1', 'Y', 'yes', 'Yes', 'YES'
18
+ # All other non-truthy values will evaluate to false, such as:
19
+ # false, 'false', 'f', 'FALSE', 'False', 0, '0', 'N', 'no', 'No', 'NO', {}, [], '',
20
+ # 'abc', 123, :abc, etc...
21
+ class Boolean < Base
22
+ def transform(_resolver, value, _time, _record)
23
+ if nullable && value.nil?
24
+ nil
25
+ elsif truthy?(value)
26
+ true
27
+ else
28
+ false
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def truthy?(value)
35
+ value.to_s.match?(/(true|t|yes|y|1)$/i)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,23 @@
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_relative 'base'
11
+
12
+ module Realize
13
+ class Type
14
+ # Call #to_s on the value and return result.
15
+ class String < Base
16
+ def transform(_resolver, value, _time, _record)
17
+ return nil if nullable && value.nil?
18
+
19
+ value.to_s
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Value
5
12
  # Always returns blank string
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Value
5
12
  # This transformer can take in a hash of: "value" -> "new value". It's basically a
@@ -0,0 +1,31 @@
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
+ class Value
12
+ # Return a current Time object.
13
+ class Now
14
+ acts_as_hashable
15
+
16
+ DEFAULT_UTC_OFFSET = '+00:00'
17
+
18
+ attr_reader :utc_offset
19
+
20
+ def initialize(utc_offset: DEFAULT_UTC_OFFSET)
21
+ @utc_offset = utc_offset || DEFAULT_UTC_OFFSET
22
+
23
+ freeze
24
+ end
25
+
26
+ def transform(_resolver, _value, _time, _record)
27
+ Time.now.utc.localtime(utc_offset)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Value
5
12
  # Transformer that always returns nil
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Value
5
12
  # Basic transformer that can take an object and extract a value based off the transformer's
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Value
5
12
  # Transformer that always returns a static value
@@ -0,0 +1,21 @@
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
+ class Value
12
+ # Return a current Time object.
13
+ class Uuid
14
+ acts_as_hashable
15
+
16
+ def transform(_resolver, _value, _time, _record)
17
+ SecureRandom.uuid
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,12 @@
1
1
  # frozen_string_literal: true
2
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
+
3
10
  module Realize
4
11
  class Value
5
12
  # Default transformer that does nothing.
@@ -8,5 +8,5 @@
8
8
  #
9
9
 
10
10
  module Realize
11
- VERSION = '1.1.1'
11
+ VERSION = '1.5.0'
12
12
  end
@@ -30,12 +30,13 @@ Gem::Specification.new do |s|
30
30
 
31
31
  s.add_dependency('acts_as_hashable', '~>1', '>=1.2.0')
32
32
  s.add_dependency('objectable', '~>1')
33
+ s.add_dependency('stringento', '~>2.1')
33
34
 
34
35
  s.add_development_dependency('guard-rspec', '~>4.7')
35
36
  s.add_development_dependency('pry', '~>0')
36
37
  s.add_development_dependency('rake', '~> 13')
37
38
  s.add_development_dependency('rspec')
38
- s.add_development_dependency('rubocop', '~>0.79.0')
39
+ s.add_development_dependency('rubocop', '~>1.1')
39
40
  s.add_development_dependency('simplecov', '~>0.17.0')
40
41
  s.add_development_dependency('simplecov-console', '~>0.6.0')
41
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: realize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Ruggio
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2020-09-09 00:00:00.000000000 Z
12
+ date: 2020-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: acts_as_hashable
@@ -45,6 +45,20 @@ dependencies:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1'
48
+ - !ruby/object:Gem::Dependency
49
+ name: stringento
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.1'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.1'
48
62
  - !ruby/object:Gem::Dependency
49
63
  name: guard-rspec
50
64
  requirement: !ruby/object:Gem::Requirement
@@ -107,14 +121,14 @@ dependencies:
107
121
  requirements:
108
122
  - - "~>"
109
123
  - !ruby/object:Gem::Version
110
- version: 0.79.0
124
+ version: '1.1'
111
125
  type: :development
112
126
  prerelease: false
113
127
  version_requirements: !ruby/object:Gem::Requirement
114
128
  requirements:
115
129
  - - "~>"
116
130
  - !ruby/object:Gem::Version
117
- version: 0.79.0
131
+ version: '1.1'
118
132
  - !ruby/object:Gem::Dependency
119
133
  name: simplecov
120
134
  requirement: !ruby/object:Gem::Requirement
@@ -172,22 +186,37 @@ files:
172
186
  - lib/realize/collection/last.rb
173
187
  - lib/realize/collection/sort.rb
174
188
  - lib/realize/collection/sort/direction.rb
189
+ - lib/realize/file/basename.rb
190
+ - lib/realize/file/extname.rb
175
191
  - lib/realize/filter/by_key_record_value.rb
176
192
  - lib/realize/filter/by_key_value.rb
177
193
  - lib/realize/filter/by_key_value_presence.rb
178
194
  - lib/realize/filter/inactive.rb
179
195
  - lib/realize/format/date.rb
196
+ - lib/realize/format/lowercase.rb
197
+ - lib/realize/format/pad.rb
180
198
  - lib/realize/format/remove_whitespace.rb
199
+ - lib/realize/format/sha256.rb
200
+ - lib/realize/format/split.rb
181
201
  - lib/realize/format/string_replace.rb
202
+ - lib/realize/format/string_template.rb
203
+ - lib/realize/format/substring.rb
204
+ - lib/realize/format/uppercase.rb
182
205
  - lib/realize/logical/switch.rb
183
206
  - lib/realize/logical/switch/case.rb
184
207
  - lib/realize/pipeline.rb
185
208
  - lib/realize/transformers.rb
209
+ - lib/realize/type/array.rb
210
+ - lib/realize/type/base.rb
211
+ - lib/realize/type/boolean.rb
212
+ - lib/realize/type/string.rb
186
213
  - lib/realize/value/blank.rb
187
214
  - lib/realize/value/map.rb
215
+ - lib/realize/value/now.rb
188
216
  - lib/realize/value/null.rb
189
217
  - lib/realize/value/resolve.rb
190
218
  - lib/realize/value/static.rb
219
+ - lib/realize/value/uuid.rb
191
220
  - lib/realize/value/verbatim.rb
192
221
  - lib/realize/version.rb
193
222
  - realize.gemspec