Slide 1

Slide 1 text

3 How to Code Like a Writer Nickolas Means

Slide 2

Slide 2 text

Hi, I’m Nick. (@nmeans if you’re on “the twitters”) "

Slide 3

Slide 3 text

(hint: you’re in this town right now)

Slide 4

Slide 4 text

I build stuff at to help fix healthcare.

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

“If we're not engineers, what are we then? We're software writers!” – David Heinemer Hansson 2014 Railsconf Keynote

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

And now, some code.

Slide 14

Slide 14 text

Keep Ruby Weird, meet shipping_utilities.rb

Slide 15

Slide 15 text

module ShippingUtilities class ShippingException < Exception; end # Struct classes ShippingGroup = Struct.new( :order_line_items, :vendor, :shipping_cost, :applied_shipping_allowance ) unless const_defined?(:ShippingGroup) ShippingDisplayOption = Struct.new( :value, :text ) unless const_defined?(:ShippingDisplayOption) CHOOSE_COUNTRY_FIRST = [ ShippingDisplayOption.new( "", "Please choose destination country first" ) ] unless const_defined?("CHOOSE_COUNTRY_FIRST") # PO Box or Domestic FCM_DOMESTIC_SHIPPING_OPTION = [ ShippingDisplayOption.new( "usps_fcm", "USPS First Class Mail") ] unless const_defined?("FCM_DOMESTIC_SHIPPING_OPTION") FREE_LIGHTWEIGHT_MAIL_OPTION = [ ShippingDisplayOption.new( "free_usps_fcm", "USPS First Class Mail - FREE!") ] unless const_defined?("DISCOUNT_LIGHTWEIGHT_MAIL_OPTION") # Domestic DOMESTIC_COMMON_SHIPPING_OPTIONS = [ ShippingDisplayOption.new( "next_business_day", "Next Business Day" ), ShippingDisplayOption.new( "two_day", "Second Business Day" ), ShippingDisplayOption.new( "three_day", "Third Business Day" ) ] unless const_defined?("DOMESTIC_COMMON_SHIPPING_OPTIONS") STANDARD_GROUND_OPTION = [ ShippingDisplayOption.new( "ground", "Standard Ground" ) ] unless const_defined?("STANDARD_GROUND_OPTION") FREE_GROUND_OPTION = [ ShippingDisplayOption.new( "free_ground", "Standard Ground - FREE!" ) ] unless const_defined?("FREE_GROUND_OPTION") STANDARD_MAIL_OPTION = [ ShippingDisplayOption.new( "usps_domestic", "USPS Priority Mail") ] unless const_defined?("STANDARD_MAIL_OPTION") DISCOUNT_MAIL_OPTION = [ ShippingDisplayOption.new( "discounted_usps_domestic", "USPS Priority Mail - DISCOUNTED!") ] unless const_defined?("DISCOUNT_MAIL_OPTION") # LOWER 48 FS_PROMO_DOMESTIC_SHIPPING_OPTIONS = DOMESTIC_COMMON_SHIPPING_OPTIONS + FREE_GROUND_OPTION FS_PROMO_LIGHTWEIGHT_SHIPPING_OPTIONS = DOMESTIC_COMMON_SHIPPING_OPTIONS + STANDARD_GROUND_OPTION + FREE_LIGHTWEIGHT_MAIL_OPTION STANDARD_DOMESTIC_SHIPPING_OPTIONS = DOMESTIC_COMMON_SHIPPING_OPTIONS + STANDARD_GROUND_OPTION LIGHTWEIGHT_DOMESTIC_SHIPPING_OPTIONS = STANDARD_DOMESTIC_SHIPPING_OPTIONS + FCM_DOMESTIC_SHIPPING_OPTION PO_BOX_DOMESTIC_SHIPPING_OPTIONS = STANDARD_MAIL_OPTION LIGHTWEIGHT_PO_BOX_DOMESTIC_SHIPPING_OPTIONS = STANDARD_MAIL_OPTION + FCM_DOMESTIC_SHIPPING_OPTION # AK/HI FS_PROMO_AK_HI_SHIPPING_OPTIONS = DOMESTIC_COMMON_SHIPPING_OPTIONS + STANDARD_GROUND_OPTION + DISCOUNT_MAIL_OPTION FS_PROMO_LIGHTWEIGHT_AK_HI_SHIPPING_OPTIONS = DOMESTIC_COMMON_SHIPPING_OPTIONS + STANDARD_GROUND_OPTION + STANDARD_MAIL_OPTION + FREE_LIGHTWEIGHT_MAIL_OPTION STANDARD_AK_HI_SHIPPING_OPTIONS = DOMESTIC_COMMON_SHIPPING_OPTIONS + STANDARD_GROUND_OPTION + STANDARD_MAIL_OPTION LIGHTWEIGHT_AK_HI_SHIPPING_OPTIONS = STANDARD_AK_HI_SHIPPING_OPTIONS + FCM_DOMESTIC_SHIPPING_OPTION # TERRITORIES FS_PROMO_TERRITORIES_SHIPPING_OPTIONS = DISCOUNT_MAIL_OPTION FS_PROMO_LIGHTWEIGHT_TERRITORIES_SHIPPING_OPTIONS = STANDARD_MAIL_OPTION + FREE_LIGHTWEIGHT_MAIL_OPTION STANDARD_TERRITORIES_SHIPPING_OPTIONS = STANDARD_MAIL_OPTION LIGHTWEIGHT_TERRITORIES_SHIPPING_OPTIONS = STANDARD_MAIL_OPTION + FCM_DOMESTIC_SHIPPING_OPTION # International COMMON_INTERNATIONAL_SHIPPING_OPTIONS = [ ShippingDisplayOption.new( "ups_express", "UPS Worldwide Express [1-3 days]" ), ShippingDisplayOption.new( "ups_expedited", "UPS Worldwide Expedited [2-5 days]" ), ShippingDisplayOption.new( "usps_international", "USPS Express Mail International") ] unless const_defined?("INTERNATIONAL_SHIPPING_OPTIONS") DISCOUNTED_INTERNATIONAL_PMI_OPTION = [ ShippingDisplayOption.new( "discounted_usps_int_priority", "USPS Priority Mail International - DISCOUNTED!") ] unless const_defined?("DISCOUNTED_INTERNATIONAL_PMI_OPTION") STANDARD_INTERNATIONAL_PMI_OPTION = [ ShippingDisplayOption.new( "usps_int_priority", "USPS Priority Mail International") ] unless const_defined?("STANDARD_INTERNATIONAL_PMI_OPTION") FS_PROMO_INTERNATIONAL_SHIPPING_OPTIONS = COMMON_INTERNATIONAL_SHIPPING_OPTIONS + DISCOUNTED_INTERNATIONAL_PMI_OPTION STANDARD_INTERNATIONAL_SHIPPING_OPTIONS = COMMON_INTERNATIONAL_SHIPPING_OPTIONS + STANDARD_INTERNATIONAL_PMI_OPTION ALL_SHIPPING_OPTIONS = DOMESTIC_COMMON_SHIPPING_OPTIONS + FREE_GROUND_OPTION + STANDARD_GROUND_OPTION + COMMON_INTERNATIONAL_SHIPPING_OPTIONS + DISCOUNTED_INTERNATIONAL_PMI_OPTION + STANDARD_INTERNATIONAL_PMI_OPTION + FCM_DOMESTIC_SHIPPING_OPTION + FREE_LIGHTWEIGHT_MAIL_OPTION + DISCOUNT_MAIL_OPTION + STANDARD_MAIL_OPTION unless const_defined?("ALL_SHIPPING_OPTIONS") INTERNATIONAL_SHIPPING_METHODS = [ ShippingMethod.new( :shipping_option => :usps_int_priority, :api => :usps, :name => "USPS Priority Mail International", :mail_class => "PriorityMailInternational", :multiplier => 1.45), ShippingMethod.new( :shipping_option => :usps_international, :api => :usps, :name => "USPS Express Mail International", :mail_class => "ExpressMailInternational", :multiplier => 1.45), ShippingMethod.new( :shipping_option => :ups_express, :api => :ups, :name => "UPS Worldwide Express", :service_code => "07", :multiplier => 0.7), ShippingMethod.new( :shipping_option => :ups_expedited, :api => :ups, :name => "UPS Worldwide Expedited", :service_code => "08", :multiplier => 0.7) ] unless const_defined?("INTERNATIONAL_SHIPPING_METHODS") DOMESTIC_FEDEX_SHIPPING_METHODS = [ ShippingMethod.new( :shipping_option => :ground, :api => :fedex, :name => "FedEx Ground", :transaction_type => "rate_ground", :service_type => "ground_service", :multiplier => 1.4 ), ShippingMethod.new( :shipping_option => :ground_res, :api => :fedex, :name => "FedEx Home Delivery", :transaction_type => "rate_ground", :service_type => "home_delivery", :multiplier => 1.4), ShippingMethod.new( :shipping_option => :three_day, :api => :fedex, :name => "FedEx Express Saver (3 Day)", :transaction_type => "rate_express", :service_type => "express_saver", :multiplier => 1.25 ), ShippingMethod.new( :shipping_option => :two_day, :api => :fedex, :name => "FedEx 2 Day", :transaction_type => "rate_express", :service_type => "2day", :multiplier => 1.25 ), ShippingMethod.new( :shipping_option => :next_business_day, :api => :fedex, :name => "FedEx Standard Overnight", :transaction_type => "rate_express", :service_type => "standard_overnight", :multiplier => 1.25 ) ] unless const_defined?("DOMESTIC_FEDEX_SHIPPING_METHODS") DOMESTIC_UPS_SHIPPING_METHODS = [ ShippingMethod.new( :shipping_option => :ground, :api => :ups, :name => "UPS Ground", :service_code => "03"), ShippingMethod.new( :shipping_option => :three_day, :api => :ups, :name => "UPS 3-Day Select", :service_code => "12", :multiplier => 1.1), ShippingMethod.new( :shipping_option => :two_day, :api => :ups, :name => "UPS 2nd Day Air", :service_code => "02", :multiplier => 1.1), ShippingMethod.new( :shipping_option => :next_business_day, :api => :ups, :name => "UPS Next Day Air", :service_code => "01", :multiplier => 1.1) ] unless const_defined?("DOMESTIC_UPS_SHIPPING_METHODS") DOMESTIC_USPS_SHIPPING_METHOD = [ ShippingMethod.new( :shipping_option => :upsmi_domestic, :api => :upsmi, :name => "UPS Mail Innovations", :service => "domestic"), ShippingMethod.new( :shipping_option => :usps_fcm, :api => :usps, :name => "USPS First Class Mail", :mail_class => "FIRST"), ShippingMethod.new( :shipping_option => :usps_domestic, :api => :usps, :name => "USPS Priority Mail", :mail_class => "PRIORITY") ] unless const_defined?("DOMESTIC_USPS_SHIPPING_METHOD") ALL_SHIPPING_METHODS = INTERNATIONAL_SHIPPING_METHODS + DOMESTIC_FEDEX_SHIPPING_METHODS + DOMESTIC_UPS_SHIPPING_METHODS + DOMESTIC_USPS_SHIPPING_METHOD # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Lookup the ShippingMethod object based on the shipping code passed into # the method here. # # Note that shipping_code will be one of the symbols based on the # :shipping_option => 'XXXXX' in the above declared ShippingMethod instances. # # Therefore shipping_code will be something like: # "ground", "three_day", "two_day", "next_business_day", "usps_domestic", etc. # # # shipping_code will be a STRING... NOT A SYMBOL! # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def lookup_shipping_method( shipping_code, api_override = false ) # Try domestic shipping first. For domestic we need to match based on the # shipping code ***AND*** the currently configured shipping API - which # will be either Fedex or UPS #RAILS_DEFAULT_LOGGER.debug "Looking up shipping method based on this code: #{shipping_code}." # Check to see what the currently configured shipping method is - Set by an administrator # in the admin interface. The only valid values are "ups" and "fedex" if api_override api = api_override else # Default shipping method is UPS api = "ups" end case api when "ups" shipping_method = get_shipping_method_from_array( shipping_code, DOMESTIC_UPS_SHIPPING_METHODS) when "fedex" shipping_method = get_shipping_method_from_array( shipping_code, DOMESTIC_FEDEX_SHIPPING_METHODS) end # ...if it turns out that we found an appropriate UPS or Fedex method from above - return it... return shipping_method unless shipping_method.nil? # ...otherwise lets see if it is a USPS domestic if ["usps_domestic", "upsmi_domestic", "usps_fcm"].include? shipping_code return get_shipping_method_from_array( shipping_code, DOMESTIC_USPS_SHIPPING_METHOD ) end # ... otherwise it must be]an international return get_shipping_method_from_array( shipping_code, INTERNATIONAL_SHIPPING_METHODS) end # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Get the generic shipping method currently selected # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def lookup_generic_shipping_method( shipping_code ) ALL_SHIPPING_OPTIONS.each do |opt| if opt.value == shipping_code return opt end end return nil end def lookup_generic_shipping_method_by_name( method_name ) ALL_SHIPPING_METHODS.each do |opt| if opt.sm_params[:name] == method_name return opt.sm_params[:shipping_option] end end return nil end def lookup_shipping_method_by_name( method_name ) ALL_SHIPPING_METHODS.each do |opt| if opt.sm_params[:name] == method_name return opt end end return nil end # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Get the total shipping cost for the order. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def get_shipping_cost_for_order( all_items, receiver_zip, receiver_country, in_stock_shipping_method_code, out_of_stock_shipping_method_code, consolidate = false ) raise Exception.new( "ShippingUtilities::get_shipping_cost_for_order is deprecated! Please use new ShippingUtilities::build_shipping_groups instead." ) end # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the shipping weight for some items # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def shipping_weight_for_items(order_line_items) weight = 0.0 order_line_items.each { |order_line_item| unless order_line_item.item.shipping_weight.nil? or order_line_item.item.shipping_weight == 0 weight += (order_line_item.quantity * order_line_item.item.shipping_weight) else weight += (order_line_item.quantity * 5) end } return weight end private # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Get a shipping method from a narrowed down array of options # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def get_shipping_method_from_array( code, methods ) #RAILS_DEFAULT_LOGGER.debug "Code is: #{code}. Methods are: #{methods.inspect}" methods.each do |shipping_method| #RAILS_DEFAULT_LOGGER.debug "Shipping Method Compare: #{shipping_method.sm_params[:shipping_option]}" if shipping_method.sm_params[:shipping_option].to_s == code #RAILS_DEFAULT_LOGGER.debug "Returning Shipping Method: #{shipping_method.inspect}" return shipping_method end end return nil end # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Technically the shipping utilities shouldn't have any awareness of what store # to bill stuff to - but given that we need some "special" logic to determine # what stores shipping credentials to use depending on what items are in the cart # this seems like the best option. # # If the cart has any NEML products - then use NEML # If the card is mixed up of NEMX and NEHP only - then use NEMX # Otherwise use whatever store that all products are from # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def determine_store_for_shipping( all_items ) return Store::NEML if all_items.length < 1 stores = Store.stores_for_items( all_items.collect{ |order_line_item| order_line_item.item || order_line_item.used_item } ) if stores.length == 1 return stores.values.first else if stores[Store::NEML] != nil return stores[Store::NEML] else return stores[Store::NEHP] end end end # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Calculate the shipping costs for a group of products. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def calculate_shipping_costs_for_items( store, order_line_items, sender_zip, receiver_zip, receiver_country, shipping_method_code, consolidate = false ) # Before doing anything, check the validity of the zip code if we're shipping to the US. # Raise an exception if it's bunk. if receiver_country == "US" ShippingCalculatorBase.state_from_zip( receiver_zip ) end shipping_weight = shipping_weight_for_items( order_line_items ) ship_symbol = consolidate ? :order_in : order_line_items[0].item.shipping_status_symbol # Prevent exception if sender_zip is not defined (eg. zip not in Vendor table) if sender_zip.nil? sender_zip = 79363 end RAILS_DEFAULT_LOGGER.info "Shipping symbol - #{ship_symbol}" if ship_symbol == :drop_ship api_override = "ups" else api_override = false end RAILS_DEFAULT_LOGGER.info "Method: #{shipping_method_code}, Override: #{api_override}, Store: #{store.id}" shipping_method = lookup_shipping_method( shipping_method_code, api_override ) RAILS_DEFAULT_LOGGER.info "We found: #{shipping_method.inspect}" ship_api = shipping_method.sm_params[:api] # If we don't have an explicit multiplier, we want to use the price # returned from the API as is (so mult by 1.0) # DEPRECATED - NEW CODE BELOW! # shipping_method.sm_params[:multiplier] ||= 1.0 mult = Multiplier.find(:first, :conditions => ["store_id = ? and service = ?", store.id, shipping_method_code], :select => "ds, wh").send((ship_symbol == :drop_ship && receiver_country == "US") ? "ds" : "wh") min = store.send(ship_symbol == :drop_ship ? "ds_min" : "wh_min") ship_params = Hash.new ship_params[:shipping_weight] = shipping_weight ship_params[:sender_zip] = sender_zip ship_params[:receiver_zip] = receiver_zip ship_params[:receiver_country] = receiver_country case ship_api when :fedex ship_params[:fedex_account] = store.fedex_account ship_params[:fedex_url] = NewEnough::FEDEX_URL ship_params[:fedex_meter] = store.fedex_meter ship_params[:service_type] = shipping_method.sm_params[:service_type] ship_params[:transaction_type] = shipping_method.sm_params[:transaction_type] amt = (FedexCalculator.new.price( ship_params ) * mult).prec(2) when :ups ship_params[:ups_user] = NewEnough::UPS_USER ship_params[:ups_pass] = NewEnough::UPS_PASS ship_params[:ups_license] = NewEnough::UPS_LICENSE ship_params[:sender_country] = store.country ship_params[:sender_phone] = store.phone_number ship_params[:sender_name] = store.name ship_params[:receiver_country] = receiver_country ship_params[:shipping_method] = shipping_method.sm_params[:service_code] amt = (UpsCalculator.new.price( ship_params ) * mult).prec(2) when :usps ship_params[:mail_class] = shipping_method.sm_params[:mail_class] ship_params[:partner_id] = NewEnough::ENDICIA_PARTNER_ID ship_params[:account_id] = NewEnough::ENDICIA_ACCOUNT_ID ship_params[:pass_phrase] = NewEnough::ENDICIA_PASS_PHRASE amt = (UspsCalculator.new.price( ship_params ) * mult).prec(2) when :upsmi ship_params[:service] = shipping_method.sm_params[:service] amt = (UpsmiCalculator.new.price( ship_params ) * mult).prec(2) end return amt > min ? amt : min end # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) RAILS_DEFAULT_LOGGER.info("Build shipments found item: #{item.inspect} from store #{item.store_id}") ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 RAILS_DEFAULT_LOGGER.debug("Attempting to consolidate in_stocks and drop_ships.") # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 RAILS_DEFAULT_LOGGER.debug("Consolidating in_stocks and drop_ships.") #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end set_consolidate_flags( shipments ) RAILS_DEFAULT_LOGGER.info "=====================================================" RAILS_DEFAULT_LOGGER.info "Shipments" RAILS_DEFAULT_LOGGER.info "=====================================================" RAILS_DEFAULT_LOGGER.info "#{shipments.inspect}" RAILS_DEFAULT_LOGGER.info "=====================================================" return shipments end def set_consolidate_flags( shipments ) #RAILS_DEFAULT_LOGGER.debug shipments.to_yaml # Now let's set appropriate consolidate flags on the shipments that are to be shipped together (NEML & NEHP) in_stock = [] order_in = [] consol = [] shipments.each do |s| if s.shipment_type == Shipment::IN_STOCK in_stock << s elsif s.shipment_type == Shipment::ORDER_IN order_in << s elsif s.shipment_type == Shipment::CONSOLIDATED consol << s end end # NEMX_TODO - Make this more robust if we need to combine with NEMX in the future. groups = [in_stock, order_in, consol] groups.each do |g| #RAILS_DEFAULT_LOGGER.debug "================================================================" #RAILS_DEFAULT_LOGGER.debug "Group #{g.inspect}" #RAILS_DEFAULT_LOGGER.debug "================================================================" if g.length == 2 # We can assume here that shipper_id == store_id always because a drop-ship from one store # will never be combined with a drop-ship from another store. If shipment was drop-shipped # shipper_id == vendor_id. if g[1].shipper_id == 1 g[0].combine_flag = "combine" g[1].combine_flag = "has_combined_shipment" g[1].combine_with_shipment = g[0] else g[1].combine_flag = "combine" g[0].combine_flag = "has_combined_shipment" g[0].combine_with_shipment = g[1] end end end end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) found = false shipments.each do |shipment| if shipment.shipment_type == shipment.shipment_type_from_symbol( key ) && shipment.shipper_id == shipper_id && shipment.store_id == store_id shipment.line_items << order_line_item found = true end end if found == false shipment = Shipment.new shipment.shipment_type = shipment.shipment_type_from_symbol( key ) shipment.shipper_id = shipper_id shipment.store_id = store_id shipment.line_items << order_line_item shipments << shipment end end # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # If shipments will be combined, consolidate them for the public view # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipping_groups( shipments, ship_method, customer_zip, customer_country, consolidate = false ) RAILS_DEFAULT_LOGGER.debug "In build_shipping_groups, ship_method: #{ship_method}" shipping_groups = {} shipments.each do |shipment| unless shipment.combine_flag == "combine" @group_line_items = [] shipment.line_items.each {|li| @group_line_items << li } if !shipment.combine_with_shipment.nil? shipment.combine_with_shipment.line_items.each {|cli| @group_line_items << cli } end ship_symbol = consolidate ? :order_in : @group_line_items[0].item.shipping_status_symbol shipping_weight = shipping_weight_for_items( @group_line_items ) if ship_method == 'free_ground' && !shipment.has_oversized_item? if ship_symbol == :drop_ship shipment.shipping_method = 'ground' RAILS_DEFAULT_LOGGER.debug("Drop Ship") elsif shipping_weight <= 5.0 # This is where the UPS Basic selection will kick in once we're approved. # TODO: UPS Basic shipment.shipping_method = 'ground' RAILS_DEFAULT_LOGGER.debug("< 5#") else shipment.shipping_method = 'ground' RAILS_DEFAULT_LOGGER.debug("Normal") end elsif ship_method == 'free_ground' && shipment.has_oversized_item? shipment.shipping_method = 'ground' elsif ship_method == 'discounted_usps_domestic' shipment.shipping_method = 'usps_domestic' elsif ship_method == 'discounted_usps_int_priority' shipment.shipping_method = 'usps_int_priority' elsif ship_method == 'free_usps_fcm' shipment.shipping_method = 'usps_fcm' else shipment.shipping_method = ship_method end shipment.shipping_cost = calculate_shipping_costs_for_items( determine_store_for_shipping(@group_line_items), @group_line_items, shipment.ships_from.zip_code, customer_zip, customer_country, shipment.shipping_method, consolidate) if ship_method == 'free_ground' shipment.applied_shipping_allowance = shipment.has_oversized_item? ? 0.0 : shipment.shipping_cost elsif ship_method == 'free_usps_fcm' shipment.applied_shipping_allowance = shipment.shipping_cost elsif ['discounted_usps_int_priority', 'discounted_usps_domestic'].include?(ship_method) shipment.applied_shipping_allowance = equivalent_ship_discount_for_weight( shipping_weight ) shipment.applied_shipping_allowance = shipment.shipping_cost if shipment.applied_shipping_allowance > shipment.shipping_cost else shipment.applied_shipping_allowance = 0.0 end if shipment.shipment_type == Shipment::DROP_SHIP vendor = shipment.ships_from key = "#{vendor.id}-#{shipment.store_id}" else vendor = nil key = shipment.shipment_type_symbol end add_key_if_necessary_and_insert( shipping_groups, key, shipment, @group_line_items, vendor) end # unless end RAILS_DEFAULT_LOGGER.info "=====================================================" RAILS_DEFAULT_LOGGER.info "Shipping Groups" RAILS_DEFAULT_LOGGER.info "=====================================================" RAILS_DEFAULT_LOGGER.info "#{shipping_groups.inspect}" RAILS_DEFAULT_LOGGER.info "=====================================================" return shipping_groups end def add_key_if_necessary_and_insert( shipping_groups, key, shipment, order_line_items, vendor ) if shipping_groups[ key ].nil? shipping_groups[key] = ShippingGroup.new( [], vendor, shipment.shipping_cost, shipment.applied_shipping_allowance ) end order_line_items.each do |item| shipping_groups[key].order_line_items << item end end def equivalent_ship_discount_for_weight( weight_in_pounds ) # Base of $6.53 covers shipments up to 1 lb. # We need to figure out rounded-up weight over 1 lb. additional_poundage = weight_in_pounds - 1 ship_discount = 6.53 + (additional_poundage * 0.29) # Now we add in the ground multiplier so the rate is realistic # if someone plays looky-loo with our domestic rates mult = Multiplier.find(:first, :conditions => {:store_id => 1, :service => 'ground'}).wh return (ship_discount * mult).prec(2) end end

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Some Statistics • 651 lines of code, 427 SLOC • Flog score: 561.1 nmeans@nicksair% flog app/models/shipping_utilities.rb 561.1: flog total 35.1: flog/method average 120.7: ShippingUtilities#build_shipments 98.0: ShippingUtilities#build_shipping_groups 96.9: ShippingUtilities#none 88.4: ShippingUtilities#calculate_shipping_costs_for_items • Flay score: 70 • CodeClimate Grade: F

Slide 18

Slide 18 text

Some Statistics • 651 lines of code, 427 SLOC • Flog score: 561.1 nmeans@nicksair% flog app/models/shipping_utilities.rb 561.1: flog total 35.1: flog/method average 120.7: ShippingUtilities#build_shipments 98.0: ShippingUtilities#build_shipping_groups 96.9: ShippingUtilities#none 88.4: ShippingUtilities#calculate_shipping_costs_for_items • Flay score: 70 • CodeClimate Grade: F

Slide 19

Slide 19 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] RAILS_DEFAULT_LOGGER.info(“Line Items: #{order_line_items.inspect}”) order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) RAILS_DEFAULT_LOGGER.info("Build shipments found item: #{item.inspect} from store #{item.store_id}") ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 RAILS_DEFAULT_LOGGER.debug("Attempting to consolidate in_stocks and drop_ships.") # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 RAILS_DEFAULT_LOGGER.debug("Consolidating in_stocks and drop_ships.") #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end set_consolidate_flags( shipments ) RAILS_DEFAULT_LOGGER.info "=====================================================" RAILS_DEFAULT_LOGGER.info "Shipments" RAILS_DEFAULT_LOGGER.info "=====================================================" RAILS_DEFAULT_LOGGER.info "#{shipments.inspect}" RAILS_DEFAULT_LOGGER.info "=====================================================" return shipments end

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

“We'll take some smelly Ruby and refactor it using only advice from Strunk and White's 
 The Elements of Style.” – Some Idiot 2014 KRW Talk Proposal

Slide 22

Slide 22 text

What, then, shall we do? V

Slide 23

Slide 23 text

Strunk and White 5.5 Revise and rewrite Revising is part of writing. Few writers are so expert that they can produce what they are after on the first try. Quite often you will discover, on examining the completed work, that there are serious flaws in the arrangement of the material, calling for transpositions.

Slide 24

Slide 24 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] RAILS_DEFAULT_LOGGER.info(“Line Items: #{order_line_items.inspect}”) order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) RAILS_DEFAULT_LOGGER.info("Build shipments found item: #{item.inspect} from store #{item.store_id}") ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 RAILS_DEFAULT_LOGGER.debug("Attempting to consolidate in_stocks and drop_ships.") # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 RAILS_DEFAULT_LOGGER.debug("Consolidating in_stocks and drop_ships.") #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end set_consolidate_flags( shipments ) RAILS_DEFAULT_LOGGER.info "=====================================================" RAILS_DEFAULT_LOGGER.info "Shipments" RAILS_DEFAULT_LOGGER.info "=====================================================" RAILS_DEFAULT_LOGGER.info "#{shipments.inspect}" RAILS_DEFAULT_LOGGER.info "=====================================================" return shipments end

Slide 25

Slide 25 text

Strunk and White 5.3 Work from a suitable design Before beginning to compose something, gauge the nature and extent of the enterprise and work from a suitable design.

Slide 26

Slide 26 text

In Stock! Vendor Foo Shopping Cart

Slide 27

Slide 27 text

In Stock! Vendor Foo Drop Ship Vendor Foo Shopping Cart

Slide 28

Slide 28 text

In Stock! Vendor Foo Drop Ship Vendor Foo Drop Ship Vendor Bar Shopping Cart

Slide 29

Slide 29 text

Shopping Cart In Stock! Vendor Foo Drop Ship Vendor Foo Drop Ship Vendor Bar

Slide 30

Slide 30 text

Shopping Cart In Stock! Vendor Foo Drop Ship Vendor Foo Drop Ship Vendor Bar

Slide 31

Slide 31 text

Shopping Cart In Stock! Vendor Foo Drop Ship Vendor Foo Drop Ship Vendor Bar

Slide 32

Slide 32 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] RAILS_DEFAULT_LOGGER.info(“Line Items: #{order_line_items.inspect}”) order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) RAILS_DEFAULT_LOGGER.info("Build shipments found item: #{item.inspect} from store #{item.store_id}") ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 RAILS_DEFAULT_LOGGER.debug("Attempting to consolidate in_stocks and drop_ships.") # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 RAILS_DEFAULT_LOGGER.debug("Consolidating in_stocks and drop_ships.") #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end RAILS_DEFAULT_LOGGER.info "=====================================================" RAILS_DEFAULT_LOGGER.info "Shipments" RAILS_DEFAULT_LOGGER.info "=====================================================" RAILS_DEFAULT_LOGGER.info "#{shipments.inspect}" RAILS_DEFAULT_LOGGER.info "=====================================================" return shipments end

Slide 33

Slide 33 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) ... end

Slide 34

Slide 34 text

class ShipmentBuilder # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def initialize(order_line_items, consolidate = false) build_shipments(order_line_items, consolidate) end def build_shipments( order_line_items, consolidate) ... end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) found = false shipments.each do |shipment| if shipment.shipment_type == key && shipment.shipper_id == shipper_id && shipment.store_id == store_id shipment.line_items << order_line_item found = true end end if found == false shipment = Shipment.new shipment.shipment_type = key shipment.shipper_id = shipper_id shipment.store_id = store_id shipment.line_items << order_line_item shipments << shipment end end end

Slide 35

Slide 35 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1) end

Slide 36

Slide 36 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1) end nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 45718 # Running: Finished in 0.001471s, 679.8097 runs/s, 0.0000 assertions/s. 1) Error: ShipmentBuilder#test_0001_exists: NameError: uninitialized constant ShipmentBuilder::RAILS_DEFAULT_LOGGER 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

Slide 37

Slide 37 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] RAILS_DEFAULT_LOGGER.info(“Line Items: #{order_line_items.inspect}”) order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) RAILS_DEFAULT_LOGGER.info("Build shipments found item: #{item.inspect} from store #{item.store_id}") ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 RAILS_DEFAULT_LOGGER.debug("Attempting to consolidate in_stocks and drop_ships.") # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 RAILS_DEFAULT_LOGGER.debug("Consolidating in_stocks and drop_ships.") #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end RAILS_DEFAULT_LOGGER.info "=====================================================" RAILS_DEFAULT_LOGGER.info "Shipments" RAILS_DEFAULT_LOGGER.info "=====================================================" RAILS_DEFAULT_LOGGER.info "#{shipments.inspect}" RAILS_DEFAULT_LOGGER.info "=====================================================" return shipments end

Slide 38

Slide 38 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end return shipments end

Slide 39

Slide 39 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 54189 # Running: Finished in 0.001391s, 718.9073 runs/s, 0.0000 assertions/s. 1) Error: ShipmentBuilder#test_0001_exists: NameError: uninitialized constant ShipmentBuilder::Item 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

Slide 40

Slide 40 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end return shipments end

Slide 41

Slide 41 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end return shipments end

Slide 42

Slide 42 text

item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id)

Slide 43

Slide 43 text

Strunk and White 5.19 Do not take shortcuts at the cost of clarity Many shortcuts are self-defeating; they waste the reader’s time instead of conserving it. The one truly reliable shortcut in writing is to choose words that are strong and surefooted to carry readers on their way.

Slide 44

Slide 44 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end return shipments end

Slide 45

Slide 45 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end return shipments end

Slide 46

Slide 46 text

line_items = [] order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 47

Slide 47 text

line_items = [] order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 48

Slide 48 text

line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 49

Slide 49 text

line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 50

Slide 50 text

line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 51

Slide 51 text

line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 52

Slide 52 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 53

Slide 53 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 54

Slide 54 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 55

Slide 55 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 56

Slide 56 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 57

Slide 57 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 58

Slide 58 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 59

Slide 59 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 60

Slide 60 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 61

Slide 61 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 62

Slide 62 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 63

Slide 63 text

Strunk and White 2.19 Express coordinate ideas in similar form This principle, that of parallel construction, requires that expressions similar in content and function be outwardly similar. The likeness of form enables the reader to recognize more readily the likeness of content and function.

Slide 64

Slide 64 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 65

Slide 65 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 66

Slide 66 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable => true) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 67

Slide 67 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable => true) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end

Slide 68

Slide 68 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable => true) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable, :consolidatable => false) end

Slide 69

Slide 69 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 70

Slide 70 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 71

Slide 71 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 72

Slide 72 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 73

Slide 73 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 74

Slide 74 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 75

Slide 75 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 76

Slide 76 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 77

Slide 77 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 78

Slide 78 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 79

Slide 79 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do it "exists" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end " line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 80

Slide 80 text

line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 81

Slide 81 text

line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 82

Slide 82 text

line_items = [] order_line_items.each do |order_line_item| item = if order_line_item.item_id Item.find(order_line_item.item_id, :include => :product) else UsedItem.find(order_line_item.used_item_id) end line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 83

Slide 83 text

line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end

Slide 84

Slide 84 text

def build_shipments( order_line_items, consolidate) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end return shipments end

Slide 85

Slide 85 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 22228 # Running: Finished in 0.001502s, 665.7790 runs/s, 665.7790 assertions/s. 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

Slide 86

Slide 86 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end return shipments end

Slide 87

Slide 87 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do before { Object.send(:const_set, :Shipment, ShipmentDouble) } after { Object.send(:remove_const, :Shipment) } it “executes when consolidate is false" do assert ShipmentBuilder.new([order_line_item_double]) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end class ShipmentDouble < OpenStruct def initialize(*) super self.line_items = [] end end

Slide 88

Slide 88 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do before { Object.send(:const_set, :Shipment, ShipmentDouble) } after { Object.send(:remove_const, :Shipment) } it “executes when consolidate is false" do assert ShipmentBuilder.new([order_line_item_double]) end it "executes when consolidate is true" do assert ShipmentBuilder.new([order_line_item_double], true) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end class ShipmentDouble < OpenStruct def initialize(*) super self.line_items = [] end end

Slide 89

Slide 89 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 11912 # Running: Finished in 0.001663s, 1202.6458 runs/s, 601.3229 assertions/s. 1) Error: ShipmentBuilder#test_0002_executes when consolidate is true: NameError: uninitialized constant ShipmentBuilder::Item 2 runs, assertions, 0 failures, 1 errors, 0 skips

Slide 90

Slide 90 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end return shipments end

Slide 91

Slide 91 text

order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item ) end

Slide 92

Slide 92 text

order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item ) end

Slide 93

Slide 93 text

order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item ) end

Slide 94

Slide 94 text

order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item ) end

Slide 95

Slide 95 text

order_line_items.each do |order_line_item| 
 create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item ) end

Slide 96

Slide 96 text

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end

Slide 97

Slide 97 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 28631 # Running: Finished in 0.001898s, 1053.7408 runs/s, 1053.7408 assertions/s. 2 runs, 2 assertions, 0 failures, 0 errors, 0 skips

Slide 98

Slide 98 text

class ShipmentBuilder # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def initialize(order_line_items, consolidate = false) build_shipments(order_line_items, consolidate) end def build_shipments( order_line_items, consolidate) ...
 return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end " > ShipmentBuilder.new([order_line_item1, order_line_item2]) => [<#Shipment ...>, <#Shipment ...>]

Slide 99

Slide 99 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do before { Object.send(:const_set, :Shipment, ShipmentDouble) } after { Object.send(:remove_const, :Shipment) } it “executes when consolidate is false" do assert ShipmentBuilder.new([order_line_item_double]) end it "executes when consolidate is true" do assert ShipmentBuilder.new([order_line_item_double], true) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end class ShipmentDouble < OpenStruct def initialize(*) super self.line_items = [] end end

Slide 100

Slide 100 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do before { Object.send(:const_set, :Shipment, ShipmentDouble) } after { Object.send(:remove_const, :Shipment) } it "returns a single in_stock shipment" do shipments = ShipmentBuilder.new([order_line_item_double, order_line_item_double]).build_shipments shipments.length.must_equal 1 shipments.first.shipment_type.must_equal :in_stock end it "executes when consolidate is true" do assert ShipmentBuilder.new([order_line_item_double], true) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end class ShipmentDouble < OpenStruct def initialize(*) super self.line_items = [] end end

Slide 101

Slide 101 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do before { Object.send(:const_set, :Shipment, ShipmentDouble) } after { Object.send(:remove_const, :Shipment) } it "returns a single in_stock shipment" do shipments = ShipmentBuilder.new([order_line_item_double, order_line_item_double]).build_shipments shipments.length.must_equal 1 shipments.first.shipment_type.must_equal :in_stock end it "executes when consolidate is true" do assert ShipmentBuilder.new([order_line_item_double], true) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end class ShipmentDouble < OpenStruct def initialize(*) super self.line_items = [] end end

Slide 102

Slide 102 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do before { Object.send(:const_set, :Shipment, ShipmentDouble) } after { Object.send(:remove_const, :Shipment) } it "returns a single in_stock shipment" do shipments = ShipmentBuilder.new([order_line_item_double, order_line_item_double]).build_shipments shipments.length.must_equal 1 shipments.first.shipment_type.must_equal :in_stock end it "executes when consolidate is true" do assert ShipmentBuilder.new([order_line_item_double], true) end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end class ShipmentDouble < OpenStruct def initialize(*) super self.line_items = [] end end

Slide 103

Slide 103 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do before { Object.send(:const_set, :Shipment, ShipmentDouble) } after { Object.send(:remove_const, :Shipment) } it "returns a single in_stock shipment" do shipments = ShipmentBuilder.new([order_line_item_double, order_line_item_double]).build_shipments shipments.length.must_equal 1 shipments.first.shipment_type.must_equal :in_stock end it "returns a single consolidated shipment when consolidate is true" do shipments = ShipmentBuilder.new([order_line_item_double, order_line_item_double], true).build_shipments shipments.length.must_equal 1 shipments.first.shipment_type.must_equal :consolidated end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end class ShipmentDouble < OpenStruct def initialize(*) super self.line_items = [] end end

Slide 104

Slide 104 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 48573 # Running: Finished in 0.001944s, 1028.8066 runs/s, 0.0000 assertions/s. 1) Error: ShipmentBuilder#test_0001_returns a single in_stock shipment: ArgumentError: wrong number of arguments (0 for 2) shipment_builder.rb:16:in `build_shipments' 2) Error: ShipmentBuilder#test_0002_returns a single consolidated shipment when consolidate is true: ArgumentError: wrong number of arguments (0 for 2) shipment_builder.rb:16:in `build_shipments' 2 runs, 0 assertions, 0 failures, 2 errors, 0 skips

Slide 105

Slide 105 text

class ShipmentBuilder # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def initialize(order_line_items, consolidate = false) build_shipments(order_line_items, consolidate) end def build_shipments( order_line_items, consolidate) ...
 return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 106

Slide 106 text

class ShipmentBuilder # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - attr_reader :order_line_items, :consolidate def initialize(order_line_items, consolidate = false) build_shipments(order_line_items, consolidate) end def build_shipments( order_line_items, consolidate) ...
 return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 107

Slide 107 text

class ShipmentBuilder # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - attr_reader :order_line_items, :consolidate def initialize(order_line_items, consolidate = false) @order_line_items = order_line_items @consolidate = consolidate build_shipments(order_line_items, consolidate) end def build_shipments( order_line_items, consolidate) ...
 return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 108

Slide 108 text

class ShipmentBuilder # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - attr_reader :order_line_items, :consolidate def initialize(order_line_items, consolidate = false) @order_line_items = order_line_items @consolidate = consolidate build_shipments(order_line_items, consolidate) end def build_shipments( order_line_items, consolidate) ...
 return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 109

Slide 109 text

class ShipmentBuilder # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - attr_reader :order_line_items, :consolidate def initialize(order_line_items, consolidate = false) @order_line_items = order_line_items @consolidate = consolidate build_shipments(order_line_items, consolidate) end def build_shipments ...
 return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 110

Slide 110 text

class ShipmentBuilder # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - attr_reader :order_line_items, :consolidate def initialize(order_line_items, consolidate = false) @order_line_items = order_line_items @consolidate = consolidate build_shipments(order_line_items, consolidate) end def build_shipments ...
 return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 111

Slide 111 text

class ShipmentBuilder # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - attr_reader :order_line_items, :consolidate def initialize(order_line_items, consolidate = false) @order_line_items = order_line_items @consolidate = consolidate end def build_shipments ...
 return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 112

Slide 112 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 41895 # Running: Finished in 0.001830s, 1092.8962 runs/s, 2185.7923 assertions/s. 2 runs, 4 assertions, 0 failures, 0 errors, 0 skips

Slide 113

Slide 113 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do before { Object.send(:const_set, :Shipment, ShipmentDouble) } after { Object.send(:remove_const, :Shipment) } it "returns a single in_stock shipment" do shipments = ShipmentBuilder.new([order_line_item_double, order_line_item_double]).build_shipments shipments.length.must_equal 1 shipments.first.shipment_type.must_equal :in_stock end it "returns a single consolidated shipment when consolidate is true" do shipments = ShipmentBuilder.new([order_line_item_double, order_line_item_double], true).build_shipments shipments.length.must_equal 1 shipments.first.shipment_type.must_equal :consolidated end end def order_line_item_double OpenStruct.new(:item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock) end class ShipmentDouble < OpenStruct def initialize(*) super self.line_items = [] end end

Slide 114

Slide 114 text

require_relative 'spec_helper' require_relative 'shipment_builder' require 'ostruct' describe ShipmentBuilder do before { Object.send(:const_set, :Shipment, ShipmentDouble) } after { Object.send(:remove_const, :Shipment) } it "returns a single consolidated shipment when consolidate is true" do order_line_items = [ order_line_item_double(ship_status_symbol: :in_stock), order_line_item_double(ship_status_symbol: :drop_ship), order_line_item_double(ship_status_symbol: :order_in), ] shipments = ShipmentBuilder.new(order_line_items, true).build_shipments shipments.length.must_equal 1 end it "returns a single in_stock shipment" do shipments = ShipmentBuilder.new([order_line_item_double, order_line_item_double]).build_shipments shipments.length.must_equal 1 shipments.first.shipment_type.must_equal :in_stock end it "returns multiple shipments when types and vendors differ" do order_line_items = [ order_line_item_double(ship_status_symbol: :in_stock), order_line_item_double(ship_status_symbol: :drop_ship, vendor_id: 9), order_line_item_double(ship_status_symbol: :drop_ship, vendor_id: 10), order_line_item_double(ship_status_symbol: :order_in), ] shipments = ShipmentBuilder.new(order_line_items).build_shipments shipments.length.must_equal 4 shipments.count{|s| s.shipment_type == :drop_ship}.must_equal 2 end it "consolidates drop ship items from the same vendor" do order_line_items = [ order_line_item_double(ship_status_symbol: :drop_ship, vendor_id: 9), order_line_item_double(ship_status_symbol: :drop_ship, vendor_id: 9), ] shipments = ShipmentBuilder.new(order_line_items).build_shipments shipments.length.must_equal 1 end it "consolidates in_stock to drop_ship if all in_stock items can be consolidated" do order_line_items = [ order_line_item_double(ship_status_symbol: :in_stock, vendor_id: 9), order_line_item_double(ship_status_symbol: :drop_ship, vendor_id: 9), ] shipments = ShipmentBuilder.new(order_line_items).build_shipments shipments.length.must_equal 1 shipments.first.shipment_type.must_equal :drop_ship end it "does not consolidate in_stock to drop ship if any in_stock items are not consolidatable" do order_line_items = [ order_line_item_double(ship_status_symbol: :in_stock, vendor_id: 9), order_line_item_double(ship_status_symbol: :drop_ship, vendor_id: 9), order_line_item_double(ship_status_symbol: :in_stock, vendor_id: 10), ] shipments = ShipmentBuilder.new(order_line_items).build_shipments shipments.length.must_equal 2 shipments.find{|s| s.shipment_type == :drop_ship}.line_items.length.must_equal 1 end end def order_line_item_double(overrides = {}) line_item_params = { :item_id => 1, :store_id => 1, :vendor_id => 1, :drop_shippable? => true, :ship_status_symbol => :in_stock }.merge(overrides) OpenStruct.new(line_item_params) end class ShipmentDouble < OpenStruct def initialize(*) super self.line_items = [] end end

Slide 115

Slide 115 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 43898 # Running: Finished in 0.004054s, 1480.0197 runs/s, 2466.6996 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 116

Slide 116 text

Katrina Owen Therapeutic Refactoring

Slide 117

Slide 117 text

class ShipmentBuilder attr_reader :order_line_items, :consolidate def initialize(order_line_items, consolidate = false) @order_line_items = order_line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) found = false shipments.each do |shipment| if shipment.shipment_type == key && shipment.shipper_id == shipper_id && shipment.store_id == store_id shipment.line_items << order_line_item found = true end end if found == false shipment = Shipment.new shipment.shipment_type = key shipment.shipper_id = shipper_id shipment.store_id = store_id shipment.line_items << order_line_item shipments << shipment end end

Slide 118

Slide 118 text

Strunk and White 2.17 Omit Needless Words Vigorous writing is concise. A sentence should contain no unnecessary words, a paragraph no unnecessary sentences, for the same reason that a drawing should have no unnecessary lines and a machine no unnecessary parts.

Slide 119

Slide 119 text

class ShipmentBuilder attr_reader :order_line_items, :consolidate def initialize(order_line_items, consolidate = false) @order_line_items = order_line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) found = false shipments.each do |shipment| if shipment.shipment_type == key && shipment.shipper_id == shipper_id && shipment.store_id == store_id shipment.line_items << order_line_item found = true end end if found == false shipment = Shipment.new shipment.shipment_type = key shipment.shipper_id = shipper_id shipment.store_id = store_id shipment.line_items << order_line_item shipments << shipment end end

Slide 120

Slide 120 text

class ShipmentBuilder attr_reader :order_line_items, :consolidate def initialize(order_line_items, consolidate = false) @order_line_items = order_line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) found = false shipments.each do |shipment| if shipment.shipment_type == key && shipment.shipper_id == shipper_id && shipment.store_id == store_id shipment.line_items << order_line_item found = true end end if found == false shipment = Shipment.new shipment.shipment_type = key shipment.shipper_id = shipper_id shipment.store_id = store_id shipment.line_items << order_line_item shipments << shipment end end

Slide 121

Slide 121 text

line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable, :consolidatable => false) end

Slide 122

Slide 122 text

line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable, :consolidatable => false) end

Slide 123

Slide 123 text

line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable, :consolidatable => order_line_item.consolidatable) end

Slide 124

Slide 124 text

line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable, :consolidatable => order_line_item.consolidatable) end nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 6599 # Running: Finished in 0.004023s, 1491.4243 runs/s, 2485.7072 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 125

Slide 125 text

line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable, :consolidatable => order_line_item.consolidatable) end

Slide 126

Slide 126 text

line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable, :consolidatable => order_line_item.consolidatable) end

Slide 127

Slide 127 text

def build_shipments shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| line_items << OpenStruct.new( :ship_status_symbol => order_line_item.ship_status_symbol, :store_id => order_line_item.store_id, :vendor_id => order_line_item.vendor_id, :line_item => order_line_item, :drop_shippable => order_line_item.drop_shippable :consolidatable => order_line_item.consolidatable) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end

Slide 128

Slide 128 text

def build_shipments shipments = [] unless consolidate line_items = order_line_items # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end

Slide 129

Slide 129 text

def build_shipments shipments = [] unless consolidate line_items = order_line_items # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end

Slide 130

Slide 130 text

def build_shipments shipments = [] unless consolidate line_items = order_line_items # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end

Slide 131

Slide 131 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 61548 # Running: Finished in 0.004479s, 1339.5847 runs/s, 2232.6412 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 132

Slide 132 text

class ShipmentBuilder attr_reader :order_line_items, :consolidate def initialize(order_line_items, consolidate = false) @order_line_items = order_line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate line_items = order_line_items # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 133

Slide 133 text

class ShipmentBuilder attr_reader :order_line_items, :consolidate def initialize(order_line_items, consolidate = false) @order_line_items = order_line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate line_items = order_line_items # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 134

Slide 134 text

class ShipmentBuilder attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate line_items = order_line_items # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 135

Slide 135 text

class ShipmentBuilder attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate line_items = order_line_items # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 136

Slide 136 text

class ShipmentBuilder attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 137

Slide 137 text

class ShipmentBuilder attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li ) end else order_line_items.each do |order_line_item| create_group_if_necessary_and_insert( shipments, :consolidated, order_line_item.store_id, order_line_item.store_id, order_line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 138

Slide 138 text

class ShipmentBuilder attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li ) end else line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 139

Slide 139 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 40759 # Running: Finished in 0.004470s, 1342.2819 runs/s, 2237.1365 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 140

Slide 140 text

class ShipmentBuilder attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li ) end else line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 141

Slide 141 text

Strunk and White 2.13 Make the paragraph the unit of composition [A] subject requires division into topics, each of which should be dealt with in a paragraph. The object of treating each topic in a paragraph by itself is, of course, to aid the reader.

Slide 142

Slide 142 text

class ShipmentBuilder attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li ) end else line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 143

Slide 143 text

class ShipmentBuilder attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li ) end else line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 144

Slide 144 text

class ShipmentBuilder attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li ) end else line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end return shipments end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) ... end end

Slide 145

Slide 145 text

attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li ) end else line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end return shipments end

Slide 146

Slide 146 text

attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li ) end else line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end

Slide 147

Slide 147 text

attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li ) end else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end

Slide 148

Slide 148 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 40759 # Running: Finished in 0.003601s, 1666.2038 runs/s, 2499.3057 assertions/s. 1) Error: ShipmentBuilder#test_0001_returns a single consolidated shipment when consolidate is true: NameError: undefined local variable or method `shipments' for # /Users/nmeans/Projects/krw_talk/shipment_builder.rb:54:in `block in build_consolidated_shipment' 6 runs, 9 assertions, 0 failures, 1 errors, 0 skips

Slide 149

Slide 149 text

attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li ) end else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end

Slide 150

Slide 150 text

attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li ) end else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end

Slide 151

Slide 151 text

attr_reader :line_items, :consolidate def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate end def build_shipments shipments = [] unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li ) end else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end

Slide 152

Slide 152 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li ) end else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end

Slide 153

Slide 153 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 25510 # Running: Finished in 0.003683s, 1629.1067 runs/s, 2715.1778 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 154

Slide 154 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li ) end else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end

Slide 155

Slide 155 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end def build_shipments_by_ship_status # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end

Slide 156

Slide 156 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end def build_shipments_by_ship_status # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end

Slide 157

Slide 157 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 50237 # Running: Finished in 0.003285s, 1826.4840 runs/s, 3044.1400 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 158

Slide 158 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end def build_shipments_by_ship_status # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end

Slide 159

Slide 159 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end def build_shipments_by_ship_status # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end

Slide 160

Slide 160 text

def build_shipments_by_ship_status # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end

Slide 161

Slide 161 text

def build_shipments_by_ship_status # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end

Slide 162

Slide 162 text

def build_shipments_by_ship_status # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end

Slide 163

Slide 163 text

def build_shipments_by_ship_status # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end

Slide 164

Slide 164 text

def build_shipments_by_ship_status # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end

Slide 165

Slide 165 text

def build_shipments_by_ship_status if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def line_items_by_sym line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end line_items_by_sym end

Slide 166

Slide 166 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 5996 # Running: Finished in 0.003557s, 1686.8147 runs/s, 2811.3579 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 167

Slide 167 text

def build_shipments_by_ship_status if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def line_items_by_sym line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end line_items_by_sym end

Slide 168

Slide 168 text

Strunk and White 2.16 Use definite, specific, concrete language Prefer the specific to the general, the definite to the vague, the concrete to the abstract.

Slide 169

Slide 169 text

def build_shipments_by_ship_status if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def line_items_by_sym line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end line_items_by_sym end

Slide 170

Slide 170 text

def build_shipments_by_ship_status if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def line_items_by_sym line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status == ship_sym} end line_items_by_sym end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 171

Slide 171 text

def build_shipments_by_ship_status if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 172

Slide 172 text

def build_shipments_by_ship_status if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 173

Slide 173 text

def build_shipments_by_ship_status if line_items_by_ship_status(:in_stock).length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 174

Slide 174 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 175

Slide 175 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 176

Slide 176 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 177

Slide 177 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 178

Slide 178 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 179

Slide 179 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 180

Slide 180 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 181

Slide 181 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 182

Slide 182 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 3653 # Running: Finished in 0.003564s, 1683.5017 runs/s, 2805.8361 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 183

Slide 183 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 184

Slide 184 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 185

Slide 185 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 186

Slide 186 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status = :drop_ship if li.ship_status == :in_stock} in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 187

Slide 187 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 188

Slide 188 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 189

Slide 189 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 190

Slide 190 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if in_stock_items.count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 191

Slide 191 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. #Woo-hoo! Let's consolidate in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end in_stock_items.count{|li| !li.consolidatable} < 1 end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 192

Slide 192 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? #Woo-hoo! Let's consolidate in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end in_stock_items.count{|li| !li.consolidatable} < 1 end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 193

Slide 193 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 48581 # Running: Finished in 0.004446s, 1349.5277 runs/s, 2249.2128 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 194

Slide 194 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end in_stock_items.count{|li| !li.consolidatable} < 1 end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 195

Slide 195 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end in_stock_items.count{|li| !li.consolidatable} < 1 end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 196

Slide 196 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end in_stock_items.count{|li| !li.consolidatable} < 1 end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 197

Slide 197 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 198

Slide 198 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? in_stock_items.each do |li| matching_ds = drop_ship_items.count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 199

Slide 199 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? in_stock_items.each do |li| matching_ds = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == li.vendor_id} li.consolidatable = (matching_ds && li.drop_shippable) end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 200

Slide 200 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? in_stock_items.each do |li| matching_ds = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == li.vendor_id} li.consolidatable = (matching_ds && li.drop_shippable) end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 201

Slide 201 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? in_stock_items.each do |in_stock_item| matching_ds = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == in_stock_item.vendor_id} in_stock_item.consolidatable = (matching_ds && in_stock_item.drop_shippable) end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 202

Slide 202 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? in_stock_items.each do |in_stock_item| matching_ds = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == in_stock_item.vendor_id} in_stock_item.consolidatable = (matching_ds && in_stock_item.drop_shippable) end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 203

Slide 203 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) in_stock_items.each do |in_stock_item| matching_ds = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == in_stock_item.vendor_id} in_stock_item.consolidatable = (matching_ds && in_stock_item.drop_shippable) end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 204

Slide 204 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) in_stock_items.each do |in_stock_item| matching_ds = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == in_stock_item.vendor_id} in_stock_item.consolidatable = (matching_ds && in_stock_item.drop_shippable) end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 205

Slide 205 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) in_stock_items.each do |in_stock_item| matching_ds = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == in_stock_item.vendor_id} in_stock_item.consolidatable = (matching_ds) end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 206

Slide 206 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) in_stock_items.each do |in_stock_item| matching_ds = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == in_stock_item.vendor_id} in_stock_item.consolidatable = (matching_ds) end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 207

Slide 207 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) in_stock_items.each do |in_stock_item| in_stock_item.consolidatable = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == in_stock_item.vendor_id} end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 208

Slide 208 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) in_stock_items.map(&:vendor_id) in_stock_items.each do |in_stock_item| in_stock_item.consolidatable = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == in_stock_item.vendor_id} end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 209

Slide 209 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id) in_stock_items.each do |in_stock_item| in_stock_item.consolidatable = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == in_stock_item.vendor_id} end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 210

Slide 210 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? in_stock_items.each do |in_stock_item| in_stock_item.consolidatable = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == in_stock_item.vendor_id} end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 211

Slide 211 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? in_stock_items.each do |in_stock_item| in_stock_item.consolidatable = drop_ship_items.any?{|drop_ship_item| drop_ship_item.vendor_id == in_stock_item.vendor_id} end in_stock_items.all?(&:consolidatable) end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 212

Slide 212 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 213

Slide 213 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 7067 # Running: Finished in 0.003385s, 1772.5258 runs/s, 2954.2097 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 214

Slide 214 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 215

Slide 215 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 216

Slide 216 text

def build_shipments_by_ship_status if in_stock_items.length > 0 && drop_ship_items.length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 217

Slide 217 text

def build_shipments_by_ship_status # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 218

Slide 218 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 26769 # Running: Finished in 0.003151s, 1904.1574 runs/s, 3173.5957 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 219

Slide 219 text

def build_shipments_by_ship_status if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 220

Slide 220 text

def build_shipments_by_ship_status if consolidate_to_drop_ships? in_stock_items.each{|li| li.ship_status = :drop_ship} end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 221

Slide 221 text

def build_shipments_by_ship_status if consolidate_to_drop_ships? end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 222

Slide 222 text

def build_shipments_by_ship_status if consolidate_to_drop_ships? end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 223

Slide 223 text

def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 224

Slide 224 text

def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end

Slide 225

Slide 225 text

def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end def assign_items_to_shipments line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end

Slide 226

Slide 226 text

def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end def assign_items_to_shipments line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end

Slide 227

Slide 227 text

def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end def assign_items_to_shipments line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end

Slide 228

Slide 228 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 46563 # Running: Finished in 0.003477s, 1725.6255 runs/s, 2876.0426 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 229

Slide 229 text

class ShipmentBuilder # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end def assign_items_to_shipments line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) found = false shipments.each do |shipment| if shipment.shipment_type == key && shipment.shipper_id == shipper_id && shipment.store_id == store_id shipment.line_items << order_line_item found = true end end if found == false shipment = Shipment.new shipment.shipment_type = key shipment.shipper_id = shipper_id shipment.store_id = store_id shipment.line_items << order_line_item shipments << shipment end end end

Slide 230

Slide 230 text

class ShipmentBuilder # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Determine the groups in which items will be shipped. This is necessary for # non-consolidated shipments when some items are out of stock or are being # drop-shipped from a vendors warehouse. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end def assign_items_to_shipments line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) found = false shipments.each do |shipment| if shipment.shipment_type == key && shipment.shipper_id == shipper_id && shipment.store_id == store_id shipment.line_items << order_line_item found = true end end if found == false shipment = Shipment.new shipment.shipment_type = key shipment.shipper_id = shipper_id shipment.store_id = store_id shipment.line_items << order_line_item shipments << shipment end end end

Slide 231

Slide 231 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 232

Slide 232 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 233

Slide 233 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 234

Slide 234 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 235

Slide 235 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 236

Slide 236 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| create_group_if_necessary_and_insert( shipments, :consolidated, line_item.store_id, line_item.store_id, line_item) end end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 237

Slide 237 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| line_item.ship_status = :consolidated end end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 238

Slide 238 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| line_item.ship_status = :consolidated end assign_items_to_shipments end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 239

Slide 239 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment line_items.each do |line_item| line_item.ship_status = :consolidated end assign_items_to_shipments end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 240

Slide 240 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 241

Slide 241 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment consolidate_to_single_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 242

Slide 242 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment consolidate_to_single_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 243

Slide 243 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment consolidate_to_single_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 244

Slide 244 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment consolidate_to_single_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 245

Slide 245 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment consolidate_to_single_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 246

Slide 246 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment consolidate_to_single_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 247

Slide 247 text

attr_reader :line_items, :consolidate, :shipments def initialize(line_items, consolidate = false) @line_items = line_items @consolidate = consolidate @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment consolidate_to_single_shipment if consolidate assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 248

Slide 248 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 249

Slide 249 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments unless consolidate build_shipments_by_ship_status else build_consolidated_shipment end return shipments end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 250

Slide 250 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments build_shipments_by_ship_status build_consolidated_shipment return shipments end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 251

Slide 251 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 46563 # Running: Finished in 0.014991s, 400.2401 runs/s, 600.3602 assertions/s. 1) Failure: ShipmentBuilder#test_0006_does not consolidate in_stock to drop_ship if any in_stock items are not consolidatable [shipment_builder_spec.rb:65]: Expected: 1 Actual: 2 2) Failure: ShipmentBuilder#test_0005_consolidates in_stock to drop_ship if all in_stock items can be consolidated [shipment_builder_spec.rb:53]: Expected: 1 Actual: 2 6 runs, 9 assertions, 2 failures, 0 errors, 0 skips

Slide 252

Slide 252 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments build_shipments_by_ship_status build_consolidated_shipment return shipments end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 253

Slide 253 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments build_shipments_by_ship_status build_consolidated_shipment return shipments end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 254

Slide 254 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments build_shipments_by_ship_status build_consolidated_shipment return shipments end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment assign_items_to_shipments end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? assign_items_to_shipments end

Slide 255

Slide 255 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments build_shipments_by_ship_status build_consolidated_shipment assign_items_to_shipments return shipments end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? end

Slide 256

Slide 256 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments build_shipments_by_ship_status build_consolidated_shipment assign_items_to_shipments return shipments end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? end

Slide 257

Slide 257 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 25830 # Running: Finished in 0.003653s, 1642.4856 runs/s, 2737.4760 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 258

Slide 258 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments build_shipments_by_ship_status build_consolidated_shipment assign_items_to_shipments return shipments end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? end

Slide 259

Slide 259 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments build_shipments_by_ship_status build_consolidated_shipment assign_items_to_shipments return shipments end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? end

Slide 260

Slide 260 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments assign_items_to_shipments return shipments end def optimize_consolidation build_consolidated_shipment build_shipments_by_ship_status end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? end

Slide 261

Slide 261 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments optimize_consolidation assign_items_to_shipments return shipments end def optimize_consolidation build_consolidated_shipment build_shipments_by_ship_status end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? end

Slide 262

Slide 262 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments optimize_consolidation assign_items_to_shipments return shipments end def optimize_consolidation build_consolidated_shipment build_shipments_by_ship_status end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? end

Slide 263

Slide 263 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments optimize_consolidation assign_items_to_shipments return shipments end def optimize_consolidation build_consolidated_shipment build_shipments_by_ship_status end def build_consolidated_shipment consolidate_to_single_shipment if single_shipment end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def build_shipments_by_ship_status consolidate_to_drop_ships if consolidate_to_drop_ships? end

Slide 264

Slide 264 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments optimize_consolidation assign_items_to_shipments return shipments end def optimize_consolidation consolidate_to_single_shipment if single_shipment consolidate_to_drop_ships if consolidate_to_drop_ships? end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end

Slide 265

Slide 265 text

attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments optimize_consolidation assign_items_to_shipments return shipments end def optimize_consolidation consolidate_to_single_shipment if single_shipment consolidate_to_drop_ships if consolidate_to_drop_ships? end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end

Slide 266

Slide 266 text

nmeans@nicksair% ruby shipment_builder_spec.rb Run options: --seed 60718 # Running: Finished in 0.003564s, 1683.5017 runs/s, 2805.8361 assertions/s. 6 runs, 10 assertions, 0 failures, 0 errors, 0 skips

Slide 267

Slide 267 text

class ShipmentBuilder attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments optimize_consolidation assign_items_to_shipments return shipments end def optimize_consolidation consolidate_to_single_shipment if single_shipment consolidate_to_drop_ships if consolidate_to_drop_ships? end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end def assign_items_to_shipments line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) found = false shipments.each do |shipment| if shipment.shipment_type == key && shipment.shipper_id == shipper_id && shipment.store_id == store_id shipment.line_items << order_line_item found = true end end if found == false shipment = Shipment.new shipment.shipment_type = key shipment.shipper_id = shipper_id shipment.store_id = store_id shipment.line_items << order_line_item shipments << shipment end end end

Slide 268

Slide 268 text

class ShipmentBuilder attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments optimize_consolidation assign_items_to_shipments return shipments end def optimize_consolidation consolidate_to_single_shipment if single_shipment consolidate_to_drop_ships if consolidate_to_drop_ships? end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end def assign_items_to_shipments line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) found = false shipments.each do |shipment| if shipment.shipment_type == key && shipment.shipper_id == shipper_id && shipment.store_id == store_id shipment.line_items << order_line_item found = true end end if found == false shipment = Shipment.new shipment.shipment_type = key shipment.shipper_id = shipper_id shipment.store_id = store_id shipment.line_items << order_line_item shipments << shipment end end end

Slide 269

Slide 269 text

class ShipmentBuilder attr_reader :line_items, :single_shipment, :shipments def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] end def build_shipments optimize_consolidation assign_items_to_shipments return shipments end def optimize_consolidation consolidate_to_single_shipment if single_shipment consolidate_to_drop_ships if consolidate_to_drop_ships? end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end def assign_items_to_shipments line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status, li.store_id, li.ship_status == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end end def create_group_if_necessary_and_insert( shipments, key, store_id, shipper_id, order_line_item) found = false shipments.each do |shipment| if shipment.shipment_type == key && shipment.shipper_id == shipper_id && shipment.store_id == store_id shipment.line_items << order_line_item found = true end end if found == false shipment = Shipment.new shipment.shipment_type = key shipment.shipper_id = shipper_id shipment.store_id = store_id shipment.line_items << order_line_item shipments << shipment end end end

Slide 270

Slide 270 text

class ShipmentBuilder attr_reader :line_items, :single_shipment, :shipments, :shipment_list def initialize(line_items, single_shipment = false) @line_items = line_items @single_shipment = single_shipment @shipments = [] @shipment_list = ShipmentList.new(shipments) end def build_shipments optimize_consolidation assign_items_to_shipments return shipments end def optimize_consolidation consolidate_to_single_shipment if single_shipment consolidate_to_drop_ships if consolidate_to_drop_ships? end def consolidate_to_single_shipment line_items.each { |li| li.ship_status = :consolidated } end def consolidate_to_drop_ships in_stock_items.each{|li| li.ship_status = :drop_ship} end def consolidate_to_drop_ships? return false unless in_stock_items.all?(&:drop_shippable) (in_stock_items.map(&:vendor_id) - drop_ship_items.map(&:vendor_id)).empty? end def in_stock_items line_items_by_ship_status(:in_stock) end def drop_ship_items line_items_by_ship_status(:drop_ship) end def line_items_by_ship_status(status) line_items.select{|li| li.ship_status == status} end def assign_items_to_shipments line_items.each{|li| shipment_list.assign_to_shipment(li)} end class ShipmentList attr_accessor :shipments def initialize(shipments) @shipments = shipments end def assign_to_shipment(line_item) shipment = find_or_create_shipment(line_item.ship_status, line_item.vendor_id) shipment.line_items << line_item end def find_or_create_shipment(type, shipper_id) find_shipment(type, shipper_id) || create_shipment(type, shipper_id) end def find_shipment(type, shipper_id) shipments.find do |shipment| shipment.shipment_type == type && (shipment.shipper_id == shipper_id || shipment.shipment_type != :drop_ship) end end def create_shipment(type, shipper_id) shipments << new_shipment = Shipment.new(shipment_type: type, shipper_id: shipper_id, store_id: 1) new_shipment end end end

Slide 271

Slide 271 text

So, How’d We Do? Original Refactored 3

Slide 272

Slide 272 text

So, How’d We Do? Original Refactored Flay Score for Class 3

Slide 273

Slide 273 text

So, How’d We Do? Original Refactored Flay Score for Class 36 3

Slide 274

Slide 274 text

So, How’d We Do? Original Refactored Flay Score for Class 36 0 3

Slide 275

Slide 275 text

So, How’d We Do? Original Refactored Flay Score for Class 36 0 Flog Score for Class 3

Slide 276

Slide 276 text

So, How’d We Do? Original Refactored Flay Score for Class 36 0 Flog Score for Class 180.4 3

Slide 277

Slide 277 text

So, How’d We Do? Original Refactored Flay Score for Class 36 0 Flog Score for Class 180.4 77.2 3

Slide 278

Slide 278 text

So, How’d We Do? Original Refactored Flay Score for Class 36 0 Flog Score for Class 180.4 77.2 Flog Score for #build_shipments 3

Slide 279

Slide 279 text

So, How’d We Do? Original Refactored Flay Score for Class 36 0 Flog Score for Class 180.4 77.2 Flog Score for #build_shipments 120.7 3

Slide 280

Slide 280 text

So, How’d We Do? Original Refactored Flay Score for Class 36 0 Flog Score for Class 180.4 77.2 Flog Score for #build_shipments 120.7 3.0 3

Slide 281

Slide 281 text

def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end return shipments end

Slide 282

Slide 282 text

def build_shipments( order_line_items, consolidate=false ) shipments = [] unless consolidate # Build a hash of line items with associated data we can use without having to re-query the database for all the iterations. line_items = [] order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) ship_status_symbol = item.shipping_status_symbol_for_quantity(order_line_item.quantity) ship_status_symbol = :in_stock if [:closeout_instock,:from_stock_only_instock].include?(ship_status_symbol) line_items << OpenStruct.new( :ship_status_symbol => ship_status_symbol, :store_id => item.instance_of?(Item) ? item.product.store_id : 1, :vendor_id => item.instance_of?(Item) ? item.product.vendor_id : nil, :line_item => order_line_item, :drop_shippable => item.drop_shippable?, :consolidatable => false) end # MAGIC GOES HERE TO DO THE CONSOLIDATION line_items_by_sym = {} [:in_stock,:drop_ship,:order_in].each do |ship_sym| line_items_by_sym[ship_sym] = line_items.select{|li| li.ship_status_symbol == ship_sym} end if line_items_by_sym[:in_stock].length > 0 && line_items_by_sym[:drop_ship].length > 0 # If we've got us some in_stocks and some drop_ships, let's see if # we can do some consolidating. It only makes sense to consolidate if we can completely # get rid of in_stocks. line_items_by_sym[:in_stock].each do |li| matching_ds = line_items_by_sym[:drop_ship].count{|dsli| dsli.vendor_id == li.vendor_id} > 0 li.consolidatable = (matching_ds && li.drop_shippable) end if line_items_by_sym[:in_stock].count{|li| !li.consolidatable} < 1 #Woo-hoo! Let's consolidate line_items.each{|li| li.ship_status_symbol = :drop_ship if li.ship_status_symbol == :in_stock} end end line_items.each do |li| create_group_if_necessary_and_insert( shipments, li.ship_status_symbol, li.store_id, li.ship_status_symbol == :drop_ship ? li.vendor_id : li.store_id, li.line_item ) end else order_line_items.each do |order_line_item| item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id) create_group_if_necessary_and_insert( shipments, :consolidated, item.store_id, item.store_id, order_line_item) end end return shipments end def build_shipments optimize_consolidation assign_items_to_shipments return shipments end "

Slide 283

Slide 283 text

Why did it work? V

Slide 284

Slide 284 text

5.5 Revise and Rewrite O

Slide 285

Slide 285 text

5.5 Revise and Rewrite O Red, Green, Refactor

Slide 286

Slide 286 text

5.3 Work from a suitable design O

Slide 287

Slide 287 text

5.3 Work from a suitable design O Gang of Four POODR etc. ad infinitum

Slide 288

Slide 288 text

5.19 Do not take shortcuts at the cost of clarity O

Slide 289

Slide 289 text

5.19 Do not take shortcuts at the cost of clarity O Law of Demeter

Slide 290

Slide 290 text

5.19 Do not take shortcuts at the cost of clarity O “Law” of Demeter

Slide 291

Slide 291 text

2.13 Make the paragraph the unit of composition O

Slide 292

Slide 292 text

2.13 Make the paragraph the unit of composition O Single Responsibility Principle

Slide 293

Slide 293 text

“Any fool can write code a computer can understand. Good programmers write code humans can understand.” – Martin Fowler Refactoring: Improve the Design of Existing Code

Slide 294

Slide 294 text

We stopped along the road for a bite to eat. The cowboy went off to have a spare tire patched, and Eddie and I sat down in a kind of homemade diner. I heard a great laugh, the greatest laugh in the world, and here came this rawhide oldtimer Nebraska farmer with a bunch of other boys into the diner; you could hear his raspy cries clear across the plains, across the whole gray world of them that day. Everybody else laughed with him. He didn’t have a care in the world and had the hugest regard for everybody. I said to myself, Wham, listen to that man laugh. That’s the West, here I am in the West.

Slide 295

Slide 295 text

Uncle Charles is saying that though he can anticipate that the Deans might be predisposed to weigh what he avers as coming from his possible appearance as a kind of cheerleader for E.T.A., he can assure the assembled Deans that all this is true, and that the Academy has presently in residence no fewer than a third of the continent’s top thirty junior, in age brackets all across the board, and that I here, who go by ‘Hal,’ usually, am ‘right up there among the very cream.’

Slide 296

Slide 296 text

He was a long time going to sleep. After a while he turned and looked at the man. His face in the small light streaked with black from the rain like some old world thespian. Can I ask you something? he said. Yes. Of course. Are we going to die? Sometime. Not now. And we’re still going south. Yes. So we’ll be warm. Yes. Okay. Okay what? Nothing. Just okay. Go to sleep. Okay. I’m going to blow out the lamp. Is that okay? Yes. Thats okay. And then later in the darkness: Can I ask you something? Yes. Of course you can. What would you do if I died? If you died I would want to die too. So you could be with me? Yes. So I could be with you. Okay.

Slide 297

Slide 297 text

“I felt uneasy posing as an expert on rhetoric when the truth is I write by ear, always with difficulty and seldom with any exact notion of what is taking place under the hood.” – E. B. White Introduction to “Will Strunk”, 1957

Slide 298

Slide 298 text

Thanks! V Twitter: @nmeans Blog: http://nickol.as/ Code: https://github.com/nmeans/code_like_a_writer