{"id":23249593,"url":"https://github.com/wizardone/dual","last_synced_at":"2025-04-06T01:24:23.727Z","repository":{"id":68987268,"uuid":"124197833","full_name":"wizardone/dual","owner":"wizardone","description":"Cloning ruby objects","archived":false,"fork":false,"pushed_at":"2018-07-17T23:32:38.000Z","size":54,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-12T07:29:53.846Z","etag":null,"topics":["clone","cloner","ruby","sequel"],"latest_commit_sha":null,"homepage":null,"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/wizardone.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2018-03-07T07:38:51.000Z","updated_at":"2023-03-09T04:20:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"6a493795-34f8-45b0-b37a-9700779af203","html_url":"https://github.com/wizardone/dual","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizardone%2Fdual","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizardone%2Fdual/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizardone%2Fdual/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wizardone%2Fdual/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wizardone","download_url":"https://codeload.github.com/wizardone/dual/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247422066,"owners_count":20936411,"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":["clone","cloner","ruby","sequel"],"created_at":"2024-12-19T08:20:07.959Z","updated_at":"2025-04-06T01:24:23.704Z","avatar_url":"https://github.com/wizardone.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dual\n\nDual is a gem that helps you clone object in Ruby. It is not tight to any particular\nweb framework and should work with ease with any PORO.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'dual'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install dual\n\n## Usage\n```ruby\nclass Order\n  attr_accessor :name, :type, :address\n\n  include Dual\n\n  dual do\n    # Exclude a certain attribute from the cloned object\n    exclude :type\n\n    # Include a new attribute in the cloned object\n    include :email, value: 'test@test.com'\n\n    # Add a guard clause for both include and exclude statements\n    include :date, value: Date.today, if: :delivery_incoming?\n    \n    # Perform any kind of finalizing actions on the created object\n    # The callable object(any object that responds to `call`) receives\n    # the dual object\n    finalize -\u003e (copied_order) { EventNotifier.call(copied_order) }\n  end\n\n  def delivery_incoming?\n    delivery.today?\n  end\nend\n\norder = Order.last\ndual_order = order.dual_copy\n```\n\nIf you are using `Sequel` you can also use dual to manipulate associations, supported ones are:\n`one_to_one`, `one_to_many`, `many_to_one`\n\n```ruby\n  class User \u003c Sequel::Model\n    one_to_many :orders\n    include Dual\n\n    dual do\n      add_association :orders\n    end\n  end\n```\nThis will create association objects that do not point to the original associations. A simple example would be:\n```ruby\nuser = User.create\norder = Order.create(address: 'some street')\nuser.add_order(order)\n\n# Duplicate the user, along with the orders association\ndupped_user = user.dual_copy\ndupped_order = dupped_user.orders.first\n# Change the order address\ndupped_order.address = 'new street'\n\norder.address == dupped_order.address\n=\u003e false\n```\n\nYou can also remove certain associations:\n```ruby\n  dual do\n    remove_association :orders\n  end\n```\n```ruby\nuser = User.create\norder = Order.create(address: 'some street')\nuser.add_order(order)\n\n# Duplicate the user, excluding the orders association\ndupped_user = user.dual_copy\ndupped_user.orders\n=\u003e []\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 tags, 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/wizardone/dual. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\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 Dual project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/dual/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwizardone%2Fdual","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwizardone%2Fdual","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwizardone%2Fdual/lists"}