{"id":19692469,"url":"https://github.com/johansenja/lap","last_synced_at":"2025-02-27T09:54:15.826Z","repository":{"id":56880744,"uuid":"316855455","full_name":"johansenja/lap","owner":"johansenja","description":"Generate ruby code from rbs type definitions","archived":false,"fork":false,"pushed_at":"2020-12-04T18:05:29.000Z","size":69,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-10T13:47:38.032Z","etag":null,"topics":["rbs","ruby","ruby3","type-definitions","type-safety"],"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/johansenja.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-29T01:41:47.000Z","updated_at":"2020-12-04T18:05:32.000Z","dependencies_parsed_at":"2022-08-20T13:00:42.559Z","dependency_job_id":null,"html_url":"https://github.com/johansenja/lap","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johansenja%2Flap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johansenja%2Flap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johansenja%2Flap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johansenja%2Flap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/johansenja","download_url":"https://codeload.github.com/johansenja/lap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241001431,"owners_count":19891994,"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":["rbs","ruby","ruby3","type-definitions","type-safety"],"created_at":"2024-11-11T19:13:29.933Z","updated_at":"2025-02-27T09:54:15.802Z","avatar_url":"https://github.com/johansenja.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Lap\n\nDon't write your code, *and* rbs types! Write the rbs types first, then generate a template to\nfill in with business logic.\n\nA `lap`idary (or `lap`idist) is someone who cuts, polishes, or engraves precious stones.\n\n## Usage\n\n```sh\n$ lap sig/file.rbs # outputs to stdout\n$ lap sig/file.rbs \u003e lib/file.rb # output into a file\n```\n\n## Example\n\n0. Plan your project/module/class by writing some blueprints:\n```ruby\n# sig/lib/bank/account.rbs\nmodule Bank\n  class Account\n    attr_reader interest_rate: Float\n    attr_reader owner: Bank::Customer\n    attr_reader balance: Float\n\n    def initialize: (Bank::Customer owner, Float interest_rate, ?Float balance) -\u003e void\n\n    type date_or_dt = Date | DateTime\n\n    # can optionally specify when the transaction should take place\n    def deposit: (Float amount, ?when: date_or_dt) -\u003e Float\n\n    # can optionally specify when the transaction should take place\n    def withdraw: (Float amount, ?when: date_or_dt) -\u003e Float\n\n    # must filter results by specifying `to` and `from` params\n    def transactions: (from: date_or_dt, to: date_or_dt) -\u003e Array[Bank::Transaction]\n  end\nend\n```\n\n1. Generate your ruby templates\n\n```sh\n$ lap sig/lib/bank/account.rbs \u003e lib/bank/account.rb\n```\n\n(Generates lib/bank/account.rb)\n```ruby\nmodule Bank\n  class Account\n    attr_reader :interest_rate\n    attr_reader :owner\n    attr_reader :balance\n\n    def initialize(owner, interest_rate, balance = nil)\n      # returns void\n    end\n\n    # can optionally specify when the transaction should take place\n    def deposit(amount, when: nil)\n      # TODO: return Float\n    end\n\n    # can optionally specify when the transaction should take place\n    def withdraw(amount, when: nil)\n      # TODO: return Float\n    end\n\n    # must filter results by specifying `to` and `from` params\n    def transactions(to:, from:)\n      # TODO: return Array\n    end\n  end\nend\n```\n\n2. Fill in your business logic!\n\n```ruby\nmodule Bank\n  class Account\n    attr_reader :interest_rate\n    attr_reader :owner\n    attr_reader :balance\n\n    def initialize(owner, interest_rate, balance = 0)\n        @owner = owner\n        @interest_rate = interest_rate\n        @balance = balance\n    end\n\n    # can optionally specify when the transaction should take place\n    def deposit(amount, when: nil)\n      @balance += amount\n    end\n\n    # can optionally specify when the transaction should take place\n    def withdraw(amount, when: nil)\n      @balance -= amount\n    end\n\n    # must filter results by specifying `to` and `from` params\n    def transactions(to:, from:)\n      Transaction.where(\"created_at BETWEEN ? AND ?\", from, to)\n    end\n  end\nend\n```\n\nFrom here, you now have some ruby code which you can [type check](https://github.com/soutaro/steep)!\n\n### Experimental:\n\nThere is an experimental feature which allows you to specify some ruby logic *within* your rbs files\nfor your methods. You specify it between `@!begin` and `@!end`, eg.:\n\n```ruby\n# take some moeny out of the customers account\n# @!begin\n#   @balance -= amount\n# @!end\ndef withdraw(Float amount) -\u003e Float\n```\nThis would produce:\n\n```ruby\n# take some moeny out of the customers account\ndef withdraw(amount)\n  @balance -= amount\nend\n```\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'lap'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install lap\n\n## Configuration\n\nYou can specify preferences in a `.lap.yml` file in your project directory. Example\n\n```yml\nindent: 4 # default is 2\nfrozen_string_literals: false # add 'frozen_string_literal: true' to top of file; default is true\n# preferred line length in characters; default is 100. Note \"preferred\" - not always a guarantee\npreferred_line_length: 80\n```\n\n## Coverage\n\nCurrently not every feature of RBS is supported - yet! (contributions\nwelcome!)\n\nFeature|Coverage\n---|---\nclasses (includes nested)|✅\nmodules (includes nested)|✅\nclass methods|✅\ninstance methods|✅\nrequired positional arguments|✅\noptional positional arguments|✅\nrequired keyword arguments|✅\noptional keyword arguments|✅\nmethod comments|✅\naccess modifiers|✅\nattr_reader|✅\nattr_writer|✅\nattr_accessor|✅\ninclude|✅\nextend|✅\nmethods with blocks|✅\nmethod overloading|⚠️\nprocs|⚠️\nconstants|⚠️\ninterfaces|⚠️\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/johansenja/lap.\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohansenja%2Flap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohansenja%2Flap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohansenja%2Flap/lists"}