Class: Fit4Ruby::GlobalFitMessage

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

Overview

The GlobalFitMessage stores an abstract description of a particular FitMessage. It holds information like the name, the global ID number and the data fields of the message.

Defined Under Namespace

Classes: AltField, Field

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, number) ⇒ GlobalFitMessage

Create a new GlobalFitMessage definition.

Parameters:

  • name (String)

    name of the FIT message

  • number (Fixnum)

    global message number



202
203
204
205
206
207
208
209
210
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 202

def initialize(name, number)
  @name = name
  @number = number
  # Field names must be unique. A name always matches a single Field.
  @fields_by_name = {}
  # Field numbers are not unique. A group of alternative fields shares the
  # same number and is stored as an AltField. Otherwise as Field.
  @fields_by_number = {}
end

Instance Attribute Details

#fields_by_nameObject (readonly)

Returns the value of attribute fields_by_name.



24
25
26
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 24

def fields_by_name
  @fields_by_name
end

#fields_by_numberObject (readonly)

Returns the value of attribute fields_by_number.



24
25
26
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 24

def fields_by_number
  @fields_by_number
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 24

def name
  @name
end

#numberObject (readonly)

Returns the value of attribute number.



24
25
26
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 24

def number
  @number
end

Instance Method Details

#==(m) ⇒ Object

Two GlobalFitMessage objects are considered equal if they have the same number, name and list of named fields.



214
215
216
217
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 214

def ==(m)
  @number == m.number && @name == m.name &&
    @fields_by_name.keys.sort == m.fields_by_name.keys.sort
end

#alt_field(number, ref_field, &block) ⇒ Object

Define a new set of Field alternatives for this message definition.



227
228
229
230
231
232
233
234
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 227

def alt_field(number, ref_field, &block)
  unless @fields_by_name.include?(ref_field)
    raise "Unknown ref_field: #{ref_field}"
  end

  field = AltField.new(self, ref_field, &block)
  register_field_by_number(field, number)
end

#construct(field_values_by_name) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 252

def construct(field_values_by_name)
  gfm = GlobalFitMessage.new(@name, @number)

  @fields_by_number.each do |number, field|
    if field.is_a?(AltField)
      # For alternative fields, we need to look at the value of the
      # selector field and pick the corresponding Field.
      field = field.select(field_values_by_name)
    end
    gfm.field(number, field.type, field.name, field.opts)
  end

  gfm
end

#field(number, type, name, opts = {}) ⇒ Object

Define a new Field for this message definition.



220
221
222
223
224
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 220

def field(number, type, name, opts = {})
  field = Field.new(type, name, opts)
  register_field_by_name(field, name)
  register_field_by_number(field, number)
end

#register_field_by_name(field, name) ⇒ Object



236
237
238
239
240
241
242
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 236

def register_field_by_name(field, name)
  if @fields_by_name.include?(name)
    raise "Field '#{name}' has already been defined"
  end

  @fields_by_name[name] = field
end

#register_field_by_number(field, number) ⇒ Object



244
245
246
247
248
249
250
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 244

def register_field_by_number(field, number)
  if @fields_by_name.include?(number)
    raise "Field #{number} has already been defined"
  end

  @fields_by_number[number] = field
end

#write(io, local_message_type, global_fit_message = self) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 267

def write(io, local_message_type, global_fit_message = self)
  header = FitRecordHeader.new
  header.normal = 0
  header.message_type = 1
  header.local_message_type = local_message_type
  header.write(io)

  definition = FitDefinition.new
  definition.global_message_number = @number
  definition.setup(global_fit_message)
  definition.write(io)
end