No commit activity in last 3 years
No release in over 3 years
Represent model attributes as Money instances
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 5.1.0
 Project Readme

EncapsulateAsMoney

License MIT Gem Version Gem Downloads Build Status

Want your model attribute to be a Money instance? EncapsulateAsMoney provides a simple way to get this done!

Installation

Add this line to your application's Gemfile:

gem 'encapsulate_as_money'

And then execute:

$ bundle

Usage

Rails

Add the encapsulate_as_money method to the Active Record base class.

ActiveRecord::Base.extend(EncapsulateAsMoney)

Now say you have the model:

class MyModel < ActiveRecord::Base
  encapsulate_as_money :amount
end

Which is based on the database table:

create_table "my_models" do |table|
  table.integer "amount"
end

Now we can create and save an instance like:

MyModel.create!(amount: 5.dollars)

This will create a row as such:

id amount
1 500

Note the value is represented as cents.

Once persisted we can find the value like:

MyModel.find_by_id(1).amount #=> 5.dollars

Note that it uses the default Money currency.

Contributing

  1. Fork it ( https://github.com/envato/encapsulate_as_money/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request