Class: InMemorySimpleCarRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/in_memory_simple_car_repository.rb

Overview

An in-memory, thread-safe repository that stores SimpleCar instances using a string identifier.

The repository pre-loads five sample cars on construction so that graft smoke tests can issue queries immediately without seeding data.

Constant Summary collapse

DEFAULT_CAPACITY =

Default capacity hint for the repository.

100

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInMemorySimpleCarRepository

Initializes the repository and seeds it with five sample cars.

The samples cover a mix of makes and production years so that range and make-based queries return non-trivial results out of the box.

Author:

  • graftcode

Since:

  • 0.2.1



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/in_memory_simple_car_repository.rb', line 41

def initialize
  self.class.instance_variable_set(:@total_instances_created, self.class.total_instances_created + 1)
  @store = {}

  samples = [
    SimpleCar.new("Toyota", "Corolla", 2018, "0"),
    SimpleCar.new("Tesla", "Model 3", 2021, "1"),
    SimpleCar.new("Ford", "Mustang", 1967, "2"),
    SimpleCar.new("Honda", "Civic", 2015, "3"),
    SimpleCar.new("Chevrolet", "Camaro", 2020, "4")
  ]

  samples.each { |car| add(car) }
end

Class Attribute Details

.total_instances_createdInteger

Returns Total number of repository instances created so far.

Returns:

  • (Integer)

    Total number of repository instances created so far.



19
20
21
# File 'lib/in_memory_simple_car_repository.rb', line 19

def total_instances_created
  @total_instances_created
end

Class Method Details

.create_with_sample_dataInMemorySimpleCarRepository

Returns a new repository pre-loaded with sample data.

Examples:

repo = InMemorySimpleCarRepository.create_with_sample_data
repo.get_all.size #=> 5

Returns:



28
29
30
# File 'lib/in_memory_simple_car_repository.rb', line 28

def self.create_with_sample_data
  new
end

Instance Method Details

#add(car) ⇒ void

This method returns an undefined value.

Adds a new car to the repository.

The check for duplicates and the insert happen atomically, so concurrent calls cannot create two entries for the same id.

Parameters:

  • car (SimpleCar)

    Car to add; must not be nil.

Raises:

  • (ArgumentError)

    If car is nil or a car with the same id already exists.



66
67
68
69
70
71
# File 'lib/in_memory_simple_car_repository.rb', line 66

def add(car)
  raise ArgumentError, "car cannot be nil" if car.nil?
  raise ArgumentError, "SimpleCar with Id #{car.id} already exists." if @store.key?(car.id)

  @store[car.id] = car
end

#add_range(cars) ⇒ void

This method returns an undefined value.

Adds a range of simple cars to the repository.

Parameters:

  • cars (Array<SimpleCar>)

    Array of cars to add.

Raises:

  • (ArgumentError)

    If cars is nil or any car has an id that already exists.



78
79
80
81
82
# File 'lib/in_memory_simple_car_repository.rb', line 78

def add_range(cars)
  raise ArgumentError, "cars cannot be nil" if cars.nil?

  cars.each { |car| add(car) }
end

#delete(id) ⇒ Boolean

Deletes a simple car by its string identifier.

Parameters:

  • id (String)

    The simple car identifier.

Returns:

  • (Boolean)

    true if the car was removed; otherwise false.

Raises:

  • (ArgumentError)

    If id is nil or whitespace.



121
122
123
124
125
126
127
128
# File 'lib/in_memory_simple_car_repository.rb', line 121

def delete(id)
  raise ArgumentError, "id cannot be nil or whitespace" if id.nil? || id.strip.empty?

  return false unless @store.key?(id)

  @store.delete(id)
  true
end

#delete_by_ids(ids) ⇒ Integer

Removes every car whose id is listed in ids.

Missing ids are silently skipped; the return value reports how many entries were actually removed.

Parameters:

  • ids (Array<String>)

    Identifiers to remove; nil is rejected.

Returns:

  • (Integer)

    Number of cars actually removed.

Raises:

  • (ArgumentError)

    If ids is nil.

Author:

  • graftcode

Since:

  • 0.2.1



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/in_memory_simple_car_repository.rb', line 181

def delete_by_ids(ids)
  raise ArgumentError, "ids cannot be nil" if ids.nil?

  count = 0
  ids.each do |id|
    if @store.key?(id)
      @store.delete(id)
      count += 1
    end
  end
  count
end

#find_by_make(make) ⇒ Array<SimpleCar>

Finds every car whose manufacturer matches make (case-insensitive).

Parameters:

  • make (String)

    Manufacturer name to match; must not be empty.

Returns:

  • (Array<SimpleCar>)

    Matching cars; empty array when none match.

Raises:

  • (ArgumentError)

    If make is nil or whitespace.



137
138
139
140
141
142
# File 'lib/in_memory_simple_car_repository.rb', line 137

def find_by_make(make)
  raise ArgumentError, "make cannot be nil or whitespace" if make.nil? || make.strip.empty?

  lower_make = make.downcase
  @store.values.select { |c| c.make.downcase == lower_make }
end

#find_by_year_range(min_year, max_year) ⇒ Array<SimpleCar>

Finds all simple cars whose production year falls within the inclusive range.

Parameters:

  • min_year (Integer)

    Minimum year (inclusive).

  • max_year (Integer)

    Maximum year (inclusive).

Returns:

  • (Array<SimpleCar>)

    Cars within the given year range.



149
150
151
# File 'lib/in_memory_simple_car_repository.rb', line 149

def find_by_year_range(min_year, max_year)
  @store.values.select { |c| c.year >= min_year && c.year <= max_year }
end

#get(id) ⇒ SimpleCar?

Gets a simple car by its string identifier.

Parameters:

  • id (String)

    The simple car identifier.

Returns:

  • (SimpleCar, nil)

    The matching SimpleCar if found; otherwise nil.

Raises:

  • (ArgumentError)

    If id is nil or whitespace.



89
90
91
92
93
# File 'lib/in_memory_simple_car_repository.rb', line 89

def get(id)
  raise ArgumentError, "id cannot be nil or whitespace" if id.nil? || id.strip.empty?

  @store[id]
end

#get_allArray<SimpleCar>

Returns all simple cars in the repository.

Returns:

  • (Array<SimpleCar>)

    A list of SimpleCar.



98
99
100
# File 'lib/in_memory_simple_car_repository.rb', line 98

def get_all
  @store.values
end

#get_makesArray<String>

Deprecated.

Prefer querying makes from a domain-specific service in new code.

Returns every distinct manufacturer present in the repository.

Returns:

  • (Array<String>)

    Sorted list of unique manufacturer names.



166
167
168
# File 'lib/in_memory_simple_car_repository.rb', line 166

def get_makes
  @store.values.map(&:make).uniq.sort
end

#get_yearsArray<Integer>

Returns all distinct production years currently stored, ascending.

Returns:

  • (Array<Integer>)

    Sorted list of distinct years.



158
159
160
# File 'lib/in_memory_simple_car_repository.rb', line 158

def get_years
  @store.values.map(&:year).uniq.sort
end

#update(car) ⇒ Boolean

Updates an existing simple car in the repository.

Parameters:

  • car (SimpleCar)

    The simple car with updated data.

Returns:

  • (Boolean)

    true if the car was updated; otherwise false (not found).

Raises:

  • (ArgumentError)

    If car is nil.



107
108
109
110
111
112
113
114
# File 'lib/in_memory_simple_car_repository.rb', line 107

def update(car)
  raise ArgumentError, "car cannot be nil" if car.nil?

  return false unless @store.key?(car.id)

  @store[car.id] = car
  true
end