stringento 1.0.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.
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2019-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 'spec_helper'
11
+ require './spec/examples/custom_formatter'
12
+
13
+ describe ::Stringento::Formatter do
14
+ describe 'when not specifying a formatter' do
15
+ it 'should return value as a string' do
16
+ value = 'some value'
17
+
18
+ actual = described_class.new.format(value)
19
+
20
+ expect(actual).to eq(value)
21
+ end
22
+ end
23
+
24
+ describe 'when specifying a formatter that does not exist' do
25
+ it 'should return value as a string' do
26
+ value = 'some value'
27
+
28
+ actual = described_class.new.format(value, 'doesnt_exist')
29
+
30
+ expect(actual).to eq(value)
31
+ end
32
+ end
33
+
34
+ describe 'custom formatters' do
35
+ it 'yes_no formatter should be called' do
36
+ formatter = CustomFormatter.new
37
+ method = 'yes_no'
38
+
39
+ expect(formatter.format(true, method)).to eq('Yes')
40
+ expect(formatter.format('true', method)).to eq('Yes')
41
+ expect(formatter.format('', method)).to eq('Yes')
42
+ expect(formatter.format('false', method)).to eq('Yes')
43
+
44
+ expect(formatter.format(false, method)).to eq('No')
45
+ expect(formatter.format(nil, method)).to eq('No')
46
+ end
47
+ end
48
+
49
+ it 'yes_no_unknown formatter should be called' do
50
+ formatter = CustomFormatter.new
51
+ method = 'yes_no_unknown'
52
+
53
+ expect(formatter.format(true, method)).to eq('Yes')
54
+ expect(formatter.format('true', method)).to eq('Yes')
55
+ expect(formatter.format('', method)).to eq('Yes')
56
+ expect(formatter.format('false', method)).to eq('Yes')
57
+
58
+ expect(formatter.format(false, method)).to eq('No')
59
+
60
+ expect(formatter.format(nil, method)).to eq('Unknown')
61
+ end
62
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2019-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 'spec_helper'
11
+
12
+ describe ::Stringento::Placeholder do
13
+ let(:tests) do
14
+ [
15
+ [
16
+ '',
17
+ {
18
+ arg: '',
19
+ formatter: '',
20
+ name: '',
21
+ value: ''
22
+ }
23
+ ],
24
+ [
25
+ nil,
26
+ {
27
+ arg: '',
28
+ formatter: '',
29
+ name: '',
30
+ value: ''
31
+ }
32
+ ],
33
+ [
34
+ 1,
35
+ {
36
+ arg: '',
37
+ formatter: '',
38
+ name: '1',
39
+ value: '1'
40
+ }
41
+ ],
42
+ [
43
+ false,
44
+ {
45
+ arg: '',
46
+ formatter: '',
47
+ name: 'false',
48
+ value: 'false'
49
+ }
50
+ ],
51
+ [
52
+ 'string-value',
53
+ {
54
+ arg: '',
55
+ formatter: '',
56
+ name: 'string-value',
57
+ value: 'string-value'
58
+ }
59
+ ],
60
+ [
61
+ 'dob::date::short',
62
+ {
63
+ arg: 'short',
64
+ formatter: 'date',
65
+ name: 'dob',
66
+ value: 'dob::date::short'
67
+ }
68
+ ],
69
+ [
70
+ 'created_at::time_ago',
71
+ {
72
+ arg: '',
73
+ formatter: 'time_ago',
74
+ name: 'created_at',
75
+ value: 'created_at::time_ago'
76
+ }
77
+ ]
78
+ ]
79
+ end
80
+
81
+ it 'should properly initialize attributes' do
82
+ tests.each do |test|
83
+ expression = test.first
84
+ expected_values = test.last
85
+
86
+ placeholder = described_class.new(expression)
87
+
88
+ expect(placeholder.arg).to eq(expected_values[:arg])
89
+ expect(placeholder.formatter).to eq(expected_values[:formatter])
90
+ expect(placeholder.name).to eq(expected_values[:name])
91
+ expect(placeholder.value).to eq(expected_values[:value])
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2019-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 'spec_helper'
11
+ require './spec/examples/custom_formatter'
12
+
13
+ class Person
14
+ attr_reader :name
15
+
16
+ def initialize(name)
17
+ @name = name
18
+ end
19
+ end
20
+
21
+ describe ::Stringento::Resolver do
22
+ context 'input is hash' do
23
+ let(:input) do
24
+ {
25
+ name: 'Matt'
26
+ }
27
+ end
28
+
29
+ it 'should resolve for existing key' do
30
+ expect(described_class.new.resolve(:name, input)).to eq(input[:name])
31
+ end
32
+
33
+ it 'should resolve nil for missing key' do
34
+ expect(described_class.new.resolve(:doesnt_exist, input)).to eq(nil)
35
+ end
36
+ end
37
+
38
+ context 'input is null' do
39
+ let(:input) { nil }
40
+
41
+ it 'should resolve to null' do
42
+ expect(described_class.new.resolve(:name, input)).to eq(nil)
43
+ end
44
+ end
45
+
46
+ context 'input is an object' do
47
+ let(:input) { Person.new('Matt') }
48
+
49
+ it 'should resolve to null' do
50
+ expect(described_class.new.resolve(:name, input)).to eq(input.name)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2019-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 'spec_helper'
11
+ require './spec/examples/custom_formatter'
12
+ require './spec/examples/indifferent_hash_resolver'
13
+ require './spec/examples/nested_hash_resolver'
14
+
15
+ describe ::Stringento do
16
+ context 'without custom formatter and resolver' do
17
+ describe '#evaluate' do
18
+ let(:tests) do
19
+ [
20
+ [
21
+ 'matt is a {animal}',
22
+ { 'animal' => 'giraffe' },
23
+ 'matt is a giraffe'
24
+ ],
25
+ [
26
+ 'matt is a {animal}',
27
+ { animal: 'giraffe' },
28
+ 'matt is a '
29
+ ]
30
+ ]
31
+ end
32
+
33
+ it 'should evaluate' do
34
+ tests.each do |test|
35
+ expression = test[0]
36
+ input = test[1]
37
+ output = test[2]
38
+
39
+ actual = described_class.evaluate(expression, input)
40
+
41
+ expect(actual).to eq(output)
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ context 'with custom formatter and resolver' do
48
+ describe '#evaluate' do
49
+ let(:tests) do
50
+ [
51
+ [
52
+ 'matt is a {animal}',
53
+ { 'animal' => 'giraffe' },
54
+ 'matt is a giraffe'
55
+ ],
56
+ [
57
+ 'matt is a {animal}',
58
+ { animal: 'giraffe' },
59
+ 'matt is a giraffe'
60
+ ],
61
+ [
62
+ '{other} {value} {types} {are} {ok} {bro}',
63
+ {
64
+ are: nil,
65
+ bro: 0,
66
+ ok: nil,
67
+ other: 1,
68
+ types: 12.3,
69
+ value: false
70
+ },
71
+ '1 false 12.3 0'
72
+ ],
73
+ [
74
+ 'matt is an elephant: {elephant::yes_no}',
75
+ { elephant: true },
76
+ 'matt is an elephant: Yes'
77
+ ],
78
+ [
79
+ 'matt is an ostrich: {ostrich::yes_no}',
80
+ { ostrich: false },
81
+ 'matt is an ostrich: No'
82
+ ],
83
+ [
84
+ 'matt is a cat: {cat::yes_no}',
85
+ { cat: nil },
86
+ 'matt is a cat: No'
87
+ ],
88
+ [
89
+ 'matt is a bird: {bird::yes_no_unknown}',
90
+ { bird: true },
91
+ 'matt is a bird: Yes'
92
+ ],
93
+ [
94
+ 'matt is an airplane: {airplane::yes_no_unknown}',
95
+ { airplane: false },
96
+ 'matt is an airplane: No'
97
+ ],
98
+ [
99
+ 'matt is a dog: {dog::yes_no_unknown}',
100
+ { dog: nil },
101
+ 'matt is a dog: Unknown'
102
+ ]
103
+ ]
104
+ end
105
+
106
+ let(:resolver) { IndifferentHashResolver.new }
107
+
108
+ let(:formatter) { CustomFormatter.new }
109
+
110
+ it 'should evaluate' do
111
+ tests.each do |test|
112
+ expression = test[0]
113
+ input = test[1]
114
+ output = test[2]
115
+
116
+ actual = described_class.evaluate(
117
+ expression,
118
+ input,
119
+ resolver: resolver,
120
+ formatter: formatter
121
+ )
122
+
123
+ expect(actual).to eq(output)
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ describe 'README examples' do
130
+ specify 'Getting Started example works' do
131
+ example = 'The {fox_speed} brown fox jumps over the {dog_speed} dog'
132
+ input = { 'fox_speed' => 'quick', 'dog_speed' => 'lazy' }
133
+ result = Stringento.evaluate(example, input)
134
+
135
+ expect(result).to eq('The quick brown fox jumps over the lazy dog')
136
+ end
137
+
138
+ specify 'Custom Resolution example works' do
139
+ example = 'The {fox.speed} brown fox jumps over the {dog.speed} dog'
140
+ input = { fox: { speed: 'quick' }, dog: { speed: 'lazy' } }
141
+
142
+ result = Stringento.evaluate(example, input, resolver: NestedHashResolver.new)
143
+
144
+ expect(result).to eq('The quick brown fox jumps over the lazy dog')
145
+ end
146
+
147
+ specify 'Custom Formatting example works' do
148
+ example = 'The fox is quick: {fox.quick::yes_no_unknown}'
149
+ input = { fox: { quick: true } }
150
+
151
+ result = Stringento.evaluate(
152
+ example,
153
+ input,
154
+ resolver: NestedHashResolver.new,
155
+ formatter: CustomFormatter.new
156
+ )
157
+
158
+ expect(result).to eq('The fox is quick: Yes')
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright (c) 2019-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 'spec_helper'
11
+ require './spec/examples/custom_formatter'
12
+ require './spec/examples/indifferent_hash_resolver'
13
+
14
+ describe ::Stringento::Template do
15
+ describe '#placeholders' do
16
+ let(:tests) do
17
+ [
18
+ [
19
+ '',
20
+ []
21
+ ],
22
+ [
23
+ nil,
24
+ []
25
+ ],
26
+ [
27
+ 1,
28
+ []
29
+ ],
30
+ [
31
+ false,
32
+ []
33
+ ],
34
+ [
35
+ 'string with none',
36
+ []
37
+ ],
38
+ [
39
+ '{one {inside} another one}',
40
+ ['one {inside']
41
+ ],
42
+ [
43
+ 'matt {is} a {monkeys} uncle.',
44
+ %w[is monkeys]
45
+ ],
46
+ [
47
+ 'matt {is::date::short} a {monkeys::timeAgo} uncle.',
48
+ %w[is::date::short monkeys::timeAgo]
49
+ ]
50
+ ]
51
+ end
52
+
53
+ it 'should parse' do
54
+ tests.each do |test|
55
+ expression = test.first
56
+ expected_values = test.last
57
+
58
+ template = described_class.new(expression)
59
+ placeholder_values = template.placeholders.map(&:value)
60
+
61
+ expect(placeholder_values).to eq(expected_values)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './lib/stringento/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'stringento'
7
+ s.version = Stringento::VERSION
8
+ s.summary = 'Configurable Data Shaper'
9
+
10
+ s.description = <<-DESCRIPTION
11
+ Stringento is a configuration-based object graphing tool which can turn a flat, single dimensional dataset into a structure of deeply nested objects.
12
+ DESCRIPTION
13
+
14
+ s.authors = ['Matthew Ruggio']
15
+ s.email = ['mruggio@bluemarblepayroll.com']
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
19
+ s.homepage = 'https://github.com/bluemarblepayroll/stringento'
20
+ s.license = 'MIT'
21
+
22
+ s.required_ruby_version = '>= 2.3.8'
23
+
24
+ s.add_development_dependency('faker', '~>1')
25
+ s.add_development_dependency('guard-rspec', '~>4.7')
26
+ s.add_development_dependency('pdf-inspector', '~>1')
27
+ s.add_development_dependency('pry', '~>0')
28
+ s.add_development_dependency('rspec', '~> 3.8')
29
+ s.add_development_dependency('rubocop', '~>0.63.1')
30
+ s.add_development_dependency('simplecov', '~>0.16.1')
31
+ s.add_development_dependency('simplecov-console', '~>0.4.2')
32
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stringento
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Ruggio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faker
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :development
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: guard-rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pdf-inspector
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1'
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: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.63.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.63.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.16.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.16.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov-console
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.4.2
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.4.2
125
+ description: " Stringento is a configuration-based object graphing tool which can
126
+ turn a flat, single dimensional dataset into a structure of deeply nested objects.\n"
127
+ email:
128
+ - mruggio@bluemarblepayroll.com
129
+ executables:
130
+ - console
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - ".editorconfig"
135
+ - ".gitignore"
136
+ - ".rubocop.yml"
137
+ - ".ruby-version"
138
+ - ".travis.yml"
139
+ - CHANGELOG.md
140
+ - Gemfile
141
+ - Gemfile.lock
142
+ - Guardfile
143
+ - LICENSE
144
+ - README.md
145
+ - bin/console
146
+ - lib/stringento.rb
147
+ - lib/stringento/formatter.rb
148
+ - lib/stringento/placeholder.rb
149
+ - lib/stringento/resolver.rb
150
+ - lib/stringento/stringento.rb
151
+ - lib/stringento/template.rb
152
+ - lib/stringento/version.rb
153
+ - spec/examples/custom_formatter.rb
154
+ - spec/examples/indifferent_hash_resolver.rb
155
+ - spec/examples/nested_hash_resolver.rb
156
+ - spec/spec_helper.rb
157
+ - spec/stringento/formatter_spec.rb
158
+ - spec/stringento/placeholder_spec.rb
159
+ - spec/stringento/resolver_spec.rb
160
+ - spec/stringento/stringento_spec.rb
161
+ - spec/stringento/template_spec.rb
162
+ - stringento.gemspec
163
+ homepage: https://github.com/bluemarblepayroll/stringento
164
+ licenses:
165
+ - MIT
166
+ metadata: {}
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: 2.3.8
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubygems_version: 3.0.1
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: Configurable Data Shaper
186
+ test_files:
187
+ - spec/examples/custom_formatter.rb
188
+ - spec/examples/indifferent_hash_resolver.rb
189
+ - spec/examples/nested_hash_resolver.rb
190
+ - spec/spec_helper.rb
191
+ - spec/stringento/formatter_spec.rb
192
+ - spec/stringento/placeholder_spec.rb
193
+ - spec/stringento/resolver_spec.rb
194
+ - spec/stringento/stringento_spec.rb
195
+ - spec/stringento/template_spec.rb