Class: Fit4Ruby::FitFileEntity

Inherits:
Object
  • Object
show all
Defined in:
lib/fit4ruby/FitFileEntity.rb

Overview

The FIT file is a generic container for all kinds of data. This could be activity data, config files, workout definitions, etc. All data is stored in FIT message records. Also the information what kind of FIT file this is is stored in such a record. When we start reading the file, we actually don’t know what kind of file it is until we find the right record to tell us. Since we already need to have gathered some information at this point, we use this utility class to store the read data until we know what Ruby objec we need to use to store it for later consumption.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFitFileEntity

Create a FitFileEntity.



33
34
35
36
# File 'lib/fit4ruby/FitFileEntity.rb', line 33

def initialize
  @top_level_record = nil
  @developer_fit_messages = GlobalFitMessageList.new
end

Instance Attribute Details

#developer_fit_messagesObject (readonly)

Returns the value of attribute developer_fit_messages.



30
31
32
# File 'lib/fit4ruby/FitFileEntity.rb', line 30

def developer_fit_messages
  @developer_fit_messages
end

#top_level_recordObject (readonly)

Returns the value of attribute top_level_record.



30
31
32
# File 'lib/fit4ruby/FitFileEntity.rb', line 30

def top_level_record
  @top_level_record
end

Instance Method Details

#checkObject

Check the consistency of the top-level object.



77
78
79
80
# File 'lib/fit4ruby/FitFileEntity.rb', line 77

def check
  return false unless @top_level_record
  @top_level_record.check
end

#new_fit_data_record(type) ⇒ Object

Add a new data record to the top-level object.



65
66
67
68
69
70
71
72
73
74
# File 'lib/fit4ruby/FitFileEntity.rb', line 65

def new_fit_data_record(type)
  return nil unless @top_level_record

  # We already have a record for the top-level type. Just return it.
  return @top_level_record if type == @type

  # For all other types, we need to create a new record inside the
  # top-level record.
  @top_level_record.new_fit_data_record(type)
end

#set_type(type) ⇒ Object

Set what kind of FIT file we are dealing with. a derivative of FitDataRecord.

Returns:

  • The Ruby object that will hold the content of the FIT file. It’s



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fit4ruby/FitFileEntity.rb', line 41

def set_type(type)
  if @top_level_record
    Log.fatal "FIT file type has already been set to " +
              "#{@top_level_record.class}"
  end
  case type
  when 4, 'activity'
    @top_level_record = Activity.new
    @type = 'activity'
  when 32, 'monitoring_b'
    @top_level_record = Monitoring_B.new
    @type = 'monitoring_b'
  when 44, 'metrics'
    @top_level_record = Metrics.new
    @type = 'metrics'
  else
    Log.error "Unsupported FIT file type #{type}"
    return nil
  end

  @top_level_record
end

#write(io, id_mapper) ⇒ Object

Write the top-level object into a IO stream.



83
84
85
86
# File 'lib/fit4ruby/FitFileEntity.rb', line 83

def write(io, id_mapper)
  return unless @top_level_record
  @top_level_record.write(io, id_mapper)
end