{"id":18489703,"url":"https://github.com/abicky/upcastable","last_synced_at":"2025-05-13T23:34:50.289Z","repository":{"id":59158713,"uuid":"49430548","full_name":"abicky/upcastable","owner":"abicky","description":"Provides the feature to emulate upcasting in Ruby","archived":false,"fork":false,"pushed_at":"2016-01-11T18:05:10.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-22T19:47:59.534Z","etag":null,"topics":[],"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/abicky.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}},"created_at":"2016-01-11T14:12:00.000Z","updated_at":"2016-01-11T14:13:27.000Z","dependencies_parsed_at":"2022-09-13T20:10:16.170Z","dependency_job_id":null,"html_url":"https://github.com/abicky/upcastable","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abicky%2Fupcastable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abicky%2Fupcastable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abicky%2Fupcastable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abicky%2Fupcastable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/abicky","download_url":"https://codeload.github.com/abicky/upcastable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254043218,"owners_count":22004912,"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":[],"created_at":"2024-11-06T12:57:37.258Z","updated_at":"2025-05-13T23:34:50.205Z","avatar_url":"https://github.com/abicky.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Upcastable [![Build Status](https://travis-ci.org/abicky/upcastable.svg?branch=master)](https://travis-ci.org/abicky/upcastable)\n\nUpcastable provides the feature to emulate upcasting in Ruby.\n\nDuck typing sometimes results in `NoMethodError` unexpectedly by calling methods some classes don't have even if the code pass a test using other classes which have the methods. We can avoid such situations by upcasting. All we have to do is implementing methods defined in the super class or module and we don't have to care about whether or not methods defined only in some subclasses are called.\n\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'upcastable'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install upcastable\n\n## Usage\n\nWe define `Animal` module and `Cat` class to explain the usage like below:\n\n```ruby\nmodule Animal\n  def talk; end\nend\n\nclass Cat\n  include Animal\n\n  def talk\n    'Meow!'\n  end\n\n  def run\n    'Running...'\n  end\nend\n```\n\nUpcastable defines `Object#upcast_to` and you can use `Animal` like a interface by upcasting `Cat` object to `Animal`.\n\n```ruby\ncat = Cat.new\nanimal = cat.upcast_to(Animal)\nanimal.class #=\u003e Cat\nanimal.talk  #=\u003e \"Meow!\"\nanimal.run   #=\u003e NoMethodError: `run' is not defined in Animal\n```\n\nInternally, the upcasted object is a `Upcastable::UpcastedObject` instance which delegates almost all methods to the original object. Therefore the value of `#object_id` is not changed.\n\n```ruby\ncat.object_id == animal.object_id #=\u003e true\n```\n\n\nYou can also downcast the upcasted object to the original class by calling `Object#downcast`.\n\n```ruby\nanimal.downcast.run #=\u003e \"Running...\"\n```\n\nThere are two additional methods,  `Object#upcasting` and `Object#upcasted?`. `Object#upcasting` returns the ancestor to which the object have been upcasted and `Object#upcasted?` returns `true` if the object is upcasted.\n\n```ruby\ncat.upcasted?    #=\u003e false\ncat.upcasting    #=\u003e nil\nanimal.upcasted? #=\u003e true\nanimal.upcasting #=\u003e Animal\n```\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 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/abicky/upcastable. 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\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabicky%2Fupcastable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabicky%2Fupcastable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabicky%2Fupcastable/lists"}