Class: Fit4Ruby::GlobalFitMessage::AltField

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, ref_field, &block) ⇒ AltField

Create a new AltField object.

Parameters:

  • message (GlobalFitMessage)

    reference to the GlobalFitMessage this field belongs to.

  • ref_field (String)

    The name of the field that is used to select the alternative.



181
182
183
184
185
186
187
# 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

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



174
175
176
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 174

def fields
  @fields
end

#ref_fieldObject (readonly)

Returns the value of attribute ref_field.



174
175
176
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 174

def ref_field
  @ref_field
end

Instance Method Details

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



189
190
191
192
193
194
195
196
197
198
199
# 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) ⇒ Object

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



203
204
205
206
207
208
209
210
211
212
213
214
215
216
# 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