Class: SimpleCar

Inherits:
Object
  • Object
show all
Includes:
Vehicle
Defined in:
lib/simple_car.rb

Overview

Represents a simplified car that uses a string identifier instead of a GUID. Includes the Vehicle module and covers:

primitive fields, static members, constructors, primitive-array I/O,
and methods that accept / return complex types from the same library.

Constant Summary collapse

DEFAULT_WHEELS =

Default number of wheels for a SimpleCar.

4

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(make, model, year, id = nil) ⇒ SimpleCar

Initializes a new car with identity fields and default runtime state.

When id is omitted or blank, a UUID string is generated automatically.

Parameters:

  • make (String)

    Vehicle manufacturer, for example Toyota.

  • model (String)

    Vehicle model name, for example Corolla.

  • year (Integer)

    Four-digit production year.

  • id (String, nil) (defaults to: nil)

    Optional stable identifier; a UUID is assigned when empty.

Raises:

  • (ArgumentError)

    If year is before 1886 or make/model are blank.



72
73
74
75
76
77
78
79
80
81
# File 'lib/simple_car.rb', line 72

def initialize(make, model, year, id = nil)
  @id = id.nil? || id.strip.empty? ? SecureRandom.uuid : id
  @make = make
  @model = model
  @year = year
  @is_running = false
  @mileage_history = []
  @tags = []
  self.class.instance_variable_set(:@total_cars_created, self.class.total_cars_created + 1)
end

Class Attribute Details

.total_cars_createdInteger (readonly)

Returns how many SimpleCar instances have been constructed.

The counter increments on every successful call to #initialize and is shared process-wide for the loaded module.

Returns:

  • (Integer)

    Total number of cars created since import.

Author:

  • graftcode

Since:

  • 0.2.1



29
30
31
# File 'lib/simple_car.rb', line 29

def total_cars_created
  @total_cars_created
end

Instance Attribute Details

#idString (readonly)

Returns The string identifier for the car.

Returns:

  • (String)

    The string identifier for the car.



61
62
63
# File 'lib/simple_car.rb', line 61

def id
  @id
end

#makeString (readonly)

Returns The make of the car.

Returns:

  • (String)

    The make of the car.



61
# File 'lib/simple_car.rb', line 61

attr_reader :id, :make, :model, :year

#modelString (readonly)

Returns The model of the car.

Returns:

  • (String)

    The model of the car.



61
# File 'lib/simple_car.rb', line 61

attr_reader :id, :make, :model, :year

#yearObject (readonly)

Returns the value of attribute year.



61
# File 'lib/simple_car.rb', line 61

attr_reader :id, :make, :model, :year

Class Method Details

.create_defaultSimpleCar

Creates a car with placeholder make, model, and the current year.

Useful for demos and graft smoke tests where explicit vehicle details are not important.

Examples:

car = SimpleCar.create_default
car.make #=> "DefaultMake"

Returns:

  • (SimpleCar)

    A new car using DefaultMake / DefaultModel / 2024.



39
40
41
# File 'lib/simple_car.rb', line 39

def create_default
  new("DefaultMake", "DefaultModel", 2024)
end

.get_default_makesArray<String>

Returns the list of default makes used in sample data.

Returns:

  • (Array<String>)

    Array of make names.



46
47
48
# File 'lib/simple_car.rb', line 46

def get_default_makes
  %w[Toyota Tesla Ford Honda Chevrolet]
end

Instance Method Details

#add_mileage_entry(miles) ⇒ void

This method returns an undefined value.

Appends a mileage reading to the in-memory history.

Parameters:

  • miles (Integer)

    Non-negative distance traveled since the previous entry.

Raises:

  • (ArgumentError)

    If miles is negative.



127
128
129
# File 'lib/simple_car.rb', line 127

def add_mileage_entry(miles)
  @mileage_history << miles
end

#duplicateSimpleCar

Returns a deep copy of this car, including runtime collections.

The duplicate shares the same id and field values as the source instance.

Returns:

  • (SimpleCar)

    Independent instance with identical state.

See Also:



162
163
164
165
166
167
168
# File 'lib/simple_car.rb', line 162

def duplicate
  copy = SimpleCar.new(@make, @model, @year, @id)
  copy.instance_variable_set(:@is_running, @is_running)
  copy.instance_variable_set(:@mileage_history, @mileage_history.dup)
  copy.instance_variable_set(:@tags, @tags.dup)
  copy
end

#get_mileage_historyArray<Integer>

Returns the recorded mileage history for the car.

Returns:

  • (Array<Integer>)

    Mileage entries in insertion order.



134
135
136
# File 'lib/simple_car.rb', line 134

def get_mileage_history
  @mileage_history.dup
end

#get_tagsArray<String>

Deprecated.

Prefer reading tags from domain-specific repositories in new code.

Returns a defensive copy of the current tags.

Returns:

  • (Array<String>)

    Tag values currently stored on the instance.



150
151
152
# File 'lib/simple_car.rb', line 150

def get_tags
  @tags.dup
end

#is_runningBoolean

Gets a value indicating whether the car is currently running (started).

Returns:

  • (Boolean)

    true if the car is running; false otherwise.



95
96
97
# File 'lib/simple_car.rb', line 95

def is_running
  @is_running
end

#set_tags(tags) ⇒ void

This method returns an undefined value.

Replaces the car’s tags with the given list.

Parameters:

  • tags (Array<String>, nil)

    Tags to assign; nil clears the list.



142
143
144
# File 'lib/simple_car.rb', line 142

def set_tags(tags)
  @tags = tags ? tags.dup : []
end

#startvoid

This method returns an undefined value.

Starts the engine and marks the car as running.

Calling this method twice in a row leaves the car running; it does not raise when already started.

Raises:

  • (RuntimeError)

    Reserved for future guard rails in stricter implementations.



107
108
109
# File 'lib/simple_car.rb', line 107

def start
  @is_running = true
end

#stopvoid

This method returns an undefined value.

Stops the engine and marks the car as not running.

Idempotent: stopping an already stopped car is allowed.



116
117
118
# File 'lib/simple_car.rb', line 116

def stop
  @is_running = false
end

#to_sString

Returns a string that represents the current car.

Returns:

  • (String)

    A string containing the id, year, make, model and type (“SimpleCar”), plus the running state.



189
190
191
# File 'lib/simple_car.rb', line 189

def to_s
  "#{@id} #{@year} #{@make} #{@model} (SimpleCar) #{@is_running ? "Running" : "Not Running"}"
end

#wheelsInteger

Gets the number of wheels on the car.

Returns:

  • (Integer)

    The number of wheels (always 4 for this sample type).



88
89
90
# File 'lib/simple_car.rb', line 88

def wheels
  DEFAULT_WHEELS
end

#with_year(new_year) ⇒ SimpleCar

Return a copy with an updated model year.

Parameters:

  • new_year (Integer)

    Replacement production year.

Returns:

  • (SimpleCar)

    New instance sharing identity fields with this car.

Raises:

  • (ArgumentError)

    If new_year is before 1886.

See Also:



176
177
178
179
180
181
182
# File 'lib/simple_car.rb', line 176

def with_year(new_year)
  copy = SimpleCar.new(@make, @model, new_year, @id)
  copy.instance_variable_set(:@is_running, @is_running)
  copy.instance_variable_set(:@mileage_history, @mileage_history.dup)
  copy.instance_variable_set(:@tags, @tags.dup)
  copy
end