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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definitions) ⇒ FitRecord

Returns a new instance of FitRecord.



31
32
33
34
# File 'lib/fit4ruby/FitRecord.rb', line 31

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

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

Instance Method Details

#dump(index) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fit4ruby/FitRecord.rb', line 78

def dump(index)
  return unless @fields

  begin
    puts "Message #{@number}: #{@name}" unless @fields.empty?
    @fields.sort.each do |field|
      puts field.to_s(index)
    end
  rescue Errno::EPIPE
    # Avoid ugly error message when aborting a less/more pipe.
  end
end

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



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
74
75
76
# File 'lib/fit4ruby/FitRecord.rb', line 36

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