Sha256: a596a3145010db212cf1fb35af6cff1a706d5b5d4dc90dfe0efe59bf99f61f56

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

require 'estimator/version'
require 'yaml'

module Estimator
    class Estimate
        LAST_ESTIMATE_EMPTY_STRING = 'last estimate is empty'

        attr_reader :values
        attr_reader :last_estimate
        attr_reader :file_path

        # TODO: handle file better
        def initialize( file_path )
            @file_path = file_path
            @values = {}
            @last_estimate = LAST_ESTIMATE_EMPTY_STRING
        end

        def load( file_path = @file_path )
            # TODO: change to exception?
            return nil unless File.exists?( file_path )
            # TODO: check for exception
            yaml_file      = YAML.load_file( file_path )
            @values        = yaml_file.fetch( :values, {} )
            @last_estimate = yaml_file.fetch( :last_estimate, LAST_ESTIMATE_EMPTY_STRING )
        end

        def save!
            File.open( @file_path, 'w' ) do |f|
                f.write( {
                    values: @values,
                    last_estimate: @last_estimate
                }.to_yaml )
            end
        end

        def add_value( value, time = nil )
            time = Time.now if time.nil?
            @values[value] = time
        end

        # TODO: Moving Averages
        # http://en.wikipedia.org/wiki/Moving_average
        def estimate
            return 'add more values before estimating' unless @values.count > 1
            # y = ax+b
            b = @values.keys.sort.last
            y = @values.keys.sort.first
            x = @values[y] - @values[b]
            a = (y-b)/x
            @last_estimate = @values[b] + (-b/a)
        end

    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
estimator-0.0.4 lib/estimator.rb
estimator-0.0.3 lib/estimator.rb