Skip to content

Instantly share code, notes, and snippets.

@Sephi-Chan
Created January 13, 2015 17:52
Show Gist options
  • Save Sephi-Chan/212ee76e747f7680c004 to your computer and use it in GitHub Desktop.
Save Sephi-Chan/212ee76e747f7680c004 to your computer and use it in GitHub Desktop.
Struct prototyping
require 'spec_helper'
RSpec.describe Convoy do
it 'has wayponts' do
Convoyy = Struct.new(:waypoints) do
def replace_waypoint(old_waypoint_step_number, new_waypoints)
waypoints
.select { |waypoint| old_waypoint_step_number <= waypoint.step_number }
.map { |waypoint| waypoint.active = false }
waypoints.push(*new_waypoints)
end
end
Waypoint = Struct.new(:creator, :node, :step_number, :active, :created_at)
# Initial path: Paris, Clermont, Mende, Nimes.
# Replacement path: Paris, Clermont, Lyon, Nimes.
time = Time.new(2015, 1, 13, 12)
paris = Waypoint.new('Corwin', 'Paris', 1, true, time)
clermont = Waypoint.new('Corwin', 'Lyon', 2, true, time)
mende = Waypoint.new('Corwin', 'Mende', 3, true, time)
nimes = Waypoint.new('Corwin', 'Nimes', 4, true, time)
lyon = Waypoint.new('Corwin', 'Lyon', 3, true, time + 30.minutes)
nimes_2 = Waypoint.new('Mandor', 'Nimes', 4, true, time + 30.minutes)
convoy = Convoyy.new([ paris, clermont, mende, nimes ])
convoy.replace_waypoint(3, [ lyon, nimes_2 ] )
expect(convoy.waypoints.size).to eq(6)
expect(convoy.waypoints[2]).to eq(mende)
expect(convoy.waypoints[2].active).to eq(false)
expect(convoy.waypoints[3]).to eq(nimes)
expect(convoy.waypoints[3].active).to eq(false)
expect(convoy.waypoints[4]).to eq(lyon)
expect(convoy.waypoints[5]).to eq(nimes_2)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment