Class: Spree::Shipstation::Export::Weight
- Inherits:
-
Object
- Object
- Spree::Shipstation::Export::Weight
- Defined in:
- app/presenters/spree/shipstation/export/weight.rb
Overview
Value object converting a Spree::Variant's weight into the (value, unit-label) pair ShipStation's export XML expects.
ShipStation accepts Pounds, Ounces, and Grams; any unrecognised Spree weight unit (including a missing weight) falls back to Grams.
Instance Attribute Summary collapse
-
#units ⇒ Object
readonly
Returns the value of attribute units.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(value:, units:) ⇒ Weight
constructor
A new instance of Weight.
Constructor Details
#initialize(value:, units:) ⇒ Weight
Returns a new instance of Weight.
14 15 16 17 |
# File 'app/presenters/spree/shipstation/export/weight.rb', line 14 def initialize(value:, units:) @value = value @units = units end |
Instance Attribute Details
#units ⇒ Object (readonly)
Returns the value of attribute units.
12 13 14 |
# File 'app/presenters/spree/shipstation/export/weight.rb', line 12 def units @units end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
12 13 14 |
# File 'app/presenters/spree/shipstation/export/weight.rb', line 12 def value @value end |
Class Method Details
.from_variant(variant) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/presenters/spree/shipstation/export/weight.rb', line 19 def self.from_variant(variant) amount = (variant.weight || 0.0).to_f case variant.weight_unit when "lb" new(value: amount, units: "Pounds") when "oz" new(value: amount, units: "Ounces") when "kg" new(value: amount * 1000, units: "Grams") else new(value: amount, units: "Grams") end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
34 35 36 |
# File 'app/presenters/spree/shipstation/export/weight.rb', line 34 def ==(other) other.is_a?(self.class) && value == other.value && units == other.units end |
#hash ⇒ Object
39 40 41 |
# File 'app/presenters/spree/shipstation/export/weight.rb', line 39 def hash [value, units].hash end |