Sha256: 3976560805cf8f58897f79a409014cf9dfb56622f6713dacff99230fa03a4f95

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

require "amee/data_api/data_category"
require "amee/data_api/data_item_value"
module Amee
  module ProfileApi
    class ProfileItem
      include Amee::Model
      self.path_prefix = "/profiles"
      
      attr_accessor :end, :amount, :data_item, :profile, :valid_from, :start_date, :end_date
      attr_accessor :data_item_label, :data_item_uid, :total_amount
      
      list_populators :item_values => {:class => Amee::DataApi::DataItemValue}   
      item_populators :data_category => {:class => Amee::DataApi::DataCategory},
                      :item_definition => {:class => Amee::DataApi::ItemDefinition}
        
      
      # @todo This currently will not work as profile data_item brings back a profile_category
      #       How stupid is that!
      def self.create(session, path, data_item_uid, fields ={})
        hash = session.create_profile_item(path, data_item_uid, {:query => fields, :raw_response => true})
        self.from_hash(hash, session)
      end
      
      def update(fields = {})
        session.api_call(:put, "profile_item", self.full_path, 
          :query => fields) do |response|
          populate_from_hash!(response)
          self
        end
      end
      
      def destroy
        session.delete_profile_item(self.full_path)
      end
      
      def co2_unit
        amount["unit"]
      end
      
      def co2_value
        amount["value"]
      end
      
      def profile_uid
        profile["uid"]
      end
      
      def data_item_uid
        data_item["uid"]
      end
      
      def full_path
        "#{self.class.path_prefix}/#{profile_uid}" + resource_path
      end
      
      def populate!
        session.api_call(:get, "profile_item", self.full_path) do |response|
          populate_from_hash!(response)
          self
        end
        @lazy_loaded = true
      end
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hookercookerman-amee-0.2.1 lib/amee/profile_api/profile_item.rb