Class: Fit4Ruby::FitMessageRecord
- Inherits:
-
Object
- Object
- Fit4Ruby::FitMessageRecord
- Defined in:
- lib/fit4ruby/FitMessageRecord.rb
Overview
The FitMessageRecord models a part of the FIT file that contains the FIT message records. Each message record has a number, a local type and a set of data fields. The content of a FitMessageRecord is defined by the FitDefinition. This class is only used for reading data from a FIT file. For writing FIT message records, the class FitDataRecord and its decendents are used.
Instance Attribute Summary collapse
-
#global_message_number ⇒ Object
readonly
Returns the value of attribute global_message_number.
-
#message_record ⇒ Object
readonly
Returns the value of attribute message_record.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(definition) ⇒ FitMessageRecord
constructor
A new instance of FitMessageRecord.
- #read(io, entity, filter = nil, fields_dump = nil) ⇒ Object
Constructor Details
#initialize(definition) ⇒ FitMessageRecord
Returns a new instance of FitMessageRecord.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/fit4ruby/FitMessageRecord.rb', line 31 def initialize(definition) @definition = definition @global_message_number = definition..snapshot if (@gfm = GlobalFitMessages[@global_message_number]) @name = @gfm.name else @name = "message#{@global_message_number}" Log.warn { "Unknown global message number #{@global_message_number}" } end @message_record = produce(definition) end |
Instance Attribute Details
#global_message_number ⇒ Object (readonly)
Returns the value of attribute global_message_number.
29 30 31 |
# File 'lib/fit4ruby/FitMessageRecord.rb', line 29 def @global_message_number end |
#message_record ⇒ Object (readonly)
Returns the value of attribute message_record.
29 30 31 |
# File 'lib/fit4ruby/FitMessageRecord.rb', line 29 def @message_record end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
29 30 31 |
# File 'lib/fit4ruby/FitMessageRecord.rb', line 29 def name @name end |
Instance Method Details
#read(io, entity, filter = nil, fields_dump = nil) ⇒ Object
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fit4ruby/FitMessageRecord.rb', line 44 def read(io, entity, filter = nil, fields_dump = nil) @message_record.read(io) if @name == 'file_id' unless (entity_type = @message_record['type'].snapshot) Log.fatal "Corrupted FIT file: file_id record has no type definition" end entity.set_type(entity_type) end obj = entity.new_fit_data_record(@name) # It's important to ensure that alternative fields processed after the # regular fields so that the decision field is already set. sorted_fields = @definition.fields.sort do |f1, f2| f1alt = is_alt_field?(f1) f2alt = is_alt_field?(f2) f1alt == f2alt ? f1.field_definition_number.snapshot <=> f2.field_definition_number.snapshot : f1alt ? 1 : -1 end sorted_fields.each do |field| value = @message_record[field.name].snapshot # Strings are null byte terminated. There may be more bytes in the # file, but we have to discard all bytes from the first null byte # onwards. if value.is_a?(String) && (null_byte = value.index("\0")) value = null_byte == 0 ? '' : value[0..(null_byte - 1)] end field_name, field_def = get_field_name_and_global_def(field, obj) obj.set(field_name, v = (field_def || field).to_machine(value)) if obj if filter && fields_dump && (filter.field_names.nil? || filter.field_names.include?(field_name)) && (value != field.undefined_value || !filter.ignore_undef) fields_dump << DumpedField.new( @global_message_number, field.field_definition_number.snapshot, field_name, field.type(true), (field_def ? field_def : field).to_s(value)) end end end |