Sha256: 7ac81fd0b74b0e6fd760df9ae1193a552eb6409904b0fea2784485959c08cede

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

class Forecast
  module Model
    
    def initialize(object_attribute_hash = {})
      object_attribute_hash.map do |(k, v)| 
        send("#{k}=", v) if respond_to?("#{k}=")
      end
    end
    
    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
    
    def self.included(base)
      base.extend(ClassMethods)
    end
  
    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
  
    def to_json *a
      as_json.to_json a
    end
    
    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
    
    # end
    
    private 
    
      def slugify(string)
        string.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
      end
      
      module ClassMethods
        
        @attributes = []
        
        def attributes
          @attributes
        end
        
        def attr_accessor *attrs
          @attributes = Array attrs
          super
        end
        
      end
    
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
forecast-0.0.7 lib/forecast/model.rb
forecast-0.0.6 lib/forecast/model.rb
forecast-0.0.5 lib/forecast/model.rb
forecast-0.0.4 lib/forecast/model.rb
forecast-0.0.3 lib/forecast/model.rb
forecast-0.0.2 lib/forecast/model.rb