Class: FreightKit::PackageItem
- Inherits:
-
Object
- Object
- FreightKit::PackageItem
- Defined in:
- lib/freight_kit/package_item.rb
Instance Attribute Summary collapse
-
#hs_code ⇒ Object
readonly
Returns the value of attribute hs_code.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#quantity ⇒ Object
readonly
Returns the value of attribute quantity.
-
#sku ⇒ Object
readonly
Returns the value of attribute sku.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #grams(options = {}) ⇒ Object (also: #g)
-
#initialize(name, grams_or_ounces, value, quantity, options = {}) ⇒ PackageItem
constructor
A new instance of PackageItem.
- #kilograms(options = {}) ⇒ Object (also: #kg, #kgs)
- #ounces(options = {}) ⇒ Object (also: #oz)
- #pounds(options = {}) ⇒ Object (also: #lb, #lbs)
- #weight(options = {}) ⇒ Object (also: #mass)
Constructor Details
#initialize(name, grams_or_ounces, value, quantity, options = {}) ⇒ PackageItem
Returns a new instance of PackageItem.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/freight_kit/package_item.rb', line 7 def initialize(name, grams_or_ounces, value, quantity, = {}) @name = name imperial = ([:units] == :imperial) @unit_system = imperial ? :imperial : :metric @weight = grams_or_ounces @weight = Measured::Weight.new( grams_or_ounces, (@unit_system == :imperial ? :oz : :g), ) unless @weight.is_a?(Measured::Weight) @value = Package.cents_from(value) @quantity = quantity > 0 ? quantity : 1 @sku = [:sku] @hs_code = [:hs_code] @options = end |
Instance Attribute Details
#hs_code ⇒ Object (readonly)
Returns the value of attribute hs_code.
5 6 7 |
# File 'lib/freight_kit/package_item.rb', line 5 def hs_code @hs_code end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/freight_kit/package_item.rb', line 5 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/freight_kit/package_item.rb', line 5 def @options end |
#quantity ⇒ Object (readonly)
Returns the value of attribute quantity.
5 6 7 |
# File 'lib/freight_kit/package_item.rb', line 5 def quantity @quantity end |
#sku ⇒ Object (readonly)
Returns the value of attribute sku.
5 6 7 |
# File 'lib/freight_kit/package_item.rb', line 5 def sku @sku end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
5 6 7 |
# File 'lib/freight_kit/package_item.rb', line 5 def value @value end |
Instance Method Details
#grams(options = {}) ⇒ Object Also known as: g
48 49 50 |
# File 'lib/freight_kit/package_item.rb', line 48 def grams( = {}) weight().convert_to(:g).value end |
#kilograms(options = {}) ⇒ Object Also known as: kg, kgs
59 60 61 |
# File 'lib/freight_kit/package_item.rb', line 59 def kilograms( = {}) weight().convert_to(:kg).value end |
#ounces(options = {}) ⇒ Object Also known as: oz
43 44 45 |
# File 'lib/freight_kit/package_item.rb', line 43 def ounces( = {}) weight().convert_to(:oz).value end |
#pounds(options = {}) ⇒ Object Also known as: lb, lbs
53 54 55 |
# File 'lib/freight_kit/package_item.rb', line 53 def pounds( = {}) weight().convert_to(:lb).value end |
#weight(options = {}) ⇒ Object Also known as: mass
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/freight_kit/package_item.rb', line 28 def weight( = {}) case [:type] when nil, :actual @weight when :volumetric, :dimensional @volumetric_weight ||= begin m = Measured::Weight.new((centimetres(:box_volume) / 6.0), :grams) @unit_system == :imperial ? m.in_ounces : m end when :billable [weight, weight(type: :volumetric)].max end end |