Class: Fit4Ruby::GlobalFitMessage::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Converters

#fit_time_to_time, #secsToDHMS, #secsToHMS, #speedToPace, #time_to_fit_time

Constructor Details

#initialize(type, name, opts = {}) ⇒ Field

Returns a new instance of Field.



28
29
30
31
32
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 28

def initialize(type, name, opts = {})
  @type = type
  @name = name
  @opts = opts
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 26

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



26
27
28
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 26

def opts
  @opts
end

#typeObject (readonly)

Returns the value of attribute type.



26
27
28
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 26

def type
  @type
end

Instance Method Details

#native_to_fit(value) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 75

def native_to_fit(value)
  return nil if value.nil?

  if @opts.include?(:dict) && (dict = GlobalFitDictionaries[@opts[:dict]])
    return dict.value_by_name(value)
  end

  value = (value * @opts[:scale].to_f).to_i if @opts[:scale]
  value += @opts[:offset] if @opts[:offset]

  case @opts[:type]
  when 'coordinate'
    value *= 2147483648.0 / 180.0
  when 'date_time'
    value = time_to_fit_time(value)
  end

  value
end

#to_human(value) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 53

def to_human(value)
  return nil if value.nil?

  if @opts.include?(:dict) &&
     (dict = GlobalFitDictionaries[@opts[:dict]])
    return [ dict.name(value) || "Undocumented value #{value}", nil ]
  end

  value /= @opts[:scale].to_f if @opts[:scale]
  value -= @opts[:offset] if @opts[:offset]

  case @opts[:type]
  when 'coordinate'
    value *= 180.0 / 2147483648
  when 'date_time'
    value = fit_time_to_time(value).strftime("%Y-%m-%d %H:%M:%S")
  when 'duration'
    value = secsToDHMS(value)
  end
  [ value, @opts[:unit] ]
end

#to_machine(value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 34

def to_machine(value)
  return nil if value.nil?

  if @opts.include?(:dict) &&
     (dict = GlobalFitDictionaries[@opts[:dict]])
    return [ dict.name(value) || "Undocumented value #{value}", nil ]
  end

  case @opts[:type]
  when 'coordinate'
    value *= 180.0 / 2147483648
  when 'date_time'
    value = fit_time_to_time(value)
  end
  value /= @opts[:scale].to_f if @opts[:scale]
  value -= @opts[:offset] if @opts[:offset]
  value
end

#to_s(value) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/fit4ruby/GlobalFitMessage.rb', line 95

def to_s(value)
  return "[no value]" if value.nil?

  human_readable = to_human(value)
  "#{human_readable[0]}" +
    "#{ human_readable[1] ? " #{human_readable[1]}" : ''}"
end