Class: Workarea::Fulfillment::Package

Inherits:
Object
  • Object
show all
Defined in:
app/models/workarea/fulfillment/package.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracking_number, events = []) ⇒ Package

Returns a new instance of Package.



22
23
24
25
# File 'app/models/workarea/fulfillment/package.rb', line 22

def initialize(tracking_number, events = [])
  @tracking_number = tracking_number
  @events = events
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



20
21
22
# File 'app/models/workarea/fulfillment/package.rb', line 20

def events
  @events
end

#tracking_numberObject (readonly)

Returns the value of attribute tracking_number.



20
21
22
# File 'app/models/workarea/fulfillment/package.rb', line 20

def tracking_number
  @tracking_number
end

Class Method Details

.create(events) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/workarea/fulfillment/package.rb', line 4

def self.create(events)
  tracking_numbers = events
                      .select { |e| e.status == 'shipped' }
                      .map { |e| e.data['tracking_number'] }
                      .uniq

  tracking_numbers.map do |tracking_number|
    matching_events = events.select do |event|
      event.status == 'shipped' &&
        event.data['tracking_number'] == tracking_number
    end

    Package.new(tracking_number, matching_events)
  end
end

Instance Method Details

#created_atObject



31
32
33
# File 'app/models/workarea/fulfillment/package.rb', line 31

def created_at
  sorted_event_creation_dates.first
end

#events_by_itemObject



27
28
29
# File 'app/models/workarea/fulfillment/package.rb', line 27

def events_by_item
  @events.group_by(&:order_item_id)
end

#updated_atObject



35
36
37
# File 'app/models/workarea/fulfillment/package.rb', line 35

def updated_at
  sorted_event_creation_dates.last
end