Class: Cornerstore::Price

Inherits:
Model::Base show all
Includes:
Comparable
Defined in:
lib/cornerstore/api/price.rb

Instance Attribute Summary collapse

Attributes inherited from Model::Base

#_id, #_slugs, #created_at, #parent, #updated_at

Instance Method Summary collapse

Methods inherited from Model::Base

#==, #attributes=, #initialize, #inspect, #method_missing, method_missing, #to_param, #url

Constructor Details

This class inherits a constructor from Cornerstore::Model::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cornerstore::Model::Base

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



4
5
6
# File 'lib/cornerstore/api/price.rb', line 4

def amount
  @amount
end

#currencyObject

Returns the value of attribute currency.



4
5
6
# File 'lib/cornerstore/api/price.rb', line 4

def currency
  @currency
end

#grossObject

Returns the value of attribute gross.



4
5
6
# File 'lib/cornerstore/api/price.rb', line 4

def gross
  @gross
end

#netObject

Returns the value of attribute net.



4
5
6
# File 'lib/cornerstore/api/price.rb', line 4

def net
  @net
end

#taxObject

Returns the value of attribute tax.



4
5
6
# File 'lib/cornerstore/api/price.rb', line 4

def tax
  @tax
end

#tax_rateObject

Returns the value of attribute tax_rate.



4
5
6
# File 'lib/cornerstore/api/price.rb', line 4

def tax_rate
  @tax_rate
end

Instance Method Details

#+(other_object) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cornerstore/api/price.rb', line 44

def +(other_object)
  return self if not other_object or other_object == 0

  if gross
    Cornerstore::Price.new({
      gross:    gross + other_object.gross,
      net:      net + other_object.net,
      currency: currency,
      tax_rate: other_object.tax_rate == tax_rate ? tax_rate : nil
    })
  else
    Cornerstore::Price.new({
      amount:   amount + other_object.amount,
      currency: currency
    })

  end
end

#<=>(other_object) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/cornerstore/api/price.rb', line 33

def <=> (other_object)
  case other_object
  when Integer, Float, Fixnum
    self.amount <=> other_object
  when Cornerstore::Price
    self.amount <=> other_object.amount
  else
    raise ArgumentError, 'can only compare Integer, Float, Fixnum or Price objects with Price'
  end
end

#attributesObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cornerstore/api/price.rb', line 17

def attributes
  if gross or tax_rate
    {
      gross: gross,
      net: net,
      tax_rate: tax_rate,
      currency: currency
    }
  else
    {
      amount: amount,
      currency: currency
    }
  end
end

#currency_symbolObject



71
72
73
74
75
76
# File 'lib/cornerstore/api/price.rb', line 71

def currency_symbol
  {
    'USD' => '$',
    'EUR' => '',
  }[currency]
end

#to_fObject



63
64
65
# File 'lib/cornerstore/api/price.rb', line 63

def to_f
  @amount || @gross
end