Sha256: 3af1259e370ac4126a417e55bc7e1b623efdf2224c1080c471fc5a589ad818f6

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require 'thermometer/evaluate'

module Thermometer
  module Temperature
    extend ActiveSupport::Concern

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

    module ClassMethods
      include Evaluate::Temperatures
      include ActiveRecord::QueryMethods



      def acts_as_thermometer
        include Thermometer::Temperature::InstanceMethods
      end


      ##
      # Takes one or more associations and defines methods to read temperature per
      # association
      #
      def measures_temperature_for *associations

        options = associations.extract_options!


        associations.each do |association|
          class_eval do
            if reflections[association].respond_to?(:options)
              reflections[association].options[:thermometer] = options
              reflections[association].options[:extend] = Thermometer::ActiveRecord::RelationMethods
            end
          end


        end

      end


      private

      def sample_records options
        options = Thermometer.configuration.process_scope_options(options)

        data_sample options
      end

    end

    module InstanceMethods
      include Evaluate::Temperatures
      #include Evaluate::CalcsForTime


      ##
      # Rollup all associations and self into a hash
      #
      def has_temperatures options={}
        results = {}
        reflections.select {|r,v| v.options.has_key? :thermometer}.each do |r,v|
          results[r] = send(r).has_temperature options
        end
        results[:self] = has_temperature options

        return results
      end

      private

      def sample_records options
        options = Thermometer.configuration.process_scope_options(options)
        sample = [send(options[:date])]
      end

    end

  end
end

ActiveRecord::Base.send :include, Thermometer::Temperature

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thermometer-0.0.3 lib/thermometer/temperature.rb