{"id":20154928,"url":"https://github.com/aliou/as_range","last_synced_at":"2026-06-10T10:31:05.189Z","repository":{"id":62553612,"uuid":"263251558","full_name":"aliou/as_range","owner":"aliou","description":"Remove the boilerplate of working with objects representing ranges.","archived":false,"fork":false,"pushed_at":"2020-06-07T13:39:17.000Z","size":15,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-13T12:42:12.232Z","etag":null,"topics":["ruby","rubygem"],"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/aliou.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-05-12T06:23:46.000Z","updated_at":"2020-07-22T11:16:35.000Z","dependencies_parsed_at":"2022-11-03T04:45:31.962Z","dependency_job_id":null,"html_url":"https://github.com/aliou/as_range","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/aliou%2Fas_range","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aliou%2Fas_range/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aliou%2Fas_range/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aliou%2Fas_range/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aliou","download_url":"https://codeload.github.com/aliou/as_range/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241593831,"owners_count":19987650,"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":["ruby","rubygem"],"created_at":"2024-11-13T23:29:04.661Z","updated_at":"2025-03-03T01:27:15.241Z","avatar_url":"https://github.com/aliou.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# as\\_range\n\nRemoves the boilerplate of working with objects representing ranges.\n\nInstead of\n\n```ruby\nclass Period\n  def initialize(period_start, period_end)\n    @period_start = period_start\n    @period_end = period_end\n  end\n\n  def as_range\n    @period_start..@period_end\n  end\nend\n```\n\nWith `as_range`, you can do:\n\n```ruby\nclass Period\n  as_range\n\n  def initialize(start_date, end_date)\n    @start_date = start_date\n    @end_date = end_date\n  end\nend\n```\n\n## Usage\n\n### Using with custom attributes\n\nBy default, `as_range` will generate a range using `start_date` and `end_date` as the bound of the range. You can customize this behavior by passing a symbol to the `:start` and `:end` options:\n\n```ruby\nclass Period\n  attr_reader :period_start\n  as_range start: :period_start, end: :compute_period_end\n\n  def initialize(period_start)\n    @period_start = period_start\n  end\n\n  def compute_period_end\n    period_start + 1.year\n  end\nend\n```\n\nThe `:start` and `:end` also accept a `proc`:\n```ruby\nclass Period\n  attr_reader :period_start\n  as_range start: :period_start, end: -\u003e { period_start + 1.year }\n\n  def initialize(period_start)\n    @period_start = period_start\n  end\nend\n```\n\n### Excluding the end of the range\n\nBy default, the generated range includes the end of the range. To exclude the end, set the `include_end` option to `false`:\n\n```ruby\nclass Period\n  attr_reader :period_start\n  as_range include_end: false\n\n  def start_date\n    Date.new(2020, 1, 1)\n  end\n\n  def end_date\n    Date.new(2021, 1, 1)\n  end\nend\n\u003e p = Period.new\n\u003e p.as_range\n# =\u003e #\u003cDate: 2020-01-01\u003e...#\u003cDate: 2021-01-01\u003e # Note the extra dot.\n\n```\n\n### Customizing the method name\n\nBy default, it names the generated method `as_range`. To set a custom method name, use the `method_name` option:\n```ruby\nclass Period\n  as_range method_name: :to_range\n\n  def start_date\n    Date.new(2020, 1, 1)\n  end\n\n  def end_date\n    Date.new(2021, 1, 1)\n  end\nend\n\n\u003e p = Period.new\n\u003e p.to_range\n# =\u003e #\u003cDate: 2020-01-01\u003e..#\u003cDate: 2021-01-01\u003e # Note the extra dot.\n```\n\n## Explicit mode\n\nBy default, `as_range` will add methods to every class and module.\n\nThis is not ideal if you're using `as_range` in a library: those who depend on your library will get those methods as well.\n\nIt's also not obvious where the methods come from. You can be more explicit about it, and restrict where the methods are added, like this:\n\n``` ruby\nrequire \"as_range/explicit\"\n\nclass MyPeriod\n  extend AsRange.explicit\n\n  as_range method_name: :now_this_class_can_use_as_range\nend\n```\n\nCrucially, you need to `require \"as_range/explicit\"` *instead of* `require \"as_range\"`. Some frameworks, like Ruby on Rails, may automatically require everything in your `Gemfile`. You can avoid that with `gem \"as_range\", require: \"as_range/explicit\"`.\n\nIn explicit mode, you need to call `extend AsRange.explicit` *in every class or module* that wants the `as_range` methods.\n\n## Installation\n\nAdd this line to your application's `Gemfile`:\n\n```ruby\ngem 'as_range'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install as_range\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` 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## Acknowledgements\n`as_range`'s explicit mode is directly inspired by [`attr_extras`](https://github.com/barsoom/attr_extras/).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at \u003chttps://github.com/aliou/as_range\u003e\n\n## License\n\n[MIT](LICENSE.txt)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faliou%2Fas_range","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faliou%2Fas_range","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faliou%2Fas_range/lists"}