Class: WeatherBug::TransformableData

Inherits:
Object
  • Object
show all
Defined in:
lib/weatherbug/transformable_data.rb

Overview

This is a super-convenient class for transorming data and requiring things in the different associated WeatherBug data classes

Direct Known Subclasses

Forecast, Link, LiveObservation, Station, StationHint

Class Method Summary collapse

Class Method Details

.from_document(document) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/weatherbug/transformable_data.rb', line 15

def self.from_document(document)
  object = self.new
  @transformers.each do |key, options|
    value = document.xpath(key).text
    # If this thing is empty, skip it
    next if value.nil?
    # If this thing needs to be transformed, transform it (from a string)
    value = TransformableData.send(options[:transform], value) if options.has_key?(:transform)
    # Assign it on the class
    object.send("#{options[:name]}=", value)
  end
  object
end

.register_transformer(name, options = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/weatherbug/transformable_data.rb', line 7

def self.register_transformer(name, options = {})
  @transformers ||= {}
  options[:name] = name.split(/[:@]/).last.gsub('-', '_').to_sym unless options.has_key?(:name)
  private; attr_writer options[:name]
  public;  attr_reader options[:name]
  @transformers[name] = options
end