Class: FriendlyShipping::PackageOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/package_options.rb

Overview

Base class for package options. Used when serializing API requests.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_id:, item_options: Set.new, item_options_class: ItemOptions) ⇒ PackageOptions

Returns a new instance of PackageOptions.

Parameters:

  • package_id (String)

    the ID for the package that belongs to these options

  • item_options (Array<ItemOptions>) (defaults to: Set.new)

    the options for items in this package

  • item_options_class (Class) (defaults to: ItemOptions)

    the class to use for item options when none are provided



12
13
14
15
16
17
18
19
20
# File 'lib/friendly_shipping/package_options.rb', line 12

def initialize(
  package_id:,
  item_options: Set.new,
  item_options_class: ItemOptions
)
  @package_id = package_id
  @item_options = item_options
  @item_options_class = item_options_class
end

Instance Attribute Details

#package_idString (readonly)

Returns the ID for the package that belongs to these options.

Returns:

  • (String)

    the ID for the package that belongs to these options



7
8
9
# File 'lib/friendly_shipping/package_options.rb', line 7

def package_id
  @package_id
end

Instance Method Details

#options_for_item(item) ⇒ ItemOptions

Finds and returns item options for the given item. If options cannot be found, the item_options_class is used to construct new options.

Parameters:

  • item (#id)

    the item for which to get options

Returns:



27
28
29
30
31
# File 'lib/friendly_shipping/package_options.rb', line 27

def options_for_item(item)
  item_options.detect do |item_option|
    item_option.item_id == item.id
  end || item_options_class.new(item_id: nil)
end