Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How To Code Like A Writer

How To Code Like A Writer

Talk on refactoring Ruby following the rules of Strunk and White's "Elements of Style" given at Keep Ruby Weird 2014.

D24879ba8b9392f8873081ecc0532f7a?s=128

Nickolas Means

October 24, 2014
Tweet

Transcript

  1. 3 How to Code Like a Writer Nickolas Means

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

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

  4. I build stuff at to help fix healthcare.

  5. None
  6. “If we're not engineers, what are we then? We're software

    writers!” – David Heinemer Hansson 2014 Railsconf Keynote
  7. None
  8. None
  9. None
  10. None
  11. None
  12. None
  13. And now, some code.

  14. Keep Ruby Weird, meet shipping_utilities.rb

  15. 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
  16. None
  17. 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
  18. 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
  19. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  20. None
  21. “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
  22. What, then, shall we do? V

  23. 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.
  24. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  25. 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.
  26. In Stock! Vendor Foo Shopping Cart

  27. In Stock! Vendor Foo Drop Ship Vendor Foo Shopping Cart

  28. In Stock! Vendor Foo Drop Ship Vendor Foo Drop Ship

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

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

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

    Drop Ship Vendor Bar
  32. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  33. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  34. 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
  35. 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
  36. 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
  37. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  38. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  39. 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
  40. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  41. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  42. item = order_line_item.item_id ? Item.find(order_line_item.item_id, :include => :product) : UsedItem.find(order_line_item.used_item_id)

  43. 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.
  44. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  45. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  46. 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
  47. 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
  48. 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
  49. 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
  50. 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
  51. 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
  52. 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
  53. 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
  54. 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
  55. 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
  56. 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
  57. 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
  58. 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
  59. 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
  60. 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
  61. 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
  62. 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
  63. 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.
  64. 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
  65. 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
  66. 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
  67. 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
  68. 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
  69. 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
  70. 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
  71. 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
  72. 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
  73. 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
  74. 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
  75. 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
  76. 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
  77. 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
  78. 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
  79. 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
  80. 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
  81. 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
  82. 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
  83. 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
  84. 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
  85. 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
  86. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  87. 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
  88. 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
  89. 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
  90. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  91. 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
  92. 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
  93. 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
  94. 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
  95. 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
  96. # - - - - - - - - -

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 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
  97. 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
  98. 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 ...>]
  99. 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
  100. 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
  101. 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
  102. 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
  103. 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
  104. 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
  105. 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
  106. 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
  107. 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
  108. 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
  109. 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
  110. 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
  111. 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
  112. 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
  113. 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
  114. 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
  115. 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
  116. Katrina Owen Therapeutic Refactoring

  117. 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
  118. 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.
  119. 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
  120. 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
  121. 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
  122. 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
  123. 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
  124. 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
  125. 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
  126. 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
  127. 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
  128. 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
  129. 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
  130. 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
  131. 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
  132. 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
  133. 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
  134. 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
  135. 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
  136. 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
  137. 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
  138. 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
  139. 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
  140. 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
  141. 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.
  142. 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
  143. 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
  144. 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
  145. 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
  146. 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
  147. 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
  148. 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 #<ShipmentBuilder:0x007fa621902da0> /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
  149. 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
  150. 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
  151. 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
  152. 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
  153. 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
  154. 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
  155. 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
  156. 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
  157. 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
  158. 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
  159. 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
  160. 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
  161. 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
  162. 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
  163. 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
  164. 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
  165. 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
  166. 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
  167. 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
  168. 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.
  169. 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
  170. 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
  171. 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
  172. 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
  173. 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
  174. 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
  175. 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
  176. 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
  177. 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
  178. 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
  179. 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
  180. 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
  181. 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
  182. 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
  183. 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
  184. 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
  185. 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
  186. 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
  187. 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
  188. 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
  189. 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
  190. 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
  191. 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
  192. 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
  193. 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
  194. 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
  195. 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
  196. 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
  197. 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
  198. 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
  199. 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
  200. 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
  201. 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
  202. 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
  203. 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
  204. 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
  205. 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
  206. 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
  207. 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
  208. 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
  209. 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
  210. 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
  211. 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
  212. 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
  213. 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
  214. 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
  215. 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
  216. 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
  217. 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
  218. 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
  219. 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
  220. 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
  221. 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
  222. 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
  223. 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
  224. 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
  225. 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
  226. 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
  227. 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
  228. 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
  229. 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
  230. 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
  231. 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
  232. 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
  233. 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
  234. 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
  235. 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
  236. 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
  237. 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
  238. 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
  239. 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
  240. 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
  241. 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
  242. 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
  243. 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
  244. 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
  245. 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
  246. 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
  247. 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
  248. 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
  249. 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
  250. 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
  251. 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
  252. 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
  253. 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
  254. 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
  255. 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
  256. 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
  257. 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
  258. 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
  259. 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
  260. 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
  261. 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
  262. 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
  263. 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
  264. 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
  265. 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
  266. 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
  267. 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
  268. 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
  269. 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
  270. 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
  271. So, How’d We Do? Original Refactored 3

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

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

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

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

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

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

    36 0 Flog Score for Class 180.4 77.2 3
  278. 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
  279. 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
  280. 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
  281. 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
  282. 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 "
  283. Why did it work? V

  284. 5.5 Revise and Rewrite O

  285. 5.5 Revise and Rewrite O Red, Green, Refactor

  286. 5.3 Work from a suitable design O

  287. 5.3 Work from a suitable design O Gang of Four

    POODR etc. ad infinitum
  288. 5.19 Do not take shortcuts at the cost of clarity

    O
  289. 5.19 Do not take shortcuts at the cost of clarity

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

    O “Law” of Demeter
  291. 2.13 Make the paragraph the unit of composition O

  292. 2.13 Make the paragraph the unit of composition O Single

    Responsibility Principle
  293. “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
  294. 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.
  295. 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.’
  296. 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.
  297. “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
  298. Thanks! V Twitter: @nmeans Blog: http://nickol.as/ Code: https://github.com/nmeans/code_like_a_writer