Class: Fit4Ruby::GlobalFitMessage::AltField
- Inherits:
-
Object
- Object
- Fit4Ruby::GlobalFitMessage::AltField
- 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
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#ref_field ⇒ Object
readonly
Returns the value of attribute ref_field.
Instance Method Summary collapse
- #field(ref_value, type, name, opts = {}) ⇒ Object
-
#initialize(message, ref_field, &block) ⇒ AltField
constructor
Create a new AltField object.
-
#select(field_values_by_name) ⇒ Object
Select the alternative field based on the actual field values of the FitMessageRecord.
Constructor Details
#initialize(message, ref_field, &block) ⇒ AltField
Create a new AltField object.
181 182 183 184 185 186 187 |
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 181 def initialize(, ref_field, &block) @message = @ref_field = ref_field @fields = {} instance_eval(&block) if block_given? end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
174 175 176 |
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 174 def fields @fields end |
#ref_field ⇒ Object (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 |