module OpenWeather::Models::Mixins::Temp

Public Instance Methods

temperature_property(field) click to toggle source
# File lib/open_weather/models/mixins/temp.rb, line 10
def temperature_property(field)
  property field

  define_method "#{field}_k" do
    to_kelvin(send(field))
  end

  define_method "#{field}_c" do
    to_celcius(send(field))
  end

  define_method "#{field}_f" do
    to_farenheit(send(field))
  end
end

Private Instance Methods

to_celcius(value) click to toggle source
# File lib/open_weather/models/mixins/temp.rb, line 40
def to_celcius(value)
  case units
  when :metric
    value
  when :imperial
    ((value.to_f - 32) * 5 / 9).round(2)
  else
    (value.to_f - 273.15).round(2)
  end
end
to_farenheit(value) click to toggle source
# File lib/open_weather/models/mixins/temp.rb, line 51
def to_farenheit(value)
  case units
  when :metric
    ((value.to_f * 9 / 5) + 32).round(2)
  when :imperial
    value
  else
    ((value.to_f - 273.15) * 9 / 5 + 32).round(2)
  end
end
to_kelvin(value) click to toggle source
# File lib/open_weather/models/mixins/temp.rb, line 29
def to_kelvin(value)
  case units
  when :metric
    (value.to_f + 273.15).round(2)
  when :imperial
    ((value.to_f - 32) * 5 / 9 + 273.15).round(2)
  else
    value
  end
end