https://github.com/envato/encapsulate_as_money
Surprise me ;)
https://github.com/envato/encapsulate_as_money
Last synced: about 1 year ago
JSON representation
Surprise me ;)
- Host: GitHub
- URL: https://github.com/envato/encapsulate_as_money
- Owner: envato
- License: mit
- Created: 2014-04-14T05:39:04.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2021-01-24T23:45:57.000Z (over 5 years ago)
- Last Synced: 2024-04-24T10:18:06.845Z (about 2 years ago)
- Language: Ruby
- Size: 41 KB
- Stars: 5
- Watchers: 73
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# EncapsulateAsMoney
[](https://github.com/envato/encapsulate_as_money/blob/master/LICENSE.txt)
[](https://rubygems.org/gems/encapsulate_as_money)
[](https://rubygems.org/gems/encapsulate_as_money)
[](https://github.com/envato/encapsulate_as_money/actions?query=branch%3Amaster+workflow%3Atests)
Want your model attribute to be a [Money](https://github.com/RubyMoney/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.
```ruby
ActiveRecord::Base.extend(EncapsulateAsMoney)
```
Now say you have the model:
```ruby
class MyModel < ActiveRecord::Base
encapsulate_as_money :amount
end
```
Which is based on the database table:
```ruby
create_table "my_models" do |table|
table.integer "amount"
end
```
Now we can create and save an instance like:
```ruby
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:
```ruby
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