Class: SpreeCmCommissioner::TripResult

Inherits:
Object
  • Object
show all
Defined in:
lib/spree_cm_commissioner/trip_result.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TripResult

Returns a new instance of TripResult.



9
10
11
12
13
# File 'lib/spree_cm_commissioner/trip_result.rb', line 9

def initialize(options = {})
  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#allow_seat_selectionObject

Returns the value of attribute allow_seat_selection.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def allow_seat_selection
  @allow_seat_selection
end

#amenitiesObject

Returns the value of attribute amenities.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def amenities
  @amenities
end

#boardingObject

Returns the value of attribute boarding.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def boarding
  @boarding
end

#compare_at_amountObject

Returns the value of attribute compare_at_amount.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def compare_at_amount
  @compare_at_amount
end

#currencyObject

Returns the value of attribute currency.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def currency
  @currency
end

#departure_timeObject

Returns the value of attribute departure_time.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def departure_time
  @departure_time
end

#destination_placeObject

Returns the value of attribute destination_place.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def destination_place
  @destination_place
end

#distanceObject

Returns the value of attribute distance.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def distance
  @distance
end

#drop_offObject

Returns the value of attribute drop_off.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def drop_off
  @drop_off
end

#durationObject

Returns the value of attribute duration.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def duration
  @duration
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def id
  @id
end

#max_capacityObject

Returns the value of attribute max_capacity.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def max_capacity
  @max_capacity
end

#offset_daysObject

Returns the value of attribute offset_days.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def offset_days
  @offset_days
end

#open_dated_productObject

Returns the value of attribute open_dated_product.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def open_dated_product
  @open_dated_product
end

#origin_placeObject

Returns the value of attribute origin_place.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def origin_place
  @origin_place
end

#priceObject

Returns the value of attribute price.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def price
  @price
end

#product_idObject

Returns the value of attribute product_id.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def product_id
  @product_id
end

#quantity_availableObject

Returns the value of attribute quantity_available.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def quantity_available
  @quantity_available
end

#route_typeObject

Returns the value of attribute route_type.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def route_type
  @route_type
end

#sequenceObject

Returns the value of attribute sequence.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def sequence
  @sequence
end

#vehicle_typeObject

Returns the value of attribute vehicle_type.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def vehicle_type
  @vehicle_type
end

#vehicle_type_idObject

Returns the value of attribute vehicle_type_id.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def vehicle_type_id
  @vehicle_type_id
end

#vendorObject

Returns the value of attribute vendor.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def vendor
  @vendor
end

#vendor_idObject

Returns the value of attribute vendor_id.



3
4
5
# File 'lib/spree_cm_commissioner/trip_result.rb', line 3

def vendor_id
  @vendor_id
end

Class Method Details

.from_trip(trip) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/spree_cm_commissioner/trip_result.rb', line 15

def self.from_trip(trip)
  return nil if trip.nil?

  vehicle_type = trip&.vehicle_type
  vendor = trip&.vendor
  new(
    id: trip&.id,
    departure_time: trip&.departure_time,
    duration: trip&.duration,
    distance: trip&.distance,
    allow_seat_selection: trip&.allow_seat_selection,
    route_type: trip&.route_type,
    origin_place: {
      id: trip&.origin_place_id,
      name: trip&.origin_place_name
    },
    destination_place: {
      id: trip&.destination_place_id,
      name: trip&.destination_place_name
    },
    vehicle_type_id: trip&.vehicle_type_id,
    vehicle_type: vehicle_type,
    vendor_id: trip&.vendor_id,
    vendor: vendor,
    product_id: trip&.product_id,
    price: trip&.amount,
    currency: trip&.currency,
    compare_at_amount: trip&.compare_at_amount,
    offset_days: trip.try(:offset_days) || 0,
    boarding: build_boarding_info(trip),
    drop_off: build_drop_off_info(trip),
    quantity_available: trip&.quantity_available,
    max_capacity: trip&.max_capacity,
    amenities: (trip.vehicle_type&.option_values || []),
    open_dated_product: trip&.open_dated_product
  )
end

Instance Method Details

#amenity_idsObject



59
60
61
# File 'lib/spree_cm_commissioner/trip_result.rb', line 59

def amenity_ids
  amenities&.map(&:id) || []
end

#arrival_timeObject



73
74
75
76
77
# File 'lib/spree_cm_commissioner/trip_result.rb', line 73

def arrival_time
  return nil if departure_time.nil? || duration_with_offset.nil?

  departure_time + duration_with_offset.seconds
end

#cache_key_with_versionObject

Enable serializer-level caching (like ActiveRecord’s cache_key_with_version) Create a version hash based on attributes that matter for caching



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/spree_cm_commissioner/trip_result.rb', line 93

def cache_key_with_version
  version_parts = [
    id,
    price,
    quantity_available,
    max_capacity,
    departure_time&.to_i,
    open_dated_product&.id
  ].compact.join('-')

  version_hash = Digest::MD5.hexdigest(version_parts)
  "trip_results/#{id}-#{version_hash}"
end

#display_priceObject



85
86
87
88
89
# File 'lib/spree_cm_commissioner/trip_result.rb', line 85

def display_price
  return nil if price.nil? || currency.nil?

  Spree::Money.new(price, currency: currency).to_s
end

#duration_in_hmsObject



63
64
65
66
67
68
69
70
71
# File 'lib/spree_cm_commissioner/trip_result.rb', line 63

def duration_in_hms
  total_seconds = duration_with_offset
  return { hours: 0, minutes: 0, seconds: 0 } if total_seconds.nil?

  hours = total_seconds / 3600
  minutes = (total_seconds % 3600) / 60
  seconds = total_seconds % 60
  { hours: hours, minutes: minutes, seconds: seconds }
end

#duration_with_offsetObject



79
80
81
82
83
# File 'lib/spree_cm_commissioner/trip_result.rb', line 79

def duration_with_offset
  return nil if duration.nil?

  duration + offset_days.to_i.days
end

#open_dated_pair_idObject



107
108
109
# File 'lib/spree_cm_commissioner/trip_result.rb', line 107

def open_dated_pair_id
  open_dated_product&.id
end

#total_soldObject



53
54
55
56
57
# File 'lib/spree_cm_commissioner/trip_result.rb', line 53

def total_sold
  return if max_capacity.nil? || quantity_available.nil?

  max_capacity - quantity_available
end