Git Product home page Git Product logo

has_price's Introduction

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.

has_price's People

Contributors

maxim avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.