Project

has_price

0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
A convenient DSL for defining complex price reader/serializer in a class and organizing a price breakdown. Price can be declared with items and groups which depend on other attributes. Price is a very simple subclass of Hash. This provides for easy serialization and flexibility in case of implementation changes. This way you can conveniently store the whole price breakdown in your serialized receipts. It also provides magic methods for convenient access, but can be fully treated as a regular Hash with some sprinkles on top.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme

has_price

Let'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.

Install

gem install has_price

In Rails you will automatically get has_price in models. Everywhere else you would need to include it yourself.

include HasPrice::HasPrice

Organize

Say 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.

class Product < ActiveRecord::Base
  has_many :discounts
end

has_price provides a small DSL with two methods, item and group, to help you organize this.

class Product < ActiveRecord::Base
  has_many :discounts

  has_price do
    item base_price, "base"

    group "taxes" do
      item federal_tax, "federal"
      item state_tax, "state"
    end

    group "discounts" do
      discounts.each do |discount|
        item discount.amount, discount.title
      end
    end
  end

end

This builds an instance method price on products which returns a Hash-like structure with some extra features. Now you can use it as so.

# Hypothetically, the actual numbers are stored in the aforementioned attributes on your model.
  
product = Product.find(1)
product.price               # => Price hash-like object
product.price.total         # => 500
product.price.base          # => 400
product.price.taxes         # => Price hash-like object
product.price.taxes.federal # => 50
product.price.taxes.total   # => 100
product.discounts.total     # => -50

Serialize

Price object actually inherits from a plain old Hash. Therefore, serialization should work out of the box.

class Receipt < ActiveRecord::Base
  serialize :price, Hash
end

Now passing the whole price breakdown into receipt is as simple as receipt.price = product.price.