Class: GitFit::Dedup::FieldElector

Inherits:
Object
  • Object
show all
Defined in:
lib/git_fit/dedup/field_elector.rb

Constant Summary collapse

FIELD_ELECTION_RULES =
{
  distance: :median,
  moving_time: :min,
  elapsed_time: :max,
  average_heartrate: :mean,
  max_heartrate: :max,
  average_power: :non_null,
  max_power: :non_null,
  average_cadence: :non_null,
  max_cadence: :non_null,
  summary_polyline: :best_polyline,
  elevation_gain: :median,
  calories: :median,
  average_temperature: :non_null,
  average_speed: :computed,
  name: :best_name,
  start_date: :earliest,
}.freeze
SOURCE_NAME_PRIORITY =
{
  garmin: 1, strava: 2, igpsport: 3, xoss: 4,
  keep: 5, xingzhe: 6, apple_health: 7
}.freeze

Instance Method Summary collapse

Instance Method Details

#elect(field, values, sources = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/git_fit/dedup/field_elector.rb', line 32

def elect(field, values, sources = [])
  rule = FIELD_ELECTION_RULES[field]
  non_nil = values.compact
  return nil if non_nil.empty?

  case rule
  when :median
    sorted = non_nil.sort
    sorted[sorted.size / 2]
  when :mean
    non_nil.sum / non_nil.size
  when :min
    non_nil.min
  when :max
    non_nil.max
  when :non_null
    non_nil.first
  when :best_polyline
    candidates = non_nil.select { |v| v.is_a?(String) && !v.empty? }
    return nil if candidates.empty?
    candidates.max_by { |v| decode_polyline_size(v) }
  when :computed
    nil
  when :best_name
    best_name(values, sources)
  when :earliest
    non_nil.min_by do |v|
      Time.parse(v.to_s)
    rescue StandardError
      Time.at(0)
    end
  else
    non_nil.first
  end
end