class Estimator::Estimate

Constants

LAST_ESTIMATE_EMPTY_STRING

Attributes

file_path[R]
last_estimate[R]
values[R]

Public Class Methods

new( file_path ) click to toggle source

TODO: handle file better

# File lib/estimator.rb, line 13
def initialize( file_path )
    @file_path = file_path
    @values = {}
    @last_estimate = LAST_ESTIMATE_EMPTY_STRING
end

Public Instance Methods

add_value( value, time = nil ) click to toggle source
# File lib/estimator.rb, line 37
def add_value( value, time = nil )
    time = Time.now if time.nil?
    @values[value] = time
end
estimate() click to toggle source

TODO: Moving Averages en.wikipedia.org/wiki/Moving_average

# File lib/estimator.rb, line 44
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
load( file_path = @file_path ) click to toggle source
# File lib/estimator.rb, line 19
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
save!() click to toggle source
# File lib/estimator.rb, line 28
def save!
    File.open( @file_path, 'w' ) do |f|
        f.write( {
            values: @values,
            last_estimate: @last_estimate
        }.to_yaml )
    end
end