Class: Workarea::ShippingOption

Inherits:
Object
  • Object
show all
Includes:
GuardNegativePrice
Defined in:
app/models/workarea/shipping_option.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GuardNegativePrice

#guard_negative_price

Constructor Details

#initialize(attributes = {}) ⇒ ShippingOption

Returns a new instance of ShippingOption.



21
22
23
24
25
26
# File 'app/models/workarea/shipping_option.rb', line 21

def initialize(attributes = {})
  @attributes = attributes.with_indifferent_access
  attributes.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
end

Instance Attribute Details

#carrierObject (readonly)

Returns the value of attribute carrier.



5
6
7
# File 'app/models/workarea/shipping_option.rb', line 5

def carrier
  @carrier
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'app/models/workarea/shipping_option.rb', line 5

def name
  @name
end

#price_adjustmentsObject



32
33
34
# File 'app/models/workarea/shipping_option.rb', line 32

def price_adjustments
  @price_adjustments || []
end

#service_codeObject (readonly)

Returns the value of attribute service_code.



5
6
7
# File 'app/models/workarea/shipping_option.rb', line 5

def service_code
  @service_code
end

#tax_codeObject (readonly)

Returns the value of attribute tax_code.



5
6
7
# File 'app/models/workarea/shipping_option.rb', line 5

def tax_code
  @tax_code
end

Class Method Details

.from_rate_estimate(rate) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/workarea/shipping_option.rb', line 8

def self.from_rate_estimate(rate)
  new(
    carrier: rate.carrier,
    name: rate.service_name,
    service_code: rate.service_code,
    price: Money.new(rate.price, rate.currency),
    tax_code: Shipping::Service.find_tax_code(
      rate.carrier,
      rate.service_name
    )
  )
end

Instance Method Details

#base_priceObject



28
29
30
# File 'app/models/workarea/shipping_option.rb', line 28

def base_price
  (@price || 0).to_m
end

#priceObject



36
37
38
39
40
# File 'app/models/workarea/shipping_option.rb', line 36

def price
  guard_negative_price do
    base_price + price_adjustments.map(&:amount).sum.to_m
  end
end

#to_hObject



42
43
44
45
46
47
48
49
50
51
# File 'app/models/workarea/shipping_option.rb', line 42

def to_h
  {
    carrier: carrier,
    name: name,
    service_code: service_code,
    price: price,
    base_price: base_price,
    tax_code: tax_code
  }
end