Sha256: 57df967ee8f173b36f013edb353c0cbae195ec911782cbe64a382e6f2e30d7e5

Contents?: true

Size: 1022 Bytes

Versions: 5

Compression:

Stored size: 1022 Bytes

Contents

require 'json'

module Slate
  module Calculation
    def self.all
      [Mean, Last]
    end

    class Base
      def self.name(name=nil)
        return @name if name.nil?
        @name = name
      end

      def self.description(description=nil)
        return @description if description.nil?
        @description = description
      end

      def initialize(graph)
        @graph = graph
      end

      def result
        targets.map do |target|
          name = target.first
          points = target.last
          target name, map(points)
        end
      end

      protected

      # This is the method to do calculations in children classes
      def map(points)
        points
      end

      def targets
        data.map { |t| [t["target"], t["datapoints"].map(&:first).compact] }
      end

      def target(name, value)
        {
          "name"  => name,
          "value" => value
        }
      end

      def data
        @data ||= JSON.parse(@graph.download(:json))
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
slate-1.1.1 lib/slate/calculation.rb
slate-1.1.0 lib/slate/calculation.rb
slate-1.0.3 lib/slate/calculation.rb
slate-1.0.2 lib/slate/calculation.rb
slate-1.0.1 lib/slate/calculation.rb