lib/fit_parser/file/data.rb in fit_parser-0.0.8 vs lib/fit_parser/file/data.rb in fit_parser-1.0.0
- old
+ new
@@ -1,16 +1,18 @@
module FitParser
class File
class Data < BinData::Record
class_attribute :global_message_number, instance_writer: false
+ class_attribute :dev_definitions, instance_writer: false
- def self.generate(definition)
+ def self.generate(definition, dev_definitions = nil)
msg_num = definition.global_message_number.snapshot
type = Definitions.get_name(msg_num) || "data_record_#{msg_num}"
Class.new(self) do
self.global_message_number = msg_num
+ self.dev_definitions = dev_definitions
endian definition.endianness
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def record_type
@@ -34,10 +36,60 @@
end
code << "def #{field.name}\n"
if field.scale && field.scale != 1
- code << "scale = #{field.scale.inspect}.0\n"
+ scale = field.scale
+ if scale.is_a?(Integer)
+ code << "scale = #{scale.inspect}.0\n"
+ else
+ code << "scale = #{scale.inspect}\n"
+ end
+ else
+ code << "scale = nil\n"
+ end
+
+ if field.dyn_data
+ code << "dyn = #{field.dyn_data}\n"
+ else
+ code << "dyn = nil\n"
+ end
+ code << <<-RUBY
+ get_value #{field.raw_name}.snapshot, '#{field.real_type}', scale, dyn
+ end
+ RUBY
+
+ class_eval code, __FILE__, __LINE__ + 1
+ end
+
+ definition.dev_fields_arr.each do |field|
+ data = dev_definitions[field[:developer_data_index].to_s][field[:field_number].to_s]
+ field.base_type_number = data[:raw_field_2]
+ field.name = data[:raw_field_3].downcase.gsub(' ', '_')
+ field.scale = data[:raw_field_6] && data[:raw_field_6] != 255 ? data[:raw_field_6] : nil
+ code = ''
+
+ # in case the field size is a multiple of the field length, we must build an array
+ if field.type != 'string' && field.field_size > field.length
+ code << "array :#{field.raw_name}, :type => :#{field.type}, :initial_length => #{field.field_size/field.length}\n"
+ else
+ # string are not null terminated when they have exactly the lenght of the field
+ code << "#{field.type} :#{field.raw_name}"
+ if field.type == 'string'
+ code << ", :read_length => #{field.field_size}, :trim_padding => true"
+ end
+ code << "\n"
+ end
+
+ code << "def #{field.name}\n"
+
+ if field.scale && field.scale != 1
+ scale = field.scale
+ if scale.is_a?(Integer)
+ code << "scale = #{scale.inspect}.0\n"
+ else
+ code << "scale = #{scale.inspect}\n"
+ end
else
code << "scale = nil\n"
end
if field.dyn_data