dry-transformer 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/LICENSE +1 -1
  4. data/README.md +1 -1
  5. data/dry-transformer.gemspec +17 -10
  6. data/lib/dry/transformer/hash.rb +2 -1
  7. data/lib/dry/transformer/version.rb +1 -1
  8. metadata +10 -56
  9. data/.codeclimate.yml +0 -12
  10. data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +0 -10
  11. data/.github/ISSUE_TEMPLATE/---bug-report.md +0 -30
  12. data/.github/ISSUE_TEMPLATE/---feature-request.md +0 -18
  13. data/.github/workflows/custom_ci.yml +0 -66
  14. data/.github/workflows/docsite.yml +0 -34
  15. data/.github/workflows/sync_configs.yml +0 -34
  16. data/.gitignore +0 -16
  17. data/.rspec +0 -4
  18. data/.rubocop.yml +0 -95
  19. data/CODE_OF_CONDUCT.md +0 -13
  20. data/CONTRIBUTING.md +0 -29
  21. data/Gemfile +0 -19
  22. data/Rakefile +0 -6
  23. data/docsite/source/built-in-transformations.html.md +0 -47
  24. data/docsite/source/index.html.md +0 -15
  25. data/docsite/source/transformation-objects.html.md +0 -32
  26. data/docsite/source/using-standalone-functions.html.md +0 -82
  27. data/spec/spec_helper.rb +0 -31
  28. data/spec/unit/array/combine_spec.rb +0 -224
  29. data/spec/unit/array_transformations_spec.rb +0 -233
  30. data/spec/unit/class_transformations_spec.rb +0 -50
  31. data/spec/unit/coercions_spec.rb +0 -132
  32. data/spec/unit/conditional_spec.rb +0 -48
  33. data/spec/unit/function_not_found_error_spec.rb +0 -12
  34. data/spec/unit/function_spec.rb +0 -193
  35. data/spec/unit/hash_transformations_spec.rb +0 -490
  36. data/spec/unit/proc_transformations_spec.rb +0 -20
  37. data/spec/unit/recursion_spec.rb +0 -145
  38. data/spec/unit/registry_spec.rb +0 -202
  39. data/spec/unit/store_spec.rb +0 -198
  40. data/spec/unit/transformer/class_interface_spec.rb +0 -350
  41. data/spec/unit/transformer/dsl_spec.rb +0 -15
  42. data/spec/unit/transformer/instance_methods_spec.rb +0 -25
@@ -1,95 +0,0 @@
1
- # this file is managed by dry-rb/devtools project
2
-
3
- AllCops:
4
- TargetRubyVersion: 2.4
5
-
6
- Style/EachWithObject:
7
- Enabled: false
8
-
9
- Style/StringLiterals:
10
- Enabled: true
11
- EnforcedStyle: single_quotes
12
-
13
- Style/Alias:
14
- Enabled: false
15
-
16
- Style/LambdaCall:
17
- Enabled: false
18
-
19
- Style/StabbyLambdaParentheses:
20
- Enabled: false
21
-
22
- Style/FormatString:
23
- Enabled: false
24
-
25
- Style/Documentation:
26
- Enabled: false
27
-
28
- Layout/SpaceInLambdaLiteral:
29
- Enabled: false
30
-
31
- Layout/MultilineMethodCallIndentation:
32
- Enabled: true
33
- EnforcedStyle: indented
34
-
35
- Metrics/LineLength:
36
- Max: 100
37
-
38
- Metrics/MethodLength:
39
- Max: 22
40
-
41
- Metrics/ClassLength:
42
- Max: 150
43
-
44
- Metrics/AbcSize:
45
- Max: 20
46
-
47
- Metrics/BlockLength:
48
- Enabled: false
49
-
50
- Metrics/CyclomaticComplexity:
51
- Enabled: true
52
- Max: 10
53
-
54
- Lint/BooleanSymbol:
55
- Enabled: false
56
-
57
- Style/AccessModifierDeclarations:
58
- Enabled: false
59
-
60
- Style/BlockDelimiters:
61
- Enabled: false
62
-
63
- Layout/IndentFirstArrayElement:
64
- EnforcedStyle: consistent
65
-
66
- Style/ClassAndModuleChildren:
67
- Exclude:
68
- - "spec/**/*_spec.rb"
69
-
70
- Lint/HandleExceptions:
71
- Exclude:
72
- - "spec/spec_helper.rb"
73
-
74
- Naming/FileName:
75
- Exclude:
76
- - "lib/dry-*.rb"
77
-
78
- Style/SymbolArray:
79
- Exclude:
80
- - "spec/**/*_spec.rb"
81
-
82
- Style/ConditionalAssignment:
83
- Enabled: false
84
-
85
- Naming/MethodName:
86
- Enabled: false
87
-
88
- Style/AsciiComments:
89
- Enabled: false
90
-
91
- Style/DateTime:
92
- Enabled: false
93
-
94
- Style/IfUnlessModifier:
95
- Enabled: false
@@ -1,13 +0,0 @@
1
- # Contributor Code of Conduct
2
-
3
- As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
-
5
- We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
-
7
- Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
-
9
- Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
-
11
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
-
13
- This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.4.0, available at [https://www.contributor-covenant.org/version/1/4/code-of-conduct](https://www.contributor-covenant.org/version/1/4/code-of-conduct)
@@ -1,29 +0,0 @@
1
- # Issue Guidelines
2
-
3
- ## Reporting bugs
4
-
5
- If you found a bug, report an issue and describe what's the expected behavior versus what actually happens. If the bug causes a crash, attach a full backtrace. If possible, a reproduction script showing the problem is highly appreciated.
6
-
7
- ## Reporting feature requests
8
-
9
- Report a feature request **only after discussing it first on [discourse.dry-rb.org](https://discourse.dry-rb.org)** where it was accepted. Please provide a concise description of the feature, don't link to a discussion thread, and instead summarize what was discussed.
10
-
11
- ## Reporting questions, support requests, ideas, concerns etc.
12
-
13
- **PLEASE DON'T** - use [discourse.dry-rb.org](http://discourse.dry-rb.org) instead.
14
-
15
- # Pull Request Guidelines
16
-
17
- A Pull Request will only be accepted if it addresses a specific issue that was reported previously, or fixes typos, mistakes in documentation etc.
18
-
19
- Other requirements:
20
-
21
- 1) Do not open a pull request if you can't provide tests along with it. If you have problems writing tests, ask for help in the related issue.
22
- 2) Follow the style conventions of the surrounding code. In most cases, this is standard ruby style.
23
- 3) Add API documentation if it's a new feature
24
- 4) Update API documentation if it changes an existing feature
25
- 5) Bonus points for sending a PR to [github.com/dry-rb/dry-rb.org](github.com/dry-rb/dry-rb.org) which updates user documentation and guides
26
-
27
- # Asking for help
28
-
29
- If these guidelines aren't helpful, and you're stuck, please post a message on [discourse.dry-rb.org](https://discourse.dry-rb.org) or join [our chat](https://dry-rb.zulipchat.com).
data/Gemfile DELETED
@@ -1,19 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- gem 'rake'
6
- gem 'rspec', '~> 3.8'
7
- gem 'dry-equalizer', '~> 0.2'
8
-
9
- platform :mri do
10
- gem 'codacy-coverage', require: false
11
- gem 'simplecov', require: false
12
- end
13
-
14
- group :tools do
15
- gem 'pry'
16
- gem 'byebug', platform: :mri
17
- gem 'benchmark-ips'
18
- gem 'ossy', git: 'https://github.com/solnic/ossy.git', branch: 'master', platform: :mri
19
- end
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require 'bundler/gem_tasks'
2
- require 'rspec/core/rake_task'
3
-
4
- task default: :spec
5
-
6
- RSpec::Core::RakeTask.new(:spec)
@@ -1,47 +0,0 @@
1
- ---
2
- title: Built-in transformation
3
- layout: gem-single
4
- name: dry-transformer
5
- ---
6
-
7
- `dry-transformer` comes with a lot of built-in functions. They come in the form of modules with class methods, which you can import into a registry:
8
-
9
- * [Coercions](https://www.rubydoc.info/gems/dry-transformer/Dry/Transformer/Coercions)
10
- * [Array transformations](https://www.rubydoc.info/gems/dry-transformer/Dry/Transformer/ArrayTransformations)
11
- * [Hash transformations](https://www.rubydoc.info/gems/dry-transformer/Dry/Transformer/HashTransformations)
12
- * [Class transformations](https://www.rubydoc.info/gems/dry-transformer/Dry/Transformer/ClassTransformations)
13
- * [Proc transformations](https://www.rubydoc.info/gems/dry-transformer/Dry/Transformer/ProcTransformations)
14
- * [Conditional](https://www.rubydoc.info/gems/dry-transformer/Dry/Transformer/Conditional)
15
- * [Recursion](https://www.rubydoc.info/gems/dry-transformer/Dry/Transformer/Recursion)
16
-
17
- You can import everything with:
18
-
19
- ```ruby
20
- module T
21
- extend Dry::Transformer::Registry
22
-
23
- import Dry::Transformer::Coercions
24
- import Dry::Transformer::ArrayTransformations
25
- import Dry::Transformer::HashTransformations
26
- import Dry::Transformer::ClassTransformations
27
- import Dry::Transformer::ProcTransformations
28
- import Dry::Transformer::Conditional
29
- import Dry::Transformer::Recursion
30
- end
31
-
32
- T[:to_string].(:abc) # => 'abc'
33
- ```
34
-
35
- Or import selectively with:
36
-
37
- ```ruby
38
- module T
39
- extend Dry::Transformer::Registry
40
-
41
- import :to_string, from: Dry::Transformer::Coercions, as: :stringify
42
- end
43
-
44
- T[:stringify].(:abc) # => 'abc'
45
- T[:to_string].(:abc)
46
- # => Dry::Transformer::FunctionNotFoundError: No registered function T[:to_string]
47
- ```
@@ -1,15 +0,0 @@
1
- ---
2
- title: Introduction
3
- description: Data transformation toolkit
4
- layout: gem-single
5
- type: gem
6
- name: dry-transformer
7
- sections:
8
- - transformation-objects
9
- - built-in-transformations
10
- - using-standalone-functions
11
- ---
12
-
13
- dry-transformer is a library that allows you to compose procs into a functional pipeline using left-to-right function composition.
14
-
15
- The approach came from Functional Programming, where simple functions are composed into more complex functions in order to transform some data. It works like `|>` in Elixir or `>>` in F#. dry-transformer provides a mechanism to define and compose transformations, along with a number of built-in transformations.
@@ -1,32 +0,0 @@
1
- ---
2
- title: Transformation objects
3
- name: dry-transformer
4
- layout: gem-single
5
- ---
6
-
7
- You can define transformation classes using the DSL which converts every method call to its corresponding transformation, and composes these transformations into a transformation pipeline. Here's a simple example where the default registry is used:
8
-
9
- ```ruby
10
- class MyMapper < Dry::Transformer[Dry::Transformer::Registry]
11
- define! do
12
- map_array do
13
- symbolize_keys
14
- rename_keys user_name: :name
15
- nest :address, [:city, :street, :zipcode]
16
- end
17
- end
18
- end
19
-
20
- mapper = MyMapper.new
21
-
22
- mapper.(
23
- [
24
- { 'user_name' => 'Jane',
25
- 'city' => 'NYC',
26
- 'street' => 'Street 1',
27
- 'zipcode' => '123'
28
- }
29
- ]
30
- )
31
- # => [{:name=>"Jane", :address=>{:city=>"NYC", :street=>"Street 1", :zipcode=>"123"}}]
32
- ```
@@ -1,82 +0,0 @@
1
- ---
2
- title: Using standalone functions
3
- name: dry-transformer
4
- layout: gem-single
5
- ---
6
-
7
- You can use `dry-transformer` and its function registry feature stand-alone, without the need to define transformation classes. To do so, simply define a module and extend it with the registry API:
8
-
9
- ``` ruby
10
- require 'json'
11
- require 'dry/transformer/all'
12
-
13
- # create your own local registry for transformation functions
14
- module Functions
15
- extend Dry::Transformer::Registry
16
- end
17
-
18
- # import necessary functions from other transprocs...
19
- module Functions
20
- # import all singleton methods from a module/class
21
- import Dry::Transformer::HashTransformations
22
- import Dry::Transformer::ArrayTransformations
23
- end
24
-
25
- # ...or from any external library
26
- require 'dry-inflector'
27
-
28
- Inflector = Dry::Inflector.new
29
-
30
- module Functions
31
- # import only necessary singleton methods from a module/class
32
- # and rename them locally
33
- import :camelize, from: Inflector, as: :camel_case
34
- end
35
-
36
- def t(*args)
37
- Functions[*args]
38
- end
39
-
40
- # use imported transformation
41
- transformation = t(:camel_case)
42
-
43
- transformation.call 'i_am_a_camel'
44
- # => "IAmACamel"
45
-
46
- transformation = t(:map_array, (
47
- t(:symbolize_keys).>> t(:rename_keys, user_name: :user)
48
- )).>> t(:wrap, :address, [:city, :street, :zipcode])
49
-
50
- transformation.call(
51
- [
52
- { 'user_name' => 'Jane',
53
- 'city' => 'NYC',
54
- 'street' => 'Street 1',
55
- 'zipcode' => '123' }
56
- ]
57
- )
58
- # => [{:user=>"Jane", :address=>{:city=>"NYC", :street=>"Street 1", :zipcode=>"123"}}]
59
-
60
- # define your own composable transformation easily
61
- transformation = t(-> v { JSON.dump(v) })
62
-
63
- transformation.call(name: 'Jane')
64
- # => "{\"name\":\"Jane\"}"
65
-
66
- # ...or add it to registered functions via singleton method of the registry
67
- module Functions
68
- # ...
69
-
70
- def self.load_json(v)
71
- JSON.load(v)
72
- end
73
- end
74
-
75
- # ...or add it to registered functions via .register method
76
- Functions.register(:load_json) { |v| JSON.load(v) }
77
-
78
- transformation = t(:load_json) >> t(:map_array, t(:symbolize_keys))
79
-
80
- transformation.call('[{"name":"Jane"}]')
81
- # => [{ :name => "Jane" }]
82
- ```
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- if ENV['COVERAGE'] == 'true'
4
- require 'codacy-coverage'
5
- Codacy::Reporter.start
6
- end
7
-
8
- begin
9
- require 'byebug'
10
- rescue LoadError;end
11
-
12
- require 'dry/transformer/all'
13
-
14
- root = Pathname(__FILE__).dirname
15
- Dir[root.join('support/*.rb').to_s].each { |f| require f }
16
-
17
- # Namespace holding all objects created during specs
18
- module Test
19
- def self.remove_constants
20
- constants.each(&method(:remove_const))
21
- end
22
- end
23
-
24
- RSpec.configure do |config|
25
- config.after do
26
- Test.remove_constants
27
- end
28
-
29
- config.disable_monkey_patching!
30
- config.warnings = true
31
- end
@@ -1,224 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'forwardable'
4
-
5
- RSpec.describe Dry::Transformer::ArrayTransformations do
6
- describe '.combine' do
7
- subject(:result) { described_class.t(:combine, mappings)[input] }
8
-
9
- let(:input) { [[]] }
10
- let(:mappings) { [] }
11
-
12
- it { is_expected.to be_a(Array) }
13
-
14
- it { is_expected.to eq([]) }
15
-
16
- context 'without groups' do
17
- let(:input) do
18
- [
19
- [
20
- {name: 'Jane', email: 'jane@doe.org'}.freeze,
21
- {name: 'Joe', email: 'joe@doe.org'}.freeze
22
- ].freeze
23
- ].freeze
24
- end
25
-
26
- it { is_expected.to eq input.first }
27
- end
28
-
29
- context 'with one group' do
30
- let(:input) do
31
- [
32
- [
33
- {name: 'Jane', email: 'jane@doe.org'}.freeze,
34
- {name: 'Joe', email: 'joe@doe.org'}.freeze
35
- ].freeze,
36
- [
37
- [
38
- {user: 'Jane', title: 'One'}.freeze,
39
- {user: 'Jane', title: 'Two'}.freeze,
40
- {user: 'Joe', title: 'Three'}.freeze
41
- ]
42
- ]
43
- ].freeze
44
- end
45
- let(:mappings) { [[:tasks, {name: :user}]] }
46
-
47
- it 'merges hashes from arrays using provided join keys' do
48
- output = [
49
- {name: 'Jane', email: 'jane@doe.org', tasks: [
50
- {user: 'Jane', title: 'One'},
51
- {user: 'Jane', title: 'Two'}
52
- ]},
53
- {name: 'Joe', email: 'joe@doe.org', tasks: [
54
- {user: 'Joe', title: 'Three'}
55
- ]}
56
- ]
57
- is_expected.to eql(output)
58
- end
59
- end
60
-
61
- context 'with empty nodes' do
62
- let(:input) do
63
- [
64
- [{name: 'Jane', email: 'jane@doe.org'}.freeze].freeze,
65
- [
66
- []
67
- ]
68
- ].freeze
69
- end
70
-
71
- let(:mappings) { [[:tasks, {name: :user}]] }
72
-
73
- it { is_expected.to eq([{name: 'Jane', email: 'jane@doe.org', tasks: []}]) }
74
- end
75
-
76
- context 'with double mapping' do
77
- let(:input) do
78
- [
79
- [
80
- {name: 'Jane', email: 'jane@doe.org'}.freeze
81
- ].freeze,
82
- [
83
- [
84
- {user: 'Jane', user_email: 'jane@doe.org', title: 'One'}.freeze,
85
- {user: 'Jane', user_email: '', title: 'Two'}.freeze
86
- ].freeze
87
- ].freeze
88
- ].freeze
89
- end
90
-
91
- let(:mappings) { [[:tasks, {name: :user, email: :user_email}]] }
92
-
93
- it 'searches by two keys simultaneously' do
94
- output = [
95
- {name: 'Jane', email: 'jane@doe.org', tasks: [
96
- {user: 'Jane', user_email: 'jane@doe.org', title: 'One'}
97
- ]}
98
- ]
99
- is_expected.to eql(output)
100
- end
101
- end
102
-
103
- context 'with non-array argument' do
104
- let(:input) do
105
- 123
106
- end
107
-
108
- let(:mappings) { [[:page, {page_id: :id}]] }
109
-
110
- it { is_expected.to eq(123) }
111
- end
112
-
113
- context 'with empty nested array' do
114
- let(:input) do
115
- [
116
- [],
117
- [
118
- []
119
- ]
120
- ]
121
- end
122
-
123
- let(:mappings) { [[:menu_items, {id: :menu_id}, [[:page, {page_id: :id}]]]] }
124
-
125
- it 'does not crash' do
126
- expect { result }.not_to raise_error
127
- end
128
- end
129
-
130
- context 'with enumerable input' do
131
- let(:my_enumerator) do
132
- Class.new do
133
- include Enumerable
134
- extend Forwardable
135
-
136
- def_delegator :@array, :each
137
-
138
- def initialize(array)
139
- @array = array
140
- end
141
- end
142
- end
143
-
144
- let(:input) do
145
- [
146
- my_enumerator.new([
147
- {name: 'Jane', email: 'jane@doe.org'}.freeze,
148
- {name: 'Joe', email: 'joe@doe.org'}.freeze
149
- ].freeze),
150
- my_enumerator.new([
151
- my_enumerator.new([
152
- {user: 'Jane', title: 'One'}.freeze,
153
- {user: 'Jane', title: 'Two'}.freeze,
154
- {user: 'Joe', title: 'Three'}.freeze
155
- ].freeze)
156
- ].freeze)
157
- ].freeze
158
- end
159
- let(:mappings) { [[:tasks, {name: :user}]] }
160
-
161
- it 'supports enumerables as well' do
162
- output = [
163
- {name: 'Jane', email: 'jane@doe.org', tasks: [
164
- {user: 'Jane', title: 'One'},
165
- {user: 'Jane', title: 'Two'}
166
- ]},
167
- {name: 'Joe', email: 'joe@doe.org', tasks: [
168
- {user: 'Joe', title: 'Three'}
169
- ]}
170
- ]
171
- is_expected.to eql(output)
172
- end
173
- end
174
-
175
- describe 'integration test' do
176
- let(:input) do
177
- [
178
- [
179
- {name: 'Jane', email: 'jane@doe.org'},
180
- {name: 'Joe', email: 'joe@doe.org'}
181
- ],
182
- [
183
- [
184
- # user tasks
185
- [
186
- {user: 'Jane', title: 'One'},
187
- {user: 'Jane', title: 'Two'},
188
- {user: 'Joe', title: 'Three'}
189
- ],
190
- [
191
- # task tags
192
- [
193
- {task: 'One', tag: 'red'},
194
- {task: 'Three', tag: 'blue'}
195
- ]
196
- ]
197
- ]
198
- ]
199
- ]
200
- end
201
-
202
- let(:mappings) { [[:tasks, {name: :user}, [[:tags, title: :task]]]] }
203
-
204
- it 'merges hashes from arrays using provided join keys' do
205
- output = [
206
- {name: 'Jane', email: 'jane@doe.org', tasks: [
207
- {user: 'Jane', title: 'One', tags: [{task: 'One', tag: 'red'}]},
208
- {user: 'Jane', title: 'Two', tags: []}
209
- ]},
210
- {
211
- name: 'Joe', email: 'joe@doe.org', tasks: [
212
- {
213
- user: 'Joe', title: 'Three', tags: [
214
- {task: 'Three', tag: 'blue'}
215
- ]
216
- }
217
- ]
218
- }
219
- ]
220
- is_expected.to eql(output)
221
- end
222
- end
223
- end
224
- end