simple_object_serialization 0.2.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0fe331a2b91828fbc51fcfe6d2f69c7b160ed985319215056cfbe4ff923c0bc7
4
+ data.tar.gz: 9f781553a3413123c43294d90d1a967ad71ee577b92976a9517f6d8c31e7720e
5
+ SHA512:
6
+ metadata.gz: 250404dd9fff54eaf4e80608764513946c727a8b12cb60a3afb30a6bd1a37556f332b427fe6dc5d984330f09f02a7daeb904c4ee034ca3cf7fce6c82f9b00ffe
7
+ data.tar.gz: 7986e700fabe1bc783119add95d9d34b1fab909ab0794602ad4b738d0493ccdd6bdcab258e5801c60d8e4a8759f44b013c7b5f34067a69aead08a6d3a59b9b6e
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "bundler"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "daily"
@@ -0,0 +1,21 @@
1
+ name: Lint
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ ruby:
12
+ - "3.0"
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - name: Set up Ruby
16
+ uses: ruby/setup-ruby@v1
17
+ with:
18
+ ruby-version: ${{matrix.ruby}}
19
+ bundler-cache: true
20
+ - name: Run rubocop
21
+ run: bundle exec rake rubocop
@@ -0,0 +1,22 @@
1
+ name: Specs
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ ruby:
12
+ - "3.0"
13
+ - "2.7"
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby
17
+ uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{matrix.ruby}}
20
+ bundler-cache: true
21
+ - name: Run specs
22
+ run: bundle exec rake spec
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,24 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ TargetRubyVersion: 2.7
8
+
9
+ Layout/LineLength:
10
+ Max: 150
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Metrics/BlockLength:
16
+ Exclude:
17
+ - simple_object_serialization.gemspec
18
+ - "spec/**/*.rb"
19
+
20
+ RSpec/MultipleExpectations:
21
+ Max: 20
22
+
23
+ RSpec/ExampleLength:
24
+ Max: 50
data/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [0.2.0] - 2022-01-20
8
+
9
+ ### Removed
10
+ - Removed dry and active-support dependencies
11
+
12
+ ### Added
13
+ - Added Ruby 3.1.0 to specs matrix
14
+
15
+ ## [0.1.0] - 2021-10-28
16
+
17
+ First public 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 norbert.malecki@icloud.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/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in enumeration.gemspec
6
+ gemspec
7
+
8
+ gem 'rake'
9
+ gem 'rspec'
10
+ gem 'rubocop'
11
+ gem 'rubocop-performance'
12
+ gem 'rubocop-rake'
13
+ gem 'rubocop-rspec'
14
+
15
+ gem 'kaminari'
data/Gemfile.lock ADDED
@@ -0,0 +1,124 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simple_object_serialization (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionview (7.0.0)
10
+ activesupport (= 7.0.0)
11
+ builder (~> 3.1)
12
+ erubi (~> 1.4)
13
+ rails-dom-testing (~> 2.0)
14
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
15
+ activemodel (7.0.0)
16
+ activesupport (= 7.0.0)
17
+ activerecord (7.0.0)
18
+ activemodel (= 7.0.0)
19
+ activesupport (= 7.0.0)
20
+ activesupport (7.0.0)
21
+ concurrent-ruby (~> 1.0, >= 1.0.2)
22
+ i18n (>= 1.6, < 2)
23
+ minitest (>= 5.1)
24
+ tzinfo (~> 2.0)
25
+ ast (2.4.2)
26
+ builder (3.2.4)
27
+ coderay (1.1.3)
28
+ concurrent-ruby (1.1.9)
29
+ crass (1.0.6)
30
+ diff-lcs (1.4.4)
31
+ erubi (1.10.0)
32
+ i18n (1.8.11)
33
+ concurrent-ruby (~> 1.0)
34
+ kaminari (1.2.2)
35
+ activesupport (>= 4.1.0)
36
+ kaminari-actionview (= 1.2.2)
37
+ kaminari-activerecord (= 1.2.2)
38
+ kaminari-core (= 1.2.2)
39
+ kaminari-actionview (1.2.2)
40
+ actionview
41
+ kaminari-core (= 1.2.2)
42
+ kaminari-activerecord (1.2.2)
43
+ activerecord
44
+ kaminari-core (= 1.2.2)
45
+ kaminari-core (1.2.2)
46
+ loofah (2.13.0)
47
+ crass (~> 1.0.2)
48
+ nokogiri (>= 1.5.9)
49
+ method_source (1.0.0)
50
+ minitest (5.15.0)
51
+ nokogiri (1.13.1-x86_64-darwin)
52
+ racc (~> 1.4)
53
+ nokogiri (1.13.1-x86_64-linux)
54
+ racc (~> 1.4)
55
+ parallel (1.21.0)
56
+ parser (3.1.0.0)
57
+ ast (~> 2.4.1)
58
+ pry (0.14.1)
59
+ coderay (~> 1.1)
60
+ method_source (~> 1.0)
61
+ racc (1.6.0)
62
+ rails-dom-testing (2.0.3)
63
+ activesupport (>= 4.2.0)
64
+ nokogiri (>= 1.6)
65
+ rails-html-sanitizer (1.4.2)
66
+ loofah (~> 2.3)
67
+ rainbow (3.1.1)
68
+ rake (13.0.6)
69
+ regexp_parser (2.2.0)
70
+ rexml (3.2.5)
71
+ rspec (3.10.0)
72
+ rspec-core (~> 3.10.0)
73
+ rspec-expectations (~> 3.10.0)
74
+ rspec-mocks (~> 3.10.0)
75
+ rspec-core (3.10.1)
76
+ rspec-support (~> 3.10.0)
77
+ rspec-expectations (3.10.1)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.10.0)
80
+ rspec-mocks (3.10.2)
81
+ diff-lcs (>= 1.2.0, < 2.0)
82
+ rspec-support (~> 3.10.0)
83
+ rspec-support (3.10.2)
84
+ rubocop (1.25.0)
85
+ parallel (~> 1.10)
86
+ parser (>= 3.1.0.0)
87
+ rainbow (>= 2.2.2, < 4.0)
88
+ regexp_parser (>= 1.8, < 3.0)
89
+ rexml
90
+ rubocop-ast (>= 1.15.1, < 2.0)
91
+ ruby-progressbar (~> 1.7)
92
+ unicode-display_width (>= 1.4.0, < 3.0)
93
+ rubocop-ast (1.15.1)
94
+ parser (>= 3.0.1.1)
95
+ rubocop-performance (1.13.2)
96
+ rubocop (>= 1.7.0, < 2.0)
97
+ rubocop-ast (>= 0.4.0)
98
+ rubocop-rake (0.6.0)
99
+ rubocop (~> 1.0)
100
+ rubocop-rspec (2.7.0)
101
+ rubocop (~> 1.19)
102
+ ruby-progressbar (1.11.0)
103
+ tzinfo (2.0.4)
104
+ concurrent-ruby (~> 1.0)
105
+ unicode-display_width (2.1.0)
106
+
107
+ PLATFORMS
108
+ x86_64-darwin-20
109
+ x86_64-darwin-21
110
+ x86_64-linux
111
+
112
+ DEPENDENCIES
113
+ kaminari
114
+ pry
115
+ rake
116
+ rspec
117
+ rubocop
118
+ rubocop-performance
119
+ rubocop-rake
120
+ rubocop-rspec
121
+ simple_object_serialization!
122
+
123
+ BUNDLED WITH
124
+ 2.3.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Norbert Małecki
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,88 @@
1
+ # SimpleObjectSerialization
2
+
3
+ Serialization system for Ruby with awsesome features!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'simple_object_serialization'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install serializer
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ class UserSerializer < SimpleObjectSerialization::Entity
25
+ object_alias :user
26
+
27
+ define_attribute :index, if: proc { options[:index] } do
28
+ options[:index]
29
+ end
30
+
31
+ define_attribute :id
32
+ define_attribute :email
33
+ define_attribute :login, if: proc { !email.nil? } do
34
+ login
35
+ end
36
+
37
+ define_attribute :created_at
38
+ define_attribute :updated_at, if: proc { !email.nil? } do
39
+ user.created_at
40
+ end
41
+
42
+ private
43
+
44
+ def email
45
+ user.email
46
+ end
47
+
48
+ def login
49
+ user.email.split('@').first
50
+ end
51
+ end
52
+ ```
53
+
54
+
55
+ ```ruby
56
+ User = Struct.new(:id, :email, :created_at)
57
+
58
+ user = User.new(1, 'user@example.com', DateTime.new(2020, 1, 1))
59
+ users = Kaminari.paginate_array(Array.new(4) { user.dup }).page(2).per(2)
60
+
61
+ UserSerializer.call(user, meta: { current_time: Time.now })
62
+ #=> {:id=>1, :email=>"user@example.com", :login=>"user", :created_at=>Wed, 01 Jan 2020 00:00:00 +0000, :updated_at=>Wed, 01 Jan 2020 00:00:00 +0000}
63
+
64
+ UserSerializer.serialize(user, meta: { current_time: Time.now })
65
+ #=> "{\"data\":{\"id\":1,\"email\":\"user@example.com\",\"login\":\"user\",\"created_at\":\"2020-01-01T00:00:00+00:00\",\"updated_at\":\"2020-01-01T00:00:00+00:00\"},\"meta\":{\"current_time\":\"2021-10-28T18:44:07.044+02:00\"}}"
66
+
67
+ UserSerializer.serialize_collection(users, meta: { current_time: Time.now })
68
+ #=> "{\"data\":[{\"index\":0,\"id\":1,\"email\":\"user@example.com\",\"login\":\"user\",\"created_at\":\"2020-01-01T00:00:00+00:00\",\"updated_at\":\"2020-01-01T00:00:00+00:00\"},{\"index\":1,\"id\":1,\"email\":\"user@example.com\",\"login\":\"user\",\"created_at\":\"2020-01-01T00:00:00+00:00\",\"updated_at\":\"2020-01-01T00:00:00+00:00\"}],\"meta\":{\"current_time\":\"2021-10-28T18:44:22.140+02:00\",\"total_count\":4,\"total_pages\":2,\"per_page\":2,\"prev_page\":1,\"current_page\":2,\"next_page\":null}}"
69
+
70
+ ```
71
+
72
+ ## Development
73
+
74
+ 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.
75
+
76
+ 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).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/norbertmaleckii/simple-object-serialization-rb. 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/norbertmaleckii/simple-object-serialization-rb/blob/main/CODE_OF_CONDUCT.md).
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
85
+
86
+ ## Code of Conduct
87
+
88
+ Everyone interacting in the Serializer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/norbertmaleckii/simple-object-serialization-rb/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]
data/bin/console ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'simple_object_serialization'
6
+
7
+ require_relative '../examples/user_serializer'
8
+
9
+ require 'pry'
10
+ require 'irb'
11
+
12
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'kaminari'
5
+
6
+ class UserSerializer < SimpleObjectSerialization::Entity
7
+ object_alias :user
8
+
9
+ define_attribute :index, if: proc { options[:index] } do
10
+ options[:index]
11
+ end
12
+
13
+ define_attribute :id
14
+ define_attribute :email
15
+ define_attribute :login, if: proc { !email.nil? } do
16
+ login
17
+ end
18
+
19
+ define_attribute :created_at
20
+ define_attribute :updated_at, if: proc { !email.nil? } do
21
+ user.created_at
22
+ end
23
+
24
+ private
25
+
26
+ def email
27
+ user.email
28
+ end
29
+
30
+ def login
31
+ user.email.split('@').first
32
+ end
33
+ end
34
+
35
+ User = Struct.new(:id, :email, :created_at)
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleObjectSerialization
4
+ class Attribute
5
+ attr_reader :name, :options, :block
6
+
7
+ def initialize(name, options, block)
8
+ @name = name
9
+ @options = options
10
+ @block = block
11
+ end
12
+
13
+ def skip_for?(serializer)
14
+ options[:if] ? !serializer.instance_eval(&options[:if]) : false
15
+ end
16
+
17
+ def value_for(serializer)
18
+ block ? serializer.instance_eval(&block) : serializer.object.public_send(name)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleObjectSerialization
4
+ class Attributes
5
+ attr_reader :attributes
6
+
7
+ def initialize
8
+ @attributes = []
9
+ end
10
+
11
+ def push(attribute)
12
+ attributes.push(attribute)
13
+ end
14
+
15
+ def hash_for(serializer)
16
+ {}.tap do |hash|
17
+ attributes.each do |attribute|
18
+ next if attribute.skip_for?(serializer)
19
+
20
+ hash[attribute.name] = attribute.value_for(serializer)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleObjectSerialization
4
+ class CollectionSerializer
5
+ attr_reader :serializer_class, :collection, :options
6
+
7
+ def initialize(serializer_class, collection, options)
8
+ @serializer_class = serializer_class
9
+ @collection = collection
10
+ @options = options
11
+ end
12
+
13
+ def self.call(*params, **options, &block)
14
+ new(*params, **options).call(&block)
15
+ end
16
+
17
+ def call
18
+ {
19
+ data: data,
20
+ meta: meta
21
+ }
22
+ end
23
+
24
+ private
25
+
26
+ def data
27
+ collection.map.with_index do |object, index|
28
+ serializer_class.call(object, options.merge(index: index))
29
+ end
30
+ end
31
+
32
+ def meta
33
+ Hash(options[:meta]).tap do |hash|
34
+ hash.merge!(pagination) if collection.respond_to?(:current_page)
35
+ end
36
+ end
37
+
38
+ def pagination
39
+ {
40
+ total_count: collection.total_count,
41
+ total_pages: collection.total_pages,
42
+ per_page: collection.limit_value,
43
+ prev_page: collection.prev_page,
44
+ current_page: collection.current_page,
45
+ next_page: collection.next_page
46
+ }
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleObjectSerialization
4
+ class Entity
5
+ attr_reader :object, :options
6
+
7
+ def initialize(object, options = {})
8
+ @object = object
9
+ @options = options
10
+ end
11
+
12
+ def self.call(*params, **options, &block)
13
+ new(*params, **options).call(&block)
14
+ end
15
+
16
+ def self.object_alias(name)
17
+ define_method(name) do
18
+ object
19
+ end
20
+ end
21
+
22
+ def self.define_attribute(name, options = {}, &block)
23
+ attribute = Attribute.new(name, options, block)
24
+
25
+ attributes.push(attribute)
26
+ end
27
+
28
+ def self.attributes
29
+ @attributes ||= Attributes.new
30
+ end
31
+
32
+ def self.serialize_collection(collection, options = {})
33
+ hash = CollectionSerializer.call(self, collection, options)
34
+
35
+ JSON.generate(hash)
36
+ end
37
+
38
+ def self.serialize(object, options = {})
39
+ hash = ObjectSerializer.call(self, object, options)
40
+
41
+ JSON.generate(hash)
42
+ end
43
+
44
+ def call
45
+ self.class.attributes.hash_for(self)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleObjectSerialization
4
+ class ObjectSerializer
5
+ attr_reader :serializer_class, :object, :options
6
+
7
+ def initialize(serializer_class, object, options)
8
+ @serializer_class = serializer_class
9
+ @object = object
10
+ @options = options
11
+ end
12
+
13
+ def self.call(*params, **options, &block)
14
+ new(*params, **options).call(&block)
15
+ end
16
+
17
+ def call
18
+ {
19
+ data: data,
20
+ meta: meta
21
+ }
22
+ end
23
+
24
+ private
25
+
26
+ def data
27
+ serializer_class.call(object, options)
28
+ end
29
+
30
+ def meta
31
+ Hash(options[:meta])
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleObjectSerialization
4
+ VERSION = '0.2.0'
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ require_relative 'simple_object_serialization/version'
6
+ require_relative 'simple_object_serialization/attribute'
7
+ require_relative 'simple_object_serialization/attributes'
8
+ require_relative 'simple_object_serialization/collection_serializer'
9
+ require_relative 'simple_object_serialization/object_serializer'
10
+ require_relative 'simple_object_serialization/entity'
11
+
12
+ module SimpleObjectSerialization
13
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/simple_object_serialization/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'simple_object_serialization'
7
+ spec.version = SimpleObjectSerialization::VERSION
8
+ spec.authors = ['Norbert Małecki']
9
+ spec.email = ['norbert.malecki@icloud.com']
10
+
11
+ spec.summary = 'Serialization system for Ruby with awsesome features!'
12
+ spec.description = 'Helps you to define serializers in a very simple and flexible way.'
13
+ spec.homepage = 'https://github.com/norbertmaleckii/simple-object-serialization-rb'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.7.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
19
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/CHANGELOG.md"
20
+ spec.metadata['rubygems_mfa_required'] = 'true'
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
26
+ end
27
+
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_development_dependency 'pry'
33
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_object_serialization
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Norbert Małecki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Helps you to define serializers in a very simple and flexible way.
28
+ email:
29
+ - norbert.malecki@icloud.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".github/dependabot.yml"
35
+ - ".github/workflows/lint.yml"
36
+ - ".github/workflows/specs.yml"
37
+ - ".gitignore"
38
+ - ".rspec"
39
+ - ".rubocop.yml"
40
+ - CHANGELOG.md
41
+ - CODE_OF_CONDUCT.md
42
+ - Gemfile
43
+ - Gemfile.lock
44
+ - LICENSE.txt
45
+ - README.md
46
+ - Rakefile
47
+ - bin/console
48
+ - bin/setup
49
+ - examples/user_serializer.rb
50
+ - lib/simple_object_serialization.rb
51
+ - lib/simple_object_serialization/attribute.rb
52
+ - lib/simple_object_serialization/attributes.rb
53
+ - lib/simple_object_serialization/collection_serializer.rb
54
+ - lib/simple_object_serialization/entity.rb
55
+ - lib/simple_object_serialization/object_serializer.rb
56
+ - lib/simple_object_serialization/version.rb
57
+ - simple_object_serialization.gemspec
58
+ homepage: https://github.com/norbertmaleckii/simple-object-serialization-rb
59
+ licenses:
60
+ - MIT
61
+ metadata:
62
+ homepage_uri: https://github.com/norbertmaleckii/simple-object-serialization-rb
63
+ source_code_uri: https://github.com/norbertmaleckii/simple-object-serialization-rb
64
+ changelog_uri: https://github.com/norbertmaleckii/simple-object-serialization-rb/CHANGELOG.md
65
+ rubygems_mfa_required: 'true'
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.7.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.3.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Serialization system for Ruby with awsesome features!
85
+ test_files: []