Class: InMemorySimpleCarRepository

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

Overview

An in-memory repository that stores SimpleCar instances keyed by string ID.

Constant Summary collapse

DEFAULT_CAPACITY =

── static members ──────────────────────────────────────────────

100

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInMemorySimpleCarRepository

── constructor ─────────────────────────────────────────────────



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/in_memory_simple_car_repository.rb', line 23

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_createdObject

Returns the value of attribute total_instances_created.



14
15
16
# File 'lib/in_memory_simple_car_repository.rb', line 14

def total_instances_created
  @total_instances_created
end

Class Method Details

.create_with_sample_dataObject



17
18
19
# File 'lib/in_memory_simple_car_repository.rb', line 17

def self.create_with_sample_data
  new
end

Instance Method Details

#add(car) ⇒ Object

── CRUD ────────────────────────────────────────────────────────

Raises:

  • (ArgumentError)


40
41
42
43
44
45
# File 'lib/in_memory_simple_car_repository.rb', line 40

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) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
# File 'lib/in_memory_simple_car_repository.rb', line 47

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

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

#delete(id) ⇒ Object

Raises:

  • (ArgumentError)


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

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) ⇒ Object

── bulk operations with primitive-array input ──────────────────

Raises:

  • (ArgumentError)


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

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) ⇒ Object

── queries returning complex types ─────────────────────────────

Raises:

  • (ArgumentError)


83
84
85
86
87
88
# File 'lib/in_memory_simple_car_repository.rb', line 83

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) ⇒ Object



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

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

#get(id) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
# File 'lib/in_memory_simple_car_repository.rb', line 53

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

  @store[id]
end

#get_allObject



59
60
61
# File 'lib/in_memory_simple_car_repository.rb', line 59

def get_all
  @store.values
end

#get_makesObject



100
101
102
# File 'lib/in_memory_simple_car_repository.rb', line 100

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

#get_yearsObject

── queries returning primitive arrays ──────────────────────────



96
97
98
# File 'lib/in_memory_simple_car_repository.rb', line 96

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

#update(car) ⇒ Object

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
# File 'lib/in_memory_simple_car_repository.rb', line 63

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