{"id":20548169,"url":"https://github.com/givelively/consolidatable","last_synced_at":"2025-06-20T01:04:52.069Z","repository":{"id":46903396,"uuid":"515726371","full_name":"givelively/consolidatable","owner":"givelively","description":null,"archived":false,"fork":false,"pushed_at":"2025-04-18T16:38:57.000Z","size":188,"stargazers_count":2,"open_issues_count":8,"forks_count":1,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-06-20T01:04:06.903Z","etag":null,"topics":["ruby"],"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/givelively.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"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,"zenodo":null}},"created_at":"2022-07-19T20:06:01.000Z","updated_at":"2025-03-11T02:18:12.000Z","dependencies_parsed_at":"2024-11-16T02:12:34.885Z","dependency_job_id":"eecc20e1-bf09-410f-a6c6-c776a814dd04","html_url":"https://github.com/givelively/consolidatable","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/givelively/consolidatable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givelively%2Fconsolidatable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givelively%2Fconsolidatable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givelively%2Fconsolidatable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givelively%2Fconsolidatable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/givelively","download_url":"https://codeload.github.com/givelively/consolidatable/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/givelively%2Fconsolidatable/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260857364,"owners_count":23073435,"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"],"created_at":"2024-11-16T02:12:26.276Z","updated_at":"2025-06-20T01:04:47.044Z","avatar_url":"https://github.com/givelively.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Consolidatable\n\nConsolidatable provides tooling to precalculate values and cache them in the database for a specified amount of time.\n\n## Installation\n\nAdd the following line to your Gemfile:\n\n```ruby\ngem 'consolidatable', git: 'https://github.com/givelively/consolidatable.git'\n```\n\nDownload and install the gem:\n```sh\nbundle install\n```\n\nGenerate the migration and initializer required by Consolidatable:\n```sh\nbundle exec rails g consolidatable:install\n```\n\nMigrate the database:\n```sh\nbundle exec rails db:migrate\n```\n\n## Interface\n\n### Basic use case\n```ruby\nclass Nonprofit\n  include Consolidatable\n  consolidates :very_expensive_value\n\n  def very_expensive_value\n    sleep(3)\n    rand(10)\n  end\nend\n```\n\nThe simplest use case above will provide a method named `consolidated_very_expensive_value`.\n\nBy default, `consolidates` assumes that you want to store a `Integer` value.\n\nThe first time you call `consolidated_very_expensive_value` it will call `very_expensive_value` and cache the value.\n\nThe second time you call `consolidated_very_expensive_value` it returns the cached value.\n\n### type\nIf you don't want to consolidate an Integer value, you have to specify the type like this:\n```ruby\nclass Nonprofit\n  [...]\n  consolidates :very_expensive_value, type: :float\n  [...]\nend\n```\nSupported types are\n- `:float`\n- `:integer` (default, stored as bigint)\n- `:boolean`\n- `:string`\n- `:datetime`\n\nOr you can change the default type in `config/initializers/consolidatable.rb`.\n### not_older_than\n\nBy default, cached values will be considered fresh for the time defined in `config/initializers/consolidatable.rb`, by default set to `1.hour`.\n\nTo overwrite that value, you can do this by specifying `not_older_than`:\n```ruby\nconsolidates :very_expensive_value, not_older_than: 1.day\n```\n\n### as\n\nConsolidatable provides a default name for the method returning the consolidated value by prefixing the original method name with `consolidated_`.\nTo change the name of that method use the `as` attribute (defining the method `cheap_value` below):\n```ruby\nconsolidates :very_expensive_value, as: :cheap_value\n```\n\n## Scope\nBy default, Consolidatable provides a scope named after the new value. Below example provides the scope `with_total_amount_raised`\n```ruby\n  consolidates :calculate_total_amount_raised, as: :total_amount_raised\n```\nThe provided scope will add an aditional artificial column named after the new value:\n\n```ruby\nNonprofit\n  .with_total_amount_raised\n  .order(total_amount_raised: :desc)\n  .limit(5)\n```\n\nWill provide you the five nonprofits with the highest (cached) values for `total_amount_raised`.\n\nIf you need to `.count` the query, you must use `.count(:all)`.\n\n## Calculating the new value\nUsing the default `InlineFetcher`, Consolidatable computes the requested value if the cache is stale or doesn't exist yet. In those cases **_Consolidatable will attempt to write to the database_**, even though you are calling a getter.\n\nThere is a fetcher called `BackgroundFetcher` that provides the cached value or nil and will not attempt to write to the database. Stale or nonexistent values will be refreshed in the background, by triggering an ActiveJob.\n\nTo change the fetcher, use\n```ruby\nconsolidates :very_expensive_value, fetcher: BackgroundFetcher\n```\nor change the default fetcher in `config/initializers/consolidatable.rb`.\n\n## Contributing\n\n### Setup\n\n1. `bundle install`\n1. `RAILS_ENV=test bundle exec rake db:create db:schema:load`\n\n### Testing\n\n`bundle exec rspec`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgivelively%2Fconsolidatable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgivelively%2Fconsolidatable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgivelively%2Fconsolidatable/lists"}