Skip to content

Commit

Permalink
add railtie for money_column, initialize version, add more specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ssoroka committed May 25, 2011
1 parent 4a461f1 commit dcd17d1
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= money

Description goes here.
Manage money in shopify with a class that wont lose pennies during division!

== Contributing to money

Expand All @@ -14,6 +14,6 @@ Description goes here.

== Copyright

Copyright (c) 2011 Steven Soroka. See LICENSE.txt for
Copyright (c) 2011 Shopify. See LICENSE.txt for
further details.

4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Jeweler::Tasks.new do |gem|
gem.name = "money"
gem.homepage = "http://github.com/ssoroka/money"
gem.license = "MIT"
gem.summary = %Q{TODO: one-line summary of your gem}
gem.description = %Q{TODO: longer description of your gem}
gem.summary = %Q{Shopify's money gem}
gem.description = %Q{Manage money in shopify with a class that wont lose pennies during division!}
gem.email = "ssoroka78@gmail.com"
gem.authors = ["Steven Soroka"]
# dependencies defined in Gemfile
Expand Down
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
3 changes: 2 additions & 1 deletion lib/money.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/money/money'
require File.dirname(__FILE__) + '/money/money_parser'
require File.dirname(__FILE__) + '/money/accounting_money_parser'
require File.dirname(__FILE__) + '/money/core_extensions'
require File.dirname(__FILE__) + '/money/core_extensions'
require File.dirname(__FILE__) + '/money_column' if defined?(Rails)
2 changes: 1 addition & 1 deletion lib/money/accounting_money_parser.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class AccountingMoneyParser < MoneyParser
private
def extract_money(input)
# set () to mean negativity
# set () to mean negativity. ignore $
super(input.gsub(/\(\$?(.*?)\)/, '-\1'))
end
end
35 changes: 2 additions & 33 deletions lib/money_column.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,2 @@
module MoneyColumn
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods

def money_column(*columns)

[columns].flatten.each do |name|
define_method(name) do
value = read_attribute(name)
value.blank? ? nil : Money.new(read_attribute(name))
end

define_method("#{name}_before_type_cast") do
send(name) && sprintf("%.2f", send(name))
end

define_method("#{name}=") do |value|
if value.blank?
write_attribute(name, nil)
nil
else
money = value.to_money
write_attribute(name, money.value)
money
end
end
end
end
end
end
require File.dirname(__FILE__) + '/money_column/active_record_hooks'
require File.dirname(__FILE__) + '/money_column/railtie'
34 changes: 34 additions & 0 deletions lib/money_column/active_record_hooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module MoneyColumn
module ActiveRecordHooks
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def money_column(*columns)
[columns].flatten.each do |name|
define_method(name) do
value = read_attribute(name)
value.blank? ? nil : Money.new(read_attribute(name))

This comment has been minimized.

Copy link
@nickhoffman

nickhoffman Nov 5, 2014

@ssoroka, why return nil if the attribute's value is nil ? This means that one has to do type checking when playing with the return value from this method. E.g.

class Product < ActiveRecord::Base
  money_column :price
end

product = Product.new

product.price > 0
# NoMethodError: undefined method `>' for nil:NilClass

product.price.to_f > 0
=> false

Money.new(product.price) > 0
=> false
end

define_method("#{name}_before_type_cast") do
send(name) && sprintf("%.2f", send(name))
end

define_method("#{name}=") do |value|
if value.blank?
write_attribute(name, nil)
nil
else
money = value.to_money
write_attribute(name, money.value)
money
end
end
end
end
end
end
end

7 changes: 7 additions & 0 deletions lib/money_column/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module MoneyColumn
class Railtie < Rails::Railtie
ActiveSupport.on_load :active_record do
ActiveRecord::Base.send(:include, MoneyColumn::ActiveRecordHooks)
end
end
end
69 changes: 69 additions & 0 deletions money.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{money}
s.version = "0.1.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Steven Soroka"]
s.date = %q{2011-05-25}
s.description = %q{Manage money in shopify with a class that wont lose pennies during division!}
s.email = %q{ssoroka78@gmail.com}
s.extra_rdoc_files = [
"LICENSE.txt",
"README.rdoc"
]
s.files = [
".document",
".rspec",
"Gemfile",
"LICENSE.txt",
"README.rdoc",
"Rakefile",
"VERSION",
"lib/money.rb",
"lib/money/accounting_money_parser.rb",
"lib/money/core_extensions.rb",
"lib/money/money.rb",
"lib/money/money_parser.rb",
"lib/money_column.rb",
"lib/money_column/active_record_hooks.rb",
"lib/money_column/railtie.rb",
"spec/accounting_money_parser_spec.rb",
"spec/core_extensions_spec.rb",
"spec/money_parser_spec.rb",
"spec/money_spec.rb",
"spec/spec.opts",
"spec/spec_helper.rb"
]
s.homepage = %q{http://github.com/ssoroka/money}
s.licenses = ["MIT"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.7.2}
s.summary = %q{Shopify's money gem}

if s.respond_to? :specification_version then
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
s.add_development_dependency(%q<rcov>, [">= 0"])
else
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
s.add_dependency(%q<rcov>, [">= 0"])
end
else
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
s.add_dependency(%q<rcov>, [">= 0"])
end
end

10 changes: 9 additions & 1 deletion spec/money_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,17 @@
Money.parser = AccountingMoneyParser
end

it "should support parenthesis" do
it "should keep AccountingMoneyParser class on new money objects" do
Money.new.class.parser.should == AccountingMoneyParser
end

it "should support parenthesis from AccountingMoneyParser" do
Money.parse("($5.00)").should == Money.new(-5)
end

it "should support parenthesis from AccountingMoneyParser for .to_money" do
"($5.00)".to_money.should == Money.new(-5)
end

after(:each) do
Money.parser = nil # reset
Expand Down

0 comments on commit dcd17d1

Please sign in to comment.