Class: Fit4Ruby::FitDataRecord

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

Direct Known Subclasses

Activity, FitFileId

Instance Method Summary collapse

Constructor Details

#initialize(record_id) ⇒ FitDataRecord

Returns a new instance of FitDataRecord.



20
21
22
23
# File 'lib/fit4ruby/FitDataRecord.rb', line 20

def initialize(record_id)
  @message = GlobalFitMessages.find_by_name(record_id)
  @renames = {}
end

Instance Method Details

#rename(fit_var, var) ⇒ Object



25
26
27
# File 'lib/fit4ruby/FitDataRecord.rb', line 25

def rename(fit_var, var)
  @renames[fit_var] = var
end

#write(io, id_mapper) ⇒ Object



29
30
31
32
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
74
# File 'lib/fit4ruby/FitDataRecord.rb', line 29

def write(io, id_mapper)
  global_message_number = @message.number

  # Map the global message number to the current local message number.
  unless (local_message_number = id_mapper.get_local(global_message_number))
    # If the current dictionary does not contain the global message
    # number, we need to create a new entry for it. The index in the
    # dictionary is the local message number.
    local_message_number = id_mapper.add_global(global_message_number)
    # Write the definition of the global message number to the file.
    @message.write(io, local_message_number)
  end

  # Write data record header.
  header = FitRecordHeader.new
  header.normal = 0
  header.message_type = 0
  header.local_message_type = local_message_number
  header.write(io)

  # Create a BinData::Struct object to store the data record.
  fields = []
  @message.fields.each do |field_number, field|
    bin_data_type = FitDefinitionField.fit_type_to_bin_data(field.type)
    fields << [ bin_data_type, field.name ]
  end
  bd = BinData::Struct.new(:endian => :little, :fields => fields)

  # Fill the BinData::Struct object with the values from the corresponding
  # instance variables.
  @message.fields.each do |field_number, field|
    iv = "@#{@renames[field.name] || field.name}"
    if instance_variable_defined?(iv) &&
       !(iv_value = instance_variable_get(iv)).nil?
      value = field.native_to_fit(iv_value)
    else
      # If we don't have a corresponding variable or the variable is nil
      # we write the 'undefined' value instead.
      value = FitDefinitionField.undefined_value(field.type)
    end
    bd[field.name] = value
  end

  # Write the data record to the file.
  bd.write(io)
end