Skip to content

Instantly share code, notes, and snippets.

@mybuddymichael
Created March 7, 2012 02:35
Show Gist options
  • Save mybuddymichael/1990527 to your computer and use it in GitHub Desktop.
Save mybuddymichael/1990527 to your computer and use it in GitHub Desktop.
require 'date'
class DataPoint
# Create a new data point.
#
# tag_name - A String containing a name or identifier for the
# data being measured.
# time_recorded - A DateTime object for the recording time.
# data - A String or Float which is the recorded data.
#
# Returns a DataPoint object.
def initialize(tag_name, time_recorded, data)
@tag_name, @time_recorded, @data = tag_name.to_sym, time_recorded, data
end
# Returns a Symbol representing the name of the data.
attr_reader :tag_name
# Returns a DateTime object containing the time recorded for the data.
attr_reader :time_recorded
# Returns a String or Float containing the DataPoint's measured data.
attr_reader :data
# Non-destructively add data to a DataPoint instance's @data
# attribute. It will add Floats together or concatenate Strings with a
# newline character between them.
#
# external_data - A String or Float to add to the stored data.
#
# Examples
#
# @data = 1.0
# accumulate(2.0) # => 3.0
#
# @data = "bar"
# accumulate("foo") # => "foo\nbar"
#
# Returns a String or Float of the combined data.
def accumulate(external_data)
if external_data.nil?
@data
else
external_data + "\n" + @data rescue external_data + @data
end
end
end
# Collate DataPoints into a summary of today's data.
#
# data - an Array of DataPoints
#
# Returns a Hash containing today's collated data points.
def run_program(data)
today = Date.today
accumulated_data = {}
data.each do |d|
next unless (d.time_recorded === today)
accumulated_data[d.tag_name] = d.accumulate(accumulated_data[d.tag_name])
end
accumulated_data
end
require_relative 'solution'
require 'minitest/autorun'
class SolutionTest < MiniTest::Unit::TestCase
def setup
@dp1 = DataPoint.new("generator status", DateTime.now, "active")
@dp2 = DataPoint.new("fan rpm", DateTime.now, 1.0)
@dp3 = DataPoint.new("generator status", DateTime.now, "idle")
@dp4 = DataPoint.new("fan rpm", DateTime.now, 2.0)
@dp5 = DataPoint.new("generator status", DateTime.now, "skipped")
@dp_old_1 = DataPoint.new("generator status", DateTime.new, "overloaded")
@dp_old_2 = DataPoint.new("fan rpm", DateTime.new, 10.0)
end
def test_data_point_can_be_initialized
assert_instance_of(DataPoint, @dp1)
end
def test_attributes_can_be_read
assert_equal(:"generator status", @dp1.tag_name)
assert_equal("active", @dp1.data)
assert_instance_of(DateTime, @dp1.time_recorded)
end
def test_accumulate_concatenates_strings_with_instance_data_at_the_end
assert_equal("idle\nactive", @dp1.accumulate("idle"))
end
def test_accumulate_adds_floats_together
assert_equal(2.0, @dp2.accumulate(1.0))
end
def test_run_program_collates_todays_data_only
data_array = [@dp1, @dp2, @dp_old_1, @dp_old_2, @dp3, @dp4, @dp5]
data_hash = run_program(data_array)
assert_equal("active\nidle\nskipped", data_hash[:"generator status"])
assert_equal(3.0, data_hash[:"fan rpm"])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment