Module: Forecast::Model

Included in:
Forecast
Defined in:
lib/forecast/model.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



18
19
20
# File 'lib/forecast/model.rb', line 18

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#as_json(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/forecast/model.rb', line 22

def as_json options = {}
  serialized = Hash.new
  if self.class.attributes != nil
    self.class.attributes.each do |attribute|
      serialized[attribute] = self.public_send attribute
    end
  end
  serialized
end

#from_json(json) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/forecast/model.rb', line 36

def from_json(json)
  self.class.attributes.each do |attribute|
    writer_m = "#{attribute}="
    value = json[attribute.to_s]
    if attribute == :date
      value = DateTime.parse(value)
    end
    send(writer_m, value) if respond_to?(writer_m)
  end
end

#iconObject



10
11
12
13
14
15
16
# File 'lib/forecast/model.rb', line 10

def icon
  if Forecast.config.theme.is_a? Hash
    icon = Forecast.config.theme[self.condition]
    return icon unless icon == nil 
  end
  return slugify(self.condition)
end

#initialize(object_attribute_hash = {}) ⇒ Object



4
5
6
7
8
# File 'lib/forecast/model.rb', line 4

def initialize(object_attribute_hash = {})
  object_attribute_hash.map do |(k, v)| 
    send("#{k}=", v) if respond_to?("#{k}=")
  end
end

#to_json(*a) ⇒ Object



32
33
34
# File 'lib/forecast/model.rb', line 32

def to_json *a
  as_json.to_json a
end