Skip to content

Commit

Permalink
Merge pull request #166 from next-tms/package-nil-dimensions
Browse files Browse the repository at this point in the history
Improve handling of `nil` `dimensions` on `Package.new`
  • Loading branch information
brodyhoskins authored Nov 17, 2024
2 parents cab110b + 6140eff commit cddd52d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/freight_kit/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ def initialize(total_grams_or_ounces, dimensions, packaging_type, options = {})
raise ArgumentError, 'Package#new: packaging_type is required' unless packaging_type
raise ArgumentError, 'Package#new: quantity is required' unless options[:quantity]

# For backward compatibility
if dimensions.is_a?(Array)
@dimensions = [dimensions].flatten.reject(&:nil?)
else
@dimensions = [dimensions.dig(:height), dimensions.dig(:width), dimensions.dig(:length)]
@dimensions = [@dimensions].flatten.reject(&:nil?)
end
@dimensions = case dimensions
when Array
dimensions.map(&:presence)
when Hash
[
dimensions[:height].presence,
dimensions[:width].presence,
dimensions[:length].presence,
]
else
[nil, nil, nil]
end

@description = options[:description]
@hazmat = options[:hazmat] == true
Expand Down

0 comments on commit cddd52d

Please sign in to comment.