lib/estimator.rb in estimator-0.0.2 vs lib/estimator.rb in estimator-0.0.3

- old
+ new

@@ -1,47 +1,55 @@ require 'estimator/version' require 'yaml' module Estimator - class Estimate - attr_reader :values - attr_reader :last_estimate - attr_reader :file_path + class Estimate + LAST_ESTIMATE_EMPTY_STRING = 'last estimate is empty' - def initialize( file_name ) - file_name = 'estimator_db.yml' if file_path.nil? - @file_path = "#{Dir.pwd}/#{file_name}" - end + attr_reader :values + attr_reader :last_estimate + attr_reader :file_path - def load - save! unless File.exists?( @file_path ) - yaml_file = ( YAML.load_file( @file_path ) || {} ) - @values = ( yaml_file[:values] || {} ) - @last_estimate = ( yaml_file[:last_estimate] || 'last estimate is empty' ) - end + # TODO: handle file better + def initialize( file_path ) + @file_path = file_path + @values = {} + @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 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 add_value( value, time = nil ) - time = Time.now if time.nil? - @values[value.to_i] = time - end + def save! + File.open( @file_path, 'w' ) do |f| + f.write( { + values: @values, + last_estimate: @last_estimate + }.to_yaml ) + end + end - 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 + def add_value( value, time = nil ) + time = Time.now if time.nil? + @values[value] = time + end - 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