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( start_time distance duration avg_speed )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Activity.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/postrunner/Activity.rb', line 15

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

  @@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
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/postrunner/Activity.rb', line 50

def method_missing(method_name, *args, &block)
  fit_file = File.join(Config['fit_dir'], @fit_file)
  begin
    @fit_activity = Fit4Ruby.read(fit_file) unless @fit_activity
  rescue Fit4Ruby::Error
    Log.error "Cannot read #{fit_file}: #{$!}"
    return false
  end
  @fit_activity.send(method_name, *args, &block)
end

Instance Attribute Details

#fit_fileObject (readonly)

Returns the value of attribute fit_file.



8
9
10
# File 'lib/postrunner/Activity.rb', line 8

def fit_file
  @fit_file
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/postrunner/Activity.rb', line 9

def name
  @name
end

Instance Method Details

#encode_with(coder) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/postrunner/Activity.rb', line 39

def encode_with(coder)
  attr_ignore = %w( @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

#yaml_initialize(tag, value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/postrunner/Activity.rb', line 27

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