dry_object_mapper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8c8af17b6fc995377722b1461d2c95b0d208265c8806e3c33a61ef022249623a
4
+ data.tar.gz: e9acfbd42a0898e9d93642a3ff15108bd2a3e708122f2093ebef03d95fab9294
5
+ SHA512:
6
+ metadata.gz: b51d6e2f6a83bf0b1ad4e2359109406546dd6d0b39e7fcb6e88dd6b3d645ec156b6700d8c20d8ba61b480ece971cd33f658938e83ac01f8185c5f5a9856441b7
7
+ data.tar.gz: 7f9517076c23639e0a8b486bdf7352684932587ed3412b60bae1ce99a85054fb58a3fca5886ce4470e001843548867fdc4729ce08bb86fb3996dafd379c35b11
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-01-17
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders 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, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at janeterziev@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Jane-Terziev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # DryObjectMapper
2
+
3
+ Transform your ActiveRecord objects into Dry::Struct DTO objects.
4
+ Benefits of returning a DTO instead of an ActiveRecord object from your services:
5
+ 1. It makes our code more readable, we know exactly what fields we are returning, with type safety.
6
+ 2. It allows us to render the response in any format with ease.
7
+ 3. It prevents us from accessing the Data Layer directly from the Presentation Layer, rather than going through the
8
+ Service Layer, preventing accidental queries when accessing data in the views.
9
+ 4. It works with other tools that i have built, like dry-swagger, which transforms our DTO's into a valid
10
+ and up to date swagger documentation, with type definitions.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'dry_object_mapper'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ bundle install
23
+
24
+ ## Usage
25
+
26
+ Lets say we have the following Dry::Struct definitions:
27
+
28
+ class Model3Dto < Dry::Struct
29
+ attribute :description, Types::String
30
+ end
31
+
32
+ class Model2Dto < Dry::Struct
33
+ attribute :name, Types::String
34
+ end
35
+
36
+ class Model1Dto < Dry::Struct
37
+ attribute :id, Types::String
38
+ attribute :age, Types::Integer
39
+ attribute :model2_dto, Model2Dto
40
+ attribute :model3_dto, Types::Array.of(Model3Dto)
41
+ end
42
+
43
+ DryObjectMapper::Mapper.call(model.all, Model1Dto)
44
+ => [
45
+ #<Model1Dto id="22bf569c-f47e-473a-a821-a41b23dae927"
46
+ age=25
47
+ model2_dto=#<Model2Dto name="Name">
48
+ model3_dto=[#<Model3Dto description="Description" >]
49
+ >
50
+ ]
51
+
52
+ If we want to add data that is not present in the model objects, we can pass options as an argument to the call method:
53
+
54
+ class Model1Dto < Dry::Struct
55
+ attribute :id, Types::String
56
+ attribute :age, Types::Integer
57
+ attribute :some_counter, Types::Integer
58
+ attribute :model2_dto, Model2Dto
59
+ attribute :model3_dto, Types::Array.of(Model3Dto)
60
+ end
61
+
62
+ DryObjectMapper::Mapper.call(model.all, Model1Dto, { some_counter: 10 })
63
+ => [
64
+ #<Model1Dto id="22bf569c-f47e-473a-a821-a41b23dae927"
65
+ age=25
66
+ some_counter=10
67
+ model2_dto=#<Model2Dto name="Name">
68
+ model3_dto=[#<Model3Dto description="Description" >]
69
+ >
70
+ ]
71
+
72
+ For nested data, pass it as a hash:
73
+
74
+ class Model2Dto < Dry::Struct
75
+ attribute :name, Types::String
76
+ attribute :some_counter, Types::Integer
77
+ end
78
+
79
+ class Model1Dto < Dry::Struct
80
+ attribute :id, Types::String
81
+ attribute :age, Types::Integer
82
+ attribute :model2_dto, Model2Dto
83
+ attribute :model3_dto, Types::Array.of(Model3Dto)
84
+ end
85
+
86
+ DryObjectMapper::Mapper.call(model.all, Model1Dto, { model2_dto: { some_counter: 10 } })
87
+ => [
88
+ #<Model1Dto id="22bf569c-f47e-473a-a821-a41b23dae927"
89
+ age=25
90
+ model2_dto=#<Model2Dto name="Name" some_counter=10 >
91
+ model3_dto=[#<Model3Dto description="Description" >]
92
+ >
93
+ ]
94
+
95
+ ## Development
96
+
97
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
98
+
99
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
100
+
101
+ ## Contributing
102
+
103
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dry_object_mapper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/dry_object_mapper/blob/main/CODE_OF_CONDUCT.md).
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
108
+
109
+ ## Code of Conduct
110
+
111
+ Everyone interacting in the DryObjectMapper project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/dry_object_mapper/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,47 @@
1
+ module DryObjectMapper
2
+ class Mapper
3
+ @@definitions = {}
4
+
5
+ def self.definitions
6
+ @@definitions
7
+ end
8
+
9
+ def self.call(model_object, struct_dto, options = {})
10
+ if @@definitions[struct_dto]
11
+ schema_definition = @@definitions[struct_dto]
12
+ else
13
+ schema_definition = DryStructParser::StructSchemaParser.new.call(struct_dto).keys
14
+ @@definitions[struct_dto] = schema_definition
15
+ end
16
+
17
+ if model_object.respond_to?(:each)
18
+ model_object = model_object.to_a
19
+ result = model_object.map {|it| struct_dto.new(get_model_hash_from_definition(it, schema_definition, options)) }
20
+ else
21
+ result = struct_dto.new(get_model_hash_from_definition(model_object, schema_definition, options))
22
+ end
23
+
24
+ result
25
+ end
26
+
27
+ def self.get_model_hash_from_definition(model_object, schema_definition, options)
28
+ result = {}
29
+ schema_definition.each do |field_name, definition|
30
+ if options&.dig(field_name) && !options.dig(field_name).is_a?(Hash)
31
+ result[field_name] = options[field_name]
32
+ elsif definition[:type] == 'hash'
33
+ result[field_name] = get_model_hash_from_definition(model_object.send(field_name), definition[:keys], options&.dig(field_name))
34
+ elsif definition[:type] == 'array' && !definition[:keys]&.empty?
35
+ result[field_name] = []
36
+ model_object.send(field_name).each do |object|
37
+ result[field_name] << get_model_hash_from_definition(object, definition[:keys], options&.dig(field_name))
38
+ end
39
+ else
40
+ result[field_name] = model_object.send(field_name)
41
+ end
42
+ end
43
+
44
+ result
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ require 'dry-types'
2
+
3
+ module DryObjectMapper
4
+ class Types
5
+ include Dry.Types()
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DryObjectMapper
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dry_object_mapper/version"
4
+ require "dry_struct_parser/struct_schema_parser"
5
+ require_relative "dry_object_mapper/mapper"
6
+ require_relative "dry_object_mapper/types"
7
+
8
+ module DryObjectMapper
9
+ class Error < StandardError; end
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,4 @@
1
+ module DryObjectMapper
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry_object_mapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jane-Terziev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-struct
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: dry_struct_parser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Transform your ActiveRecord objects into Dry::Struct objects
42
+ email:
43
+ - janeterziev@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - CHANGELOG.md
51
+ - CODE_OF_CONDUCT.md
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/dry_object_mapper.rb
56
+ - lib/dry_object_mapper/mapper.rb
57
+ - lib/dry_object_mapper/types.rb
58
+ - lib/dry_object_mapper/version.rb
59
+ - sig/dry_object_mapper.rbs
60
+ homepage: https://github.com/Jane-Terziev/dry_object_mapper
61
+ licenses:
62
+ - MIT
63
+ metadata:
64
+ allowed_push_host: https://rubygems.org
65
+ homepage_uri: https://github.com/Jane-Terziev/dry_object_mapper
66
+ source_code_uri: https://github.com/Jane-Terziev/dry_object_mapper
67
+ changelog_uri: https://github.com/Jane-Terziev/dry_object_mapper
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.6.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.4.10
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Transform your ActiveRecord objects into Dry::Struct objects
87
+ test_files: []