dm-money 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/dm-money.rb +13 -2
  3. data/test/test_money.rb +7 -1
  4. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -41,14 +41,25 @@ module DataMapper
41
41
  def money(name, options = {})
42
42
  raise ArgumentError.new('You need to pass at least one argument') if name.empty?
43
43
 
44
+ if default = options.delete(:default)
45
+ raise TypeError.new("Expected BigMoney +default+ but got #{options[:default].class}") \
46
+ unless default.is_a?(BigMoney)
47
+ end
48
+
49
+ # TODO: Golf.
44
50
  property = Property.new(self, name, BigDecimal, options)
45
51
  name = property.name.to_s
46
52
  instance_variable_name = property.instance_variable_name
47
53
  name_amount = "#{name}_amount"
48
54
  name_currency = "#{name}_currency"
49
55
 
50
- self.property name_amount.to_sym, BigDecimal, property.options.except(:accessor, :reader, :writer).merge(:accessor => :private)
51
- self.property name_currency.to_sym, String, :accessor => :private, :required => property.required, :length => 3
56
+ options_amount = options.only(:required, :length, :offset).merge(:accessor => :private)
57
+ options_amount.merge!(:default => default.amount) if default
58
+ self.property name_amount.to_sym, BigDecimal, options_amount
59
+
60
+ options_currency = options.only(:required).merge(:accessor => :private, :length => 3)
61
+ options_currency.merge!(:default => default.currency) if default
62
+ self.property name_currency.to_sym, String, options_currency
52
63
 
53
64
  # TODO: Access amount, currency via readers or properties?
54
65
  # TODO: Validations or error message attempting to set with something other than BigMoney?
@@ -8,7 +8,7 @@ class TestMoney < Test::Unit::TestCase
8
8
  include DataMapper::Resource
9
9
  property :id, Serial
10
10
  property :type, String
11
- money :price
11
+ money :price, :default => BigMoney.new(0, :aud)
12
12
  end # Cake
13
13
  end
14
14
 
@@ -27,5 +27,11 @@ class TestMoney < Test::Unit::TestCase
27
27
  assert_equal price, cake.price
28
28
  end
29
29
  end
30
+
31
+ should 'have BigMoney default' do
32
+ price = BigMoney.new(0, :aud)
33
+ cake = Cake.create(:type => 'banana')
34
+ assert_equal price, cake.price
35
+ end
30
36
  end
31
37
  end # TestMoney
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-money
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Hanna
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-25 00:00:00 +11:00
12
+ date: 2010-02-23 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency