Class: MoneyType

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid_money_field/type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MoneyType

Returns a new instance of MoneyType.



4
5
6
7
8
9
10
11
# File 'lib/mongoid_money_field/type.rb', line 4

def initialize(options = {})
  @options = {
      fixed_currency: nil,
      default: nil,
      required: false,
      default_currency: nil
  }.merge(options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/mongoid_money_field/type.rb', line 2

def options
  @options
end

Instance Method Details

#demongoize(object) ⇒ Object

Get the object as it was stored in the database, and instantiate this custom class from it.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mongoid_money_field/type.rb', line 15

def demongoize(object)
  if object.is_a?(Hash)
    object = object.symbolize_keys

    if object.has_key?(:cents)
      if @options[:fixed_currency]
        ::Money.new(object[:cents], @options[:fixed_currency])
      else
        if object.has_key?(:currency_iso)
          ::Money.new(object[:cents], object[:currency_iso])
        else
          ::Money.new(object[:cents], @options[:default_currency])
        end
      end
    else
      nil
    end
  elsif object.is_a?(Fixnum) || object.is_a?(Float)
    if @options[:fixed_currency]
      ::Money.new(object, @options[:fixed_currency])
    else
      ::Money.new(object, @options[:default_currency])
    end
  else
    nil
  end
end

#evolve(object) ⇒ Object

Converts the object that was supplied to a criteria and converts it into a database friendly form.



80
81
82
83
84
85
# File 'lib/mongoid_money_field/type.rb', line 80

def evolve(object)
  case object
    when Money then object.mongoize
    else object
  end
end

#mongoize(object) ⇒ Object

Takes any possible object and converts it to how it would be stored in the database.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mongoid_money_field/type.rb', line 45

def mongoize(object)
  unless @options[:default_currency].nil?
    old_default = Money.default_currency
    Money.default_currency = Money::Currency.new(@options[:default_currency])
  end

  ret = case
    when object.is_a?(Money) then object.mongoize
    when object.is_a?(Hash) then
      object.symbolize_keys! if object.respond_to?(:symbolize_keys!)
      ::Money.new(object[:cents], object[:currency_iso]).mongoize
    when object.blank? then
      if !@options[:default].nil?
        @options[:default].to_money.mongoize
      else
        nil
      end
    when object.respond_to?(:to_money) then
      object.to_money.mongoize
    else object
  end

  unless @options[:default_currency].nil?
    Money.default_currency = old_default
  end
  
  if !ret.nil? && @options[:fixed_currency]
    ret[:currency_iso] = @options[:fixed_currency]
  end

  ret
end