Class: Barometer::Query::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/barometer/query/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, units = :metric) ⇒ Base

Returns a new instance of Base



6
7
8
9
10
11
12
# File 'lib/barometer/query/base.rb', line 6

def initialize(query, units=:metric)
  @q = query.dup
  @units = units
  detect_format
  freeze_query
  @conversions = {}
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format



4
5
6
# File 'lib/barometer/query/base.rb', line 4

def format
  @format
end

#geoObject

Returns the value of attribute geo



4
5
6
# File 'lib/barometer/query/base.rb', line 4

def geo
  @geo
end

#unitsObject (readonly)

Returns the value of attribute units



4
5
6
# File 'lib/barometer/query/base.rb', line 4

def units
  @units
end

Instance Method Details

#add_conversion(format, q) ⇒ Object



18
19
20
21
22
23
# File 'lib/barometer/query/base.rb', line 18

def add_conversion(format, q)
  return unless q

  @conversions[format] = q
  converted_query(q, format)
end

#convert!(*preferred_formats) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/barometer/query/base.rb', line 36

def convert!(*preferred_formats)
  return self if preferred_formats.include?(format)

  get_conversion(*preferred_formats) ||
    do_conversion(format, preferred_formats) ||
    raise(ConversionNotPossible)
end

#get_conversion(*formats) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/barometer/query/base.rb', line 25

def get_conversion(*formats)
  converted_format = formats.detect{|f| format == f || @conversions.has_key?(f)}
  return unless converted_format

  if converted_format == format
    self
  else
    converted_query(@conversions[converted_format], converted_format)
  end
end

#metric?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/barometer/query/base.rb', line 44

def metric?
  units == :metric
end

#qObject



14
15
16
# File 'lib/barometer/query/base.rb', line 14

def q
  @format_klass.convert_query(@q)
end

#to_sObject



48
49
50
# File 'lib/barometer/query/base.rb', line 48

def to_s
  q.to_s
end