Class: Stormglass::Value

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/stormglass/value.rb

Overview

represents a value for a stormglass prediction source with prefered unit_type contains a collection of Stormglass::Subvalue’s, which represent a particular unit of measurement for example, a Stormglass::Value for air_temperature would have Stormglass::Subvalue’s for Celsius and Fahrenheit

Instance Method Summary collapse

Constructor Details

#initialize(attribute, src, data_source, unit_type) ⇒ Value

Returns a new instance of Value.



10
11
12
13
14
15
# File 'lib/stormglass/value.rb', line 10

def initialize(attribute, src, data_source, unit_type)
  @attribute = attribute
  @src = src
  @data_source = data_source
  @unit_type = unit_type
end

Instance Method Details

#attributeObject



36
37
38
# File 'lib/stormglass/value.rb', line 36

def attribute
  @attribute
end

#data_sourceObject



17
18
19
# File 'lib/stormglass/value.rb', line 17

def data_source
  @data_source
end

#data_sourcesObject

returns the sources available for this value



28
29
30
# File 'lib/stormglass/value.rb', line 28

def data_sources
  src.collect{|v| v['source']}
end

#dictObject



65
66
67
# File 'lib/stormglass/value.rb', line 65

def dict
  Stormglass::RESULT_DICT[attribute]
end

#for_sourceObject



21
22
23
24
25
# File 'lib/stormglass/value.rb', line 21

def for_source
  @src.collect do |v|
    v['value'] if (!v['source'].nil? && v['source'] == data_source)
  end.compact.first
end

#inspectObject



44
45
46
47
48
49
# File 'lib/stormglass/value.rb', line 44

def inspect
  string = "#<#{self.class.to_s} "
  string +="value=#{preffered_subvalue.value}, unit='#{preffered_subvalue.unit}', description='#{preffered_subvalue.description}', "
  string +="unit_type='#{preffered_subvalue.unit_type}', unit_types=#{unit_types.to_s}, data_source='#{@data_source}', data_sources=#{data_sources}>"
  string
end

#preffered_subvalueObject



55
56
57
58
59
60
61
62
63
# File 'lib/stormglass/value.rb', line 55

def preffered_subvalue
  if @unit_type
    subvalues.find{|subvalue| subvalue.unit_type == @unit_type}
  elsif setting_key = Stormglass.settings.units[@attribute]
    subvalues.find{|subvalue| subvalue.unit_type == setting_key}
  else
    subvalues.first
  end
end

#srcObject



40
41
42
# File 'lib/stormglass/value.rb', line 40

def src
  @src
end

#subvaluesObject



69
70
71
72
73
74
75
# File 'lib/stormglass/value.rb', line 69

def subvalues
  subvals = []
  raw_val = for_source()
  subvals << {value: raw_val}.merge(dict)
  Stormglass::AlternateValues.perform(subvals)
  subvals.collect{|subvalue| Stormglass::Subvalue.new(subvalue) }
end

#to_sObject



32
33
34
# File 'lib/stormglass/value.rb', line 32

def to_s
  preffered_subvalue.to_s
end

#unit_typesObject



51
52
53
# File 'lib/stormglass/value.rb', line 51

def unit_types
  subvalues.collect(&:unit_type)
end