cratchit 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/cratchit.gemspec +23 -0
- data/lib/cratchit/version.rb +3 -0
- data/lib/cratchit.rb +62 -0
- metadata +105 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/cratchit.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/cratchit/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "cratchit"
|
6
|
+
s.version = Cratchit::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = "Jonathan Martin"
|
9
|
+
s.email = "me@nybblr.com"
|
10
|
+
s.homepage = "http://rubygems.org/gems/cratchit"
|
11
|
+
s.summary = "RubyMoney is easily the best money class for Ruby out there, but implementation as an ActiveRecord datatype has traditionally been prone to bugs and core level hacks. This gem aims to bring the incredible foundation of RubyMoney into the slick Ruby on Rails framework by exposing money as an ActiveRecord column type."
|
12
|
+
s.description = "Ruby on Rails friendly money for ActiveRecord, built on RubyMoney."
|
13
|
+
|
14
|
+
s.required_rubygems_version = ">= 1.3.6"
|
15
|
+
s.rubyforge_project = "cratchit"
|
16
|
+
|
17
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
18
|
+
s.add_dependency "money", ">= 3.7.1"
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
22
|
+
s.require_path = 'lib'
|
23
|
+
end
|
data/lib/cratchit.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# == Info ===========================================================
|
2
|
+
# This gem was developed by Jonathan Martin for use in a company eCommerce project.
|
3
|
+
#
|
4
|
+
# RubyMoney is easily the best money class for Ruby out there, but implementation as an ActiveRecord datatype has traditionally been prone to bugs and core level hacks. This gem aims to bring the incredible foundation of RubyMoney into the slick Ruby on Rails framework by exposing money as an ActiveRecord column type.
|
5
|
+
#
|
6
|
+
# Be sure to check out www.nybblr.com for other Rails goodies, and if you're looking for more of my gems (when I get around to it!) go to nybblr.com/gems.
|
7
|
+
|
8
|
+
require 'money'
|
9
|
+
|
10
|
+
module Cratchit
|
11
|
+
def self.included(base)
|
12
|
+
base.extend ClassMethods
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
# == Usage ==========================================================
|
17
|
+
# From your ActiveRecord model, simply call the money method:
|
18
|
+
#
|
19
|
+
# class Product < ActiveRecord::Base
|
20
|
+
# # Example usages
|
21
|
+
# money :price
|
22
|
+
#
|
23
|
+
# money :price, :default => :zero
|
24
|
+
#
|
25
|
+
# money :price, :default => 10.99
|
26
|
+
#
|
27
|
+
# money :price, :default => Money.new(10, "EUR")
|
28
|
+
#
|
29
|
+
# money :price, :cents => "rate_in_cents", :include_prefix => false
|
30
|
+
#
|
31
|
+
# end
|
32
|
+
|
33
|
+
def money method, opts = {}
|
34
|
+
options = { :include_prefix => true, :default => nil }
|
35
|
+
options.merge! opts
|
36
|
+
|
37
|
+
options[:cents] ||= options[:include_prefix] ? method.to_s+'_cents' : 'cents'
|
38
|
+
options[:currency] ||= options[:include_prefix] ? method.to_s+'_currency' : 'currency'
|
39
|
+
|
40
|
+
default = case options[:default]
|
41
|
+
when :zero then Money.new(0)
|
42
|
+
when nil then nil
|
43
|
+
else options[:default].to_money
|
44
|
+
end
|
45
|
+
|
46
|
+
composed_of method.to_sym,
|
47
|
+
:class_name => "Money",
|
48
|
+
:allow_nil => true,
|
49
|
+
:mapping => [ [ options[:cents], 'cents' ], [ options[:currency], 'currency_as_string' ] ],
|
50
|
+
:converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
|
51
|
+
|
52
|
+
after_initialize do
|
53
|
+
send "#{method}=".to_sym, default if send(method.to_sym).nil?
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Include as a model instance method
|
60
|
+
class ActiveRecord::Base
|
61
|
+
include Cratchit
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cratchit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jonathan Martin
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-06-27 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: bundler
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 1.0.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: money
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 25
|
46
|
+
segments:
|
47
|
+
- 3
|
48
|
+
- 7
|
49
|
+
- 1
|
50
|
+
version: 3.7.1
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
description: Ruby on Rails friendly money for ActiveRecord, built on RubyMoney.
|
54
|
+
email: me@nybblr.com
|
55
|
+
executables: []
|
56
|
+
|
57
|
+
extensions: []
|
58
|
+
|
59
|
+
extra_rdoc_files: []
|
60
|
+
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- Rakefile
|
65
|
+
- cratchit.gemspec
|
66
|
+
- lib/cratchit.rb
|
67
|
+
- lib/cratchit/version.rb
|
68
|
+
has_rdoc: true
|
69
|
+
homepage: http://rubygems.org/gems/cratchit
|
70
|
+
licenses: []
|
71
|
+
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 3
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 23
|
92
|
+
segments:
|
93
|
+
- 1
|
94
|
+
- 3
|
95
|
+
- 6
|
96
|
+
version: 1.3.6
|
97
|
+
requirements: []
|
98
|
+
|
99
|
+
rubyforge_project: cratchit
|
100
|
+
rubygems_version: 1.3.7
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: RubyMoney is easily the best money class for Ruby out there, but implementation as an ActiveRecord datatype has traditionally been prone to bugs and core level hacks. This gem aims to bring the incredible foundation of RubyMoney into the slick Ruby on Rails framework by exposing money as an ActiveRecord column type.
|
104
|
+
test_files: []
|
105
|
+
|