Class: CoffeeOutside::LocationChooser

Inherits:
Object
  • Object
show all
Defined in:
lib/coffeeoutside/locations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(forecast, destructive: false) ⇒ LocationChooser

Returns a new instance of LocationChooser.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/coffeeoutside/locations.rb', line 88

def initialize(forecast, destructive: false)
  @location = nil
  of = OverrideFile.new
  plf = PriorLocationsFile.new

  # First check override file
  if of.override?
    @location = of.location
    of.delete_file if destructive
  else
    # If no override location, determine one
    locations = LocationFile.new.locations

    # Remove paused locations
    locations.delete_if(&:paused?)

    # Delete locations that don't meet forecast criteria
    locations.keep_if { |l| l.weather_appropriate? forecast }

    # Raise if no locations remaining
    raise "No locations remaining!" if locations.empty?

    # Remove previously selected locations
    prior_locations = plf.previous_locations.dup
    while !prior_locations.empty? && locations.count > 1
      pl = prior_locations.pop(locations.count - 1)
      locations.delete_if { |l| pl.include? l.name }
    end

    # Pick random location if more than one remaining
    @location = locations.sample
  end

  # Append to prior locations list
  plf.append_location @location if destructive

  @location # rubocop:disable Lint/Void
end

Instance Attribute Details

#location ⇒ Object (readonly)

Returns the value of attribute location.



86
87
88
# File 'lib/coffeeoutside/locations.rb', line 86

def location
  @location
end