Method: PostRunner::FitFileStore#find

Defined in:
lib/postrunner/FitFileStore.rb

#find(query) ⇒ Object

Find a specific subset of the activities based on their index.

Parameters:

  • query (String)


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/postrunner/FitFileStore.rb', line 282

def find(query)
  case query
  when /\A-?\d+$\z/
    index = query.to_i
    # The UI counts the activities from 1 to N. Ruby counts from 0 -
    # (N-1).
    if index <= 0
      Log.error 'Index must be larger than 0'
      return []
    end
    # The UI counts the activities from 1 to N. Ruby counts from 0 -
    # (N-1).
    if (a = activities[index - 1])
      return [ a ]
    end
  when /\A-?\d+--?\d+\z/
    idxs = query.match(/(?<sidx>-?\d+)-(?<eidx>-?[0-9]+)/)
    if (sidx = idxs['sidx'].to_i) <= 0
      Log.error 'Start index must be larger than 0'
      return []
    end
    if (eidx = idxs['eidx'].to_i) <= 0
      Log.error 'End index must be larger than 0'
      return []
    end
    if eidx < sidx
      Log.error 'Start index must be smaller than end index'
      return []
    end
    # The UI counts the activities from 1 to N. Ruby counts from 0 -
    # (N-1).
    unless (as = activities[(sidx - 1)..(eidx - 1)]).empty?
      return as
    end
  else
    Log.error "Invalid activity query: #{query}"
  end

  []
end