Class: Fit4Ruby::FitRecord

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

Overview

The FitRecord is a basic building block of a FIT file. It can either contain a definition of a message record or an actual message record with FIT data. A FIT record always starts with a header that describes what kind of record this is.

Instance Method Summary collapse

Constructor Details

#initialize(definitions) ⇒ FitRecord

Returns a new instance of FitRecord.



28
29
30
31
# File 'lib/fit4ruby/FitRecord.rb', line 28

def initialize(definitions)
  @definitions = definitions
  @name = @number = @fields = nil
end

Instance Method Details

#dumpObject



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fit4ruby/FitRecord.rb', line 75

def dump
  return unless @fields

  begin
    puts "Message #{@number}: #{@name}" unless @fields.empty?
    @fields.each do |type, name, value|
      puts " [#{"%-7s" % type}] #{name}: " + "#{value}"
    end
  rescue Errno::EPIPE
    # Avoid ugly error message when aborting a less/more pipe.
  end
end

#read(io, entity, filter, record_counters) ⇒ Object



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
67
68
69
70
71
72
73
# File 'lib/fit4ruby/FitRecord.rb', line 33

def read(io, entity, filter, record_counters)
  header = FitRecordHeader.read(io)

  if !header.compressed?
    # process normal headers
    local_message_type = header.local_message_type.snapshot
    if header.message_type.snapshot == 1
      # process definition message
      definition = FitDefinition.read(io)
      @definitions[local_message_type] = FitMessageRecord.new(definition)
    else
      # process data message
      definition = @definitions[local_message_type]
      unless definition
        Log.fatal "Undefined local message type: #{local_message_type}"
      end
      if filter
        @number = @definitions[local_message_type].global_message_number
        index = (record_counters[@number] += 1)

        # Check if we have a filter defined to collect raw dumps of the
        # data in the message records. The dump is collected in @fields
        # for later output.
        if (filter.record_numbers.nil? ||
            filter.record_numbers.include?(@number)) &&
           (filter.record_indexes.nil? ||
            filter.record_indexes.include?(index))
          @name = definition.name
          @fields = []
        end
      end
      definition.read(io, entity, filter, @fields)
    end
  else
    # process compressed timestamp header
    time_offset = header.time_offset
    Log.fatal "Support for compressed headers not yet implemented"
  end

  self
end