Permalink
Browse files

initial commit

  • Loading branch information...
1 parent 3325caf commit f9c3df47625ee3e87bbd3eb67bc2d83cb04d6ffa @kristianmandrup committed Jun 16, 2012
View
@@ -28,22 +28,22 @@ pkg
#
# For MacOS:
#
-#.DS_Store
+.DS_Store
# For TextMate
-#*.tmproj
-#tmtags
+*.tmproj
+tmtags
# For emacs:
-#*~
-#\#*
-#.\#*
+*~
+\#*
+.\#*
# For vim:
-#*.swp
+*.swp
# For redcar:
-#.redcar
+.redcar
# For rubinius:
-#*.rbc
+*.rbc
View
29 Gemfile
@@ -1,14 +1,21 @@
-source "http://rubygems.org"
-# Add dependencies required to use your gem here.
-# Example:
-# gem "activesupport", ">= 2.3.5"
+source :rubygems
+
+gem 'money'
+
+gem 'mongoid', "~> 3.0.0.rc"
+gem 'moped'
+
+# gem 'mongoid', "~> 2.4"
+# gem 'bson'
-# Add dependencies to develop your gem here.
-# Include everything needed to run rake, tests, features, etc.
group :development do
- gem "rspec", "~> 2.8.0"
- gem "rdoc", "~> 3.12"
- gem "bundler", "~> 1.0.0"
- gem "jeweler", "~> 1.8.3"
- gem "rcov", ">= 0"
+ gem "rspec", "~> 2.10"
+ gem "rdoc", ">= 3.12"
+ gem "bundler", "~> 1.1.0"
+ gem "jeweler", ">= 1.8.3"
+ gem "simplecov",">= 0.5"
end
+
+group :test do
+ # gem 'bson_ext'
+end
View
@@ -0,0 +1,59 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activemodel (3.2.6)
+ activesupport (= 3.2.6)
+ builder (~> 3.0.0)
+ activesupport (3.2.6)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ builder (3.0.0)
+ diff-lcs (1.1.3)
+ git (1.2.5)
+ i18n (0.6.0)
+ jeweler (1.8.3)
+ bundler (~> 1.0)
+ git (>= 1.2.5)
+ rake
+ rdoc
+ json (1.7.3)
+ money (5.0.0)
+ i18n (~> 0.4)
+ json
+ mongoid (3.0.0.rc)
+ activemodel (~> 3.1)
+ moped (~> 1.0.0.rc)
+ origin (~> 1.0.0.rc)
+ tzinfo (~> 0.3.22)
+ moped (1.0.0.rc)
+ multi_json (1.3.6)
+ origin (1.0.1)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ rspec (2.10.0)
+ rspec-core (~> 2.10.0)
+ rspec-expectations (~> 2.10.0)
+ rspec-mocks (~> 2.10.0)
+ rspec-core (2.10.1)
+ rspec-expectations (2.10.0)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.10.1)
+ simplecov (0.6.4)
+ multi_json (~> 1.0)
+ simplecov-html (~> 0.5.3)
+ simplecov-html (0.5.3)
+ tzinfo (0.3.33)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bundler (~> 1.1.0)
+ jeweler (>= 1.8.3)
+ money
+ mongoid (~> 3.0.0.rc)
+ moped
+ rdoc (>= 3.12)
+ rspec (~> 2.10)
+ simplecov (>= 0.5)
View
@@ -1,8 +1,28 @@
-= money-mongoid
+# Money for mongoid
-Description goes here.
+Use the [money](https://github.com/RubyMoney/money) gem with Mongoid 2.x and 3.x.
-== Contributing to money-mongoid
+## Install
+
+in Gemfile
+
+```ruby
+gem 'money-mongoid'
+```
+
+Bundle via Terminal:
+
+`$ bundle`
+
+## Usage
+
+```ruby
+require 'money-mongoid'
+``
+
+See specs for usage examples, fx `money/mongoid/3x/money_spec.rb`
+
+## Contributing to money-mongoid
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
View
@@ -17,8 +17,8 @@ Jeweler::Tasks.new do |gem|
gem.name = "money-mongoid"
gem.homepage = "http://github.com/kristianmandrup/money-mongoid"
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{Mongoid support and integration for the money gem}
+ gem.description = %Q{Makes it easy to use money with mongoid}
gem.email = "kmandrup@gmail.com"
gem.authors = ["Kristian Mandrup"]
# dependencies defined in Gemfile
View
@@ -0,0 +1 @@
+require 'money/mongoid'
View
@@ -0,0 +1,16 @@
+require 'money/mongoid/core_ext'
+require 'money/orm/generic'
+
+if Mongoid::VERSION > '3'
+ require "money/mongoid/3x/money"
+else
+ require "money/mongoid/2x/money"
+end
+
+module Mongoid
+ module Money
+ class << self
+ attr_accessor :default_polymorphic_money
+ end
+ end
+end
@@ -0,0 +1,22 @@
+# extend Mongoid::Fields in Mongoid document (model) that wants to use Money
+# this makes Money accessible as a type
+
+module Mongoid
+ module Fields
+ class Money
+ include Mongoid::Fields::Serializable
+
+ def self.instantiate(name, options = {})
+ super
+ end
+
+ def deserialize(value)
+ ::Money.new value[:cents], value[:currency_iso]
+ end
+
+ def serialize(value)
+ {:cents => value.cents, :currency_iso => value.currency.iso_code}
+ end
+ end
+ end
+end
@@ -0,0 +1,38 @@
+module Mongoize
+ extend ActiveSupport::Concern
+ # See http://mongoid.org/en/mongoid/docs/upgrading.html
+
+ def mongoize
+ {'cents' => cents, 'currency_iso' => currency.iso_code}
+ end
+
+ module ClassMethods
+ def demongoize(value)
+ ::Money.new get_cents(value), get_currency(value)
+ end
+
+ def evolve(object)
+ object.__evolve_to_money__.mongoize
+ end
+
+ private
+
+ def get_cents value
+ value[:cents] || value['cents']
+ end
+
+ def get_currency value
+ value[:currency_iso] || value['currency_iso']
+ end
+ end
+end
+
+
+
+class Money
+ include Mongoize
+
+ def __evolve_to_money__
+ self
+ end
+end
@@ -0,0 +1,2 @@
+require 'money/mongoid/core_ext/string'
+require 'money/mongoid/core_ext/array'
@@ -0,0 +1,5 @@
+class Array
+ def exchange_to! currency
+ self.map!{|m| m.exchange_to!(currency) }
+ end
+end
@@ -0,0 +1,6 @@
+# encoding: utf-8
+class String
+ def __evolve_to_money__
+ Money.parse(self)
+ end
+end
@@ -0,0 +1,29 @@
+module Mongoid
+ module Money
+ module Macros
+ def self.included(base)
+ base.send :include, ClassMethods
+ end
+
+ module ClassMethods
+ def monetize
+ self.send :include, "Mongoid::Monetize".constantize
+ end
+
+ def monetizable options = {}
+ self.send :include, "Mongoid::Monetizable".constantize
+ embedded_in options[:as], :polymorphic => true if options[:as]
+ end
+ end
+ end
+ end
+end
+
+module Mongoid
+ module Document
+ module ClassMethods
+ include Money::Mongoid::Macros
+ end
+ end
+end
+
@@ -0,0 +1,17 @@
+# Thanks to: https://gist.github.com/840500
+
+module Mongoid
+ module Monetizable
+ include Money::Orm::Generic
+
+ def self.included(base)
+ base.class_eval do
+ field :cents, :type => Integer, :default => 0
+ field :currency_iso, :type => String, :default => ::Money.default_currency.iso_code
+
+ validates_numericality_of :cents
+ end
+ base.extend Money::Orm::Generic::ClassMethods
+ end
+ end
+end
@@ -0,0 +1,35 @@
+module Mongoid
+ module Monetize
+ def self.included(base)
+ base.extend ClassMethods
+ base.extend Money::Orm::Generic::ClassMethods
+ end
+
+ module ClassMethods
+ def monetize_one name, options = {:as => :priced}
+ as_poly = options.delete(:as) || default_polymorphic_money
+
+ unless as_poly
+ raise ArgumentError, "You must set an :as option indicating the polymorphic Monetizable model. See money-rails gem."
+ end
+
+ self.embeds_one name.to_sym, options.merge(:as => as_poly)
+ end
+ alias_method :monetize, :monetize_one
+
+ def monetize_many name, options = {:as => :priced}
+ as_poly = options.delete(:as) || default_polymorphic_money
+ unless as_poly
+ raise ArgumentError, "You must set an :as option indicating the polymorphic Monetizable model. See money-rails gem."
+ end
+ self.embeds_many name, options.merge(:as => as_poly)
+ end
+
+ protected
+
+ def default_polymorphic_money
+ Mongoid::Money.default_polymorphic_money
+ end
+ end # module
+ end
+end
View
@@ -0,0 +1,24 @@
+class Money
+ module Orm
+ module Generic
+ # Virtual price / currency attributes
+ module ClassMethods
+ def monetize_for *names
+ options = names.last.kind_of?(Hash) ? names.delete(names.last) : {:as => :priced}
+ names.each {|name| monetize name, options }
+ end
+ end
+
+ def price
+ ::Money.new(self.price_pence, currency)
+ end
+ def price=(price)
+ self.price_pence = price.cents
+ end
+
+ def currency
+ ::Money::Currency.new(currency_iso)
+ end
+ end
+ end
+end
@@ -1,7 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-
-describe "MoneyMongoid" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
- end
-end
Oops, something went wrong.

0 comments on commit f9c3df4

Please sign in to comment.