{"id":13850652,"url":"https://github.com/maxim/has_price","last_synced_at":"2025-07-12T22:31:28.216Z","repository":{"id":742250,"uuid":"393274","full_name":"maxim/has_price","owner":"maxim","description":"Provides a convenient DSL for organizing a price breakdown in a class.","archived":true,"fork":false,"pushed_at":"2012-05-25T09:37:53.000Z","size":467,"stargazers_count":44,"open_issues_count":0,"forks_count":2,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-29T05:38:33.158Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://github.com/maxim/has_price","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"getsentry/sentry-webhooks","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2009-12-02T20:21:10.000Z","updated_at":"2023-01-08T20:47:13.000Z","dependencies_parsed_at":"2022-07-05T13:14:21.277Z","dependency_job_id":null,"html_url":"https://github.com/maxim/has_price","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fhas_price","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fhas_price/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fhas_price/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fhas_price/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxim","download_url":"https://codeload.github.com/maxim/has_price/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225839622,"owners_count":17532305,"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-08-04T20:01:21.617Z","updated_at":"2024-11-22T03:31:44.648Z","avatar_url":"https://github.com/maxim.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"has_price\n=========\n\nLet's just say, it organizes your price breakdowns and allows for easy retrieval of price subgroups and subtotals, as well as simple serialization for your receipts.\n\nInstall\n-------\n\n    gem install has_price\n\nIn Rails you will automatically get `has_price` in models. \nEverywhere else you would need to include it yourself.\n\n```ruby\ninclude HasPrice::HasPrice\n```\n\nOrganize\n--------\n\nSay you have a Product class with some attributes which price depends on. For this example assume that `base_price`, `federal_tax`, and `state_tax` are integer attributes on a `Product` model.\n\n```ruby\nclass Product \u003c ActiveRecord::Base\n  has_many :discounts\nend\n```\n\n`has_price` provides a small DSL with two methods, `item` and `group`, to help you organize this.\n\n```ruby\nclass Product \u003c ActiveRecord::Base\n  has_many :discounts\n\n  has_price do\n    item base_price, \"base\"\n\n    group \"taxes\" do\n      item federal_tax, \"federal\"\n      item state_tax, \"state\"\n    end\n\n    group \"discounts\" do\n      discounts.each do |discount|\n        item discount.amount, discount.title\n      end\n    end\n  end\n\nend\n```\n\nThis builds an instance method `price` on products which returns a Hash-like structure with some extra features. Now you can use it as so.\n\n```ruby\n# Hypothetically, the actual numbers are stored in the aforementioned attributes on your model.\n  \nproduct = Product.find(1)\nproduct.price               # =\u003e Price hash-like object\nproduct.price.total         # =\u003e 500\nproduct.price.base          # =\u003e 400\nproduct.price.taxes         # =\u003e Price hash-like object\nproduct.price.taxes.federal # =\u003e 50\nproduct.price.taxes.total   # =\u003e 100\nproduct.discounts.total     # =\u003e -50\n```\n\nSerialize\n---------\n\nPrice object actually inherits from a plain old Hash. Therefore, serialization should work out of the box.\n\n```ruby\nclass Receipt \u003c ActiveRecord::Base\n  serialize :price, Hash\nend\n```\n\nNow passing the whole price breakdown into receipt is as simple as `receipt.price = product.price`.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxim%2Fhas_price","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxim%2Fhas_price","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxim%2Fhas_price/lists"}