-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add railtie for money_column, initialize version, add more specs
- Loading branch information
Showing
10 changed files
with
129 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong. |
||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ssoroka, why return
nil
if the attribute's value isnil
? This means that one has to do type checking when playing with the return value from this method. E.g.