Sha256: 068d4dd099a854c5b011550fe2c24613d4718cf2597c4d115736ae619288c9d7

Contents?: true

Size: 743 Bytes

Versions: 3

Compression:

Stored size: 743 Bytes

Contents

require 'json'

module Slate
  module Calculation
    class Base
      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

3 entries across 3 versions & 1 rubygems

Version Path
slate-1.0.0 lib/slate/calculation.rb
slate-0.2.1 lib/slate/calculation.rb
slate-0.2.0 lib/slate/calculation.rb