Class: InMemorySimpleCarRepository
- Inherits:
-
Object
- Object
- InMemorySimpleCarRepository
- 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
-
.total_instances_created ⇒ Integer
Total number of repository instances created so far.
Class Method Summary collapse
-
.create_with_sample_data ⇒ InMemorySimpleCarRepository
Returns a new repository pre-loaded with sample data.
Instance Method Summary collapse
-
#add(car) ⇒ void
Adds a new car to the repository.
-
#add_range(cars) ⇒ void
Adds a range of simple cars to the repository.
-
#delete(id) ⇒ Boolean
Deletes a simple car by its string identifier.
-
#delete_by_ids(ids) ⇒ Integer
Removes every car whose id is listed in ids.
-
#find_by_make(make) ⇒ Array<SimpleCar>
Finds every car whose manufacturer matches make (case-insensitive).
-
#find_by_year_range(min_year, max_year) ⇒ Array<SimpleCar>
Finds all simple cars whose production year falls within the inclusive range.
-
#get(id) ⇒ SimpleCar?
Gets a simple car by its string identifier.
-
#get_all ⇒ Array<SimpleCar>
Returns all simple cars in the repository.
-
#get_makes ⇒ Array<String>
deprecated
Deprecated.
Prefer querying makes from a domain-specific service in new code.
-
#get_years ⇒ Array<Integer>
Returns all distinct production years currently stored, ascending.
-
#initialize ⇒ InMemorySimpleCarRepository
constructor
Initializes the repository and seeds it with five sample cars.
-
#update(car) ⇒ Boolean
Updates an existing simple car in the repository.
Constructor Details
#initialize ⇒ InMemorySimpleCarRepository
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.
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_created ⇒ Integer
Returns 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_data ⇒ InMemorySimpleCarRepository
Returns a new repository pre-loaded with sample data.
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.
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.
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.
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.
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).
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.
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.
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_all ⇒ Array<SimpleCar>
Returns all simple cars in the repository.
98 99 100 |
# File 'lib/in_memory_simple_car_repository.rb', line 98 def get_all @store.values end |
#get_makes ⇒ Array<String>
Prefer querying makes from a domain-specific service in new code.
Returns every distinct manufacturer present in the repository.
166 167 168 |
# File 'lib/in_memory_simple_car_repository.rb', line 166 def get_makes @store.values.map(&:make).uniq.sort end |
#get_years ⇒ Array<Integer>
Returns all distinct production years currently stored, ascending.
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.
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 |