{"id":23445965,"url":"https://github.com/norbertmaleckii/simple-object-serialization-rb","last_synced_at":"2025-04-13T15:11:46.640Z","repository":{"id":38299874,"uuid":"422284069","full_name":"norbertmaleckii/simple-object-serialization-rb","owner":"norbertmaleckii","description":"Helps you to define serializers in a very simple and flexible way.","archived":false,"fork":false,"pushed_at":"2023-04-19T17:58:35.000Z","size":72,"stargazers_count":2,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-01T16:56:56.482Z","etag":null,"topics":["rails","rails-serialization","ruby","ruby-serialization"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/norbertmaleckii.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-28T16:48:30.000Z","updated_at":"2022-01-20T18:05:06.000Z","dependencies_parsed_at":"2024-08-05T18:35:27.628Z","dependency_job_id":null,"html_url":"https://github.com/norbertmaleckii/simple-object-serialization-rb","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/norbertmaleckii%2Fsimple-object-serialization-rb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/norbertmaleckii%2Fsimple-object-serialization-rb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/norbertmaleckii%2Fsimple-object-serialization-rb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/norbertmaleckii%2Fsimple-object-serialization-rb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/norbertmaleckii","download_url":"https://codeload.github.com/norbertmaleckii/simple-object-serialization-rb/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248732489,"owners_count":21152852,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rails","rails-serialization","ruby","ruby-serialization"],"created_at":"2024-12-23T20:28:44.309Z","updated_at":"2025-04-13T15:11:46.614Z","avatar_url":"https://github.com/norbertmaleckii.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SimpleObjectSerialization\n\nSimple object serialization system for Ruby with awsesome features!\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'simple_object_serialization'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install simple_object_serialization\n\n## Usage\n\nYou can simply define call method which returns hash.\n\n```ruby\nclass UserSerializer \u003c SimpleObjectSerialization::Entity\n  def call\n    hash = {}\n    hash[:index] = options[:index] if options[:index]\n    hash[:id] = user.id\n    hash[:email] = user.email\n    hash[:login] = login unless email.nil?\n    hash[:created_at] = user.created_at\n    hash[:updated_at] = user.created_at unless email.nil?\n    hash\n  end\n\n  private\n\n  def email\n    user.email\n  end\n\n  def login\n    user.email.split('@').first\n  end\n\n  def user\n    object\n  end\nend\n```\n\nHowever, you can also use gem DSL to implement serialization.\n\n```ruby\nclass UserSerializer \u003c SimpleObjectSerialization::Entity\n  object_alias :user\n\n  define_attribute :index, if: proc { options[:index] } do\n    options[:index]\n  end\n\n  define_attribute :id\n  define_attribute :email\n  define_attribute :login, if: proc { !email.nil? } do\n    login\n  end\n\n  define_attribute :created_at\n  define_attribute :updated_at, if: proc { !email.nil? } do\n    user.created_at\n  end\n\n  private\n\n  def email\n    user.email\n  end\n\n  def login\n    user.email.split('@').first\n  end\nend\n```\n\nFinally, the result will be the same for both implementations.\n\n```ruby\nUser = Struct.new(:id, :email, :created_at)\n\nuser = User.new(1, 'user@example.com', DateTime.new(2020, 1, 1))\nusers = Kaminari.paginate_array(Array.new(4) { user.dup }).page(2).per(2)\n\nUserSerializer.call(user, meta: { current_time: Time.now })\n#=\u003e {:id=\u003e1, :email=\u003e\"user@example.com\", :login=\u003e\"user\", :created_at=\u003eWed, 01 Jan 2020 00:00:00 +0000, :updated_at=\u003eWed, 01 Jan 2020 00:00:00 +0000}\n\nUserSerializer.serialize(user, meta: { current_time: Time.now })\n#=\u003e \"{\\\"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\\\"}}\"\n\nUserSerializer.serialize_collection(users, meta: { current_time: Time.now })\n#=\u003e \"{\\\"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}}\"\n```\n\n## Development\n\nAfter 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.\n\nTo 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).\n\n## Contributing\n\nBug 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).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone 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).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnorbertmaleckii%2Fsimple-object-serialization-rb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnorbertmaleckii%2Fsimple-object-serialization-rb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnorbertmaleckii%2Fsimple-object-serialization-rb/lists"}