Class: Fit4Ruby::FitDefinitionField

Inherits:
BinData::Record
  • Object
show all
Includes:
FitDefinitionFieldBase
Defined in:
lib/fit4ruby/FitDefinitionField.rb

Overview

The FitDefinitionField models the part of the FIT file that contains the template definition for a field of a FitMessageRecord. It should match the corresponding definition in GlobalFitMessages. In case we don’t have a known entry in GlobalFitMessages we can still read the file since we know the exact size of the binary records.

Instance Method Summary collapse

Methods included from FitDefinitionFieldBase

#base_type_bytes, fit_type_to_bin_data, #is_array?, #set_length, #set_type, #total_bytes, #type, undefined_value, #undefined_value

Instance Method Details

#initObject



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

def init
  @global_message_number = parent.parent.global_message_number.snapshot
  @global_message_definition = GlobalFitMessages[@global_message_number]
  field_number = field_definition_number.snapshot
  if @global_message_definition &&
     (field = @global_message_definition.fields_by_number[field_number])
     @name = field.respond_to?('name') ? field.name :
                                         "choice_#{field_number}"
     @type = field.respond_to?('type') ? field.type : nil

     if @type && (td = FIT_TYPE_DEFS[checked_base_type_number]) &&
        td[0] != @type
       Log.warn "#{@global_message_number}:#{@name} must be of type " +
       "#{@type}, not #{td[0]}"
     end
  else
    @name = "field#{field_definition_number.snapshot}"
    @type = nil
    Log.debug { "Unknown field number #{field_definition_number} " +
                "in global message #{@global_message_number}" }
  end
end

#nameObject



62
63
64
65
# File 'lib/fit4ruby/FitDefinitionField.rb', line 62

def name
  init unless @global_message_number
  @name
end

#to_machine(value) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fit4ruby/FitDefinitionField.rb', line 67

def to_machine(value)
  init unless @global_message_number
  value = nil if value == undefined_value

  field_number = field_definition_number.snapshot
  if value.kind_of?(Array)
    ary = []
    value.each { |v| ary << to_machine(v) }
    return ary
  else
    if @global_message_definition &&
       (field = @global_message_definition.
        fields_by_number[field_number]) &&
       field.respond_to?('to_machine')
      return field.to_machine(value)
    else
      return value
    end
  end
end

#to_s(value) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fit4ruby/FitDefinitionField.rb', line 88

def to_s(value)
  init unless @global_message_number
  value = nil if value == undefined_value

  if value.kind_of?(Array)
    s = '[ '
    value.each { |v| s << to_s(v) + ' ' }
    s + ']'
  else
    field_number = field_definition_number.snapshot
    if @global_message_definition &&
       (field = @global_message_definition.fields_by_number[field_number])
      field.to_s(value)
    else
      "[#{value.to_s}]"
    end
  end
end