class Fit4Ruby::GlobalFitMessage::AltField

A GlobalFitMessage may have Field entries that are dependent on the value of another Field. These alternative fields all depend on the value of a specific other Field of the GlobalFitMessage and their presense is mutually exclusive. An AltField object models such a group of Field objects.

Attributes

fields[R]
ref_field[R]

Public Class Methods

new(message, ref_field, &block) click to toggle source

Create a new AltField object. @param message [GlobalFitMessage] reference to the GlobalFitMessage

this field belongs to.

@param ref_field [String] The name of the field that is used to select

the alternative.
# File lib/fit4ruby/GlobalFitMessage.rb, line 181
def initialize(message, ref_field, &block)
  @message = message
  @ref_field = ref_field
  @fields = {}

  instance_eval(&block) if block_given?
end

Public Instance Methods

field(ref_value, type, name, opts = {}) click to toggle source
# File lib/fit4ruby/GlobalFitMessage.rb, line 189
def field(ref_value, type, name, opts = {})
  field = Field.new(type, name, opts)
  if ref_value.respond_to?(:each)
    ref_value.each do |rv|
      @fields[rv] = field
    end
  else
    @fields[ref_value] = field
  end
  @message.register_field_by_name(field, name)
end
select(field_values_by_name) click to toggle source

Select the alternative field based on the actual field values of the FitMessageRecord.

# File lib/fit4ruby/GlobalFitMessage.rb, line 203
def select(field_values_by_name)
  unless (value_of_referenced_field = field_values_by_name[@ref_field])
    Log.fatal "The selection field #{@ref_field} for the alternative " +
              "field is undefined in global message #{@message.name}: " +
              field_values_by_name.inspect
  end
  @fields.each do |ref_value, field|
    return field if ref_value == value_of_referenced_field
  end
  return @fields[:default] if @fields[:default]

  Log.fatal "The selector value #{value} for the alternative field " +
            "is not supported in global message #{@message.name}."
end