Class: PostRunner::Activity

Inherits:
Object
  • Object
show all
Defined in:
lib/postrunner/Activity.rb

Constant Summary collapse

@@CachedVariables =

This is a list of variables that provide data from the fit file. To speed up access to it, we cache the data in the activity database.

%w( timestamp total_distance total_timer_time
avg_speed )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, fit_file, fit_activity, name = nil) ⇒ Activity

Returns a new instance of Activity.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/postrunner/Activity.rb', line 29

def initialize(db, fit_file, fit_activity, name = nil)
  @fit_file = fit_file
  @fit_activity = fit_activity
  @name = name || fit_file
  late_init(db)

  @@CachedVariables.each do |v|
    v_str = "@#{v}"
    instance_variable_set(v_str, fit_activity.send(v))
    self.class.send(:attr_reader, v.to_sym)
  end
  # Generate HTML file for this activity.
  generate_html_view
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



22
23
24
# File 'lib/postrunner/Activity.rb', line 22

def db
  @db
end

#fit_activityObject (readonly)

Returns the value of attribute fit_activity.



22
23
24
# File 'lib/postrunner/Activity.rb', line 22

def fit_activity
  @fit_activity
end

#fit_fileObject (readonly)

Returns the value of attribute fit_file.



22
23
24
# File 'lib/postrunner/Activity.rb', line 22

def fit_file
  @fit_file
end

#html_dirObject (readonly)

Returns the value of attribute html_dir.



22
23
24
# File 'lib/postrunner/Activity.rb', line 22

def html_dir
  @html_dir
end

#html_fileObject (readonly)

Returns the value of attribute html_file.



22
23
24
# File 'lib/postrunner/Activity.rb', line 22

def html_file
  @html_file
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/postrunner/Activity.rb', line 22

def name
  @name
end

Instance Method Details

#checkObject



50
51
52
53
# File 'lib/postrunner/Activity.rb', line 50

def check
  @fit_activity = load_fit_file
  Log.info "FIT file #{@fit_file} is OK"
end

#dump(filter) ⇒ Object



55
56
57
# File 'lib/postrunner/Activity.rb', line 55

def dump(filter)
  @fit_activity = load_fit_file(filter)
end

#encode_with(coder) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/postrunner/Activity.rb', line 71

def encode_with(coder)
  attr_ignore = %w( @db @fit_activity )

  instance_variables.each do |v|
    v = v.to_s
    next if attr_ignore.include?(v)

    coder[v[1..-1]] = instance_variable_get(v)
  end
end

#generate_html_viewObject



110
111
112
113
114
# File 'lib/postrunner/Activity.rb', line 110

def generate_html_view
  @fit_activity = load_fit_file unless @fit_activity
  ActivityView.new(self, @db.cfg[:unit_system], @db.predecessor(self),
                   @db.successor(self))
end

#late_init(db) ⇒ Object



44
45
46
47
48
# File 'lib/postrunner/Activity.rb', line 44

def late_init(db)
  @db = db
  @html_dir = File.join(@db.db_dir, 'html')
  @html_file = File.join(@html_dir, "#{@fit_file[0..-5]}.html")
end

#register_records(db) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/postrunner/Activity.rb', line 98

def register_records(db)
  @fit_activity.personal_records.each do |r|
    if r.longest_distance == 1
      # In case longest_distance is 1 the distance is stored in the
      # duration field in 10-th of meters.
      db.register_result(r.duration * 10.0 , 0, r.start_time, @fit_file)
    else
      db.register_result(r.distance, r.duration, r.start_time, @fit_file)
    end
  end
end

#rename(name) ⇒ Object



93
94
95
96
# File 'lib/postrunner/Activity.rb', line 93

def rename(name)
  @name = name
  generate_html_view
end

#showObject



82
83
84
85
86
# File 'lib/postrunner/Activity.rb', line 82

def show
  generate_html_view #unless File.exists?(@html_file)

  @db.show_in_browser(@html_file)
end

#summaryObject



88
89
90
91
# File 'lib/postrunner/Activity.rb', line 88

def summary
  @fit_activity = load_fit_file unless @fit_activity
  puts ActivitySummary.new(@fit_activity, name, @db.cfg[:unit_system]).to_s
end

#yaml_initialize(tag, value) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/postrunner/Activity.rb', line 59

def yaml_initialize(tag, value)
  # Create attr_readers for cached variables.
  @@CachedVariables.each { |v| self.class.send(:attr_reader, v.to_sym) }

  # Load all attributes and assign them to instance variables.
  value.each do |a, v|
    instance_variable_set("@" + a, v)
  end
  # Use the FIT file name as activity name if none has been set yet.
  @name = @fit_file unless @name
end