sdn_test_simple_car 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rspec +3 -3
- data/.rubocop.yml +8 -8
- data/LICENSE.txt +21 -21
- data/README.md +73 -73
- data/Rakefile +12 -12
- data/grft_test_simple_car.gemspec +30 -30
- data/lib/grft_test_simple_car/version.rb +5 -5
- data/lib/simple_car.rb +192 -107
- data/lib/vehicle.rb +38 -38
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1d288205fa6469a8140227436c936298faf8a470a3ceb1486cecf2e959eb7058
|
|
4
|
+
data.tar.gz: 2568cbe8303917aa40e033639bd72f5bc035f83131bd75590286024121bb5d4c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8e64856bd0da90eeced5524e0b00ba93a4e5c385f0d6953bd389a52fcc97edeb4026dc1a33f7461e5656392a518150823988db4f501cda73e043052e9af4f92e
|
|
7
|
+
data.tar.gz: e293340375942735f79710b633830b33f930553d85bad581e3bbce2ea2eae4296698eee16d48ec1cecfc578478b556029793c3d7449d23a7b746cf7972c665c1
|
data/.rspec
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
--format documentation
|
|
2
|
-
--color
|
|
3
|
-
--require spec_helper
|
|
1
|
+
--format documentation
|
|
2
|
+
--color
|
|
3
|
+
--require spec_helper
|
data/.rubocop.yml
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
AllCops:
|
|
2
|
-
TargetRubyVersion: 3.1
|
|
3
|
-
|
|
4
|
-
Style/StringLiterals:
|
|
5
|
-
EnforcedStyle: double_quotes
|
|
6
|
-
|
|
7
|
-
Style/StringLiteralsInInterpolation:
|
|
8
|
-
EnforcedStyle: double_quotes
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 3.1
|
|
3
|
+
|
|
4
|
+
Style/StringLiterals:
|
|
5
|
+
EnforcedStyle: double_quotes
|
|
6
|
+
|
|
7
|
+
Style/StringLiteralsInInterpolation:
|
|
8
|
+
EnforcedStyle: double_quotes
|
data/LICENSE.txt
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
The MIT License (MIT)
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Graftcode
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Graftcode
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
# SimpleCar
|
|
2
|
-
|
|
3
|
-
A minimal Ruby class that represents a car and its running state.
|
|
4
|
-
|
|
5
|
-
## What it provides
|
|
6
|
-
|
|
7
|
-
- Auto-generated UUID `id` (or custom `id`)
|
|
8
|
-
- Car metadata: `make`, `model`, `year`
|
|
9
|
-
- Engine state control: `start`, `stop`, `is_running`
|
|
10
|
-
- String representation via `to_s`
|
|
11
|
-
|
|
12
|
-
## Requirements
|
|
13
|
-
|
|
14
|
-
- Ruby 3.1+
|
|
15
|
-
|
|
16
|
-
## Import
|
|
17
|
-
|
|
18
|
-
Use one of the following approaches:
|
|
19
|
-
|
|
20
|
-
```ruby
|
|
21
|
-
require "simple_car"
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
or, when loading directly from the repository:
|
|
25
|
-
|
|
26
|
-
```ruby
|
|
27
|
-
require_relative "lib/simple_car"
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
## Quick Start
|
|
31
|
-
|
|
32
|
-
```ruby
|
|
33
|
-
require "simple_car"
|
|
34
|
-
|
|
35
|
-
car = SimpleCar.new("Toyota", "Corolla", 2020)
|
|
36
|
-
|
|
37
|
-
puts car.id # generated UUID
|
|
38
|
-
puts car.make # "Toyota"
|
|
39
|
-
puts car.model # "Corolla"
|
|
40
|
-
puts car.year # 2020
|
|
41
|
-
puts car.wheels # 4
|
|
42
|
-
puts car.is_running # false
|
|
43
|
-
|
|
44
|
-
car.start
|
|
45
|
-
puts car.is_running # true
|
|
46
|
-
|
|
47
|
-
car.stop
|
|
48
|
-
puts car.is_running # false
|
|
49
|
-
|
|
50
|
-
puts car.to_s
|
|
51
|
-
# => "<id> 2020 Toyota Corolla (SimpleCar) Not Running"
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
## Custom ID
|
|
55
|
-
|
|
56
|
-
```ruby
|
|
57
|
-
car = SimpleCar.new("Ford", "Mustang", 1968, "car-001")
|
|
58
|
-
puts car.id # "car-001"
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
If `id` is `nil` or empty, `SimpleCar` generates a UUID automatically.
|
|
62
|
-
|
|
63
|
-
## API Reference
|
|
64
|
-
|
|
65
|
-
- `SimpleCar.new(make, model, year, id = nil)`
|
|
66
|
-
- `id` -> `String`
|
|
67
|
-
- `make` -> `String`
|
|
68
|
-
- `model` -> `String`
|
|
69
|
-
- `year` -> `Integer` (or any value passed in)
|
|
70
|
-
- `wheels` -> `4`
|
|
71
|
-
- `is_running` -> `true` / `false`
|
|
72
|
-
- `start` -> sets running state to `true`
|
|
73
|
-
- `stop` -> sets running state to `false`
|
|
1
|
+
# SimpleCar
|
|
2
|
+
|
|
3
|
+
A minimal Ruby class that represents a car and its running state.
|
|
4
|
+
|
|
5
|
+
## What it provides
|
|
6
|
+
|
|
7
|
+
- Auto-generated UUID `id` (or custom `id`)
|
|
8
|
+
- Car metadata: `make`, `model`, `year`
|
|
9
|
+
- Engine state control: `start`, `stop`, `is_running`
|
|
10
|
+
- String representation via `to_s`
|
|
11
|
+
|
|
12
|
+
## Requirements
|
|
13
|
+
|
|
14
|
+
- Ruby 3.1+
|
|
15
|
+
|
|
16
|
+
## Import
|
|
17
|
+
|
|
18
|
+
Use one of the following approaches:
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
require "simple_car"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
or, when loading directly from the repository:
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
require_relative "lib/simple_car"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
require "simple_car"
|
|
34
|
+
|
|
35
|
+
car = SimpleCar.new("Toyota", "Corolla", 2020)
|
|
36
|
+
|
|
37
|
+
puts car.id # generated UUID
|
|
38
|
+
puts car.make # "Toyota"
|
|
39
|
+
puts car.model # "Corolla"
|
|
40
|
+
puts car.year # 2020
|
|
41
|
+
puts car.wheels # 4
|
|
42
|
+
puts car.is_running # false
|
|
43
|
+
|
|
44
|
+
car.start
|
|
45
|
+
puts car.is_running # true
|
|
46
|
+
|
|
47
|
+
car.stop
|
|
48
|
+
puts car.is_running # false
|
|
49
|
+
|
|
50
|
+
puts car.to_s
|
|
51
|
+
# => "<id> 2020 Toyota Corolla (SimpleCar) Not Running"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Custom ID
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
car = SimpleCar.new("Ford", "Mustang", 1968, "car-001")
|
|
58
|
+
puts car.id # "car-001"
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
If `id` is `nil` or empty, `SimpleCar` generates a UUID automatically.
|
|
62
|
+
|
|
63
|
+
## API Reference
|
|
64
|
+
|
|
65
|
+
- `SimpleCar.new(make, model, year, id = nil)`
|
|
66
|
+
- `id` -> `String`
|
|
67
|
+
- `make` -> `String`
|
|
68
|
+
- `model` -> `String`
|
|
69
|
+
- `year` -> `Integer` (or any value passed in)
|
|
70
|
+
- `wheels` -> `4`
|
|
71
|
+
- `is_running` -> `true` / `false`
|
|
72
|
+
- `start` -> sets running state to `true`
|
|
73
|
+
- `stop` -> sets running state to `false`
|
|
74
74
|
- `to_s` -> `"<id> <year> <make> <model> (SimpleCar) <Running|Not Running>"`
|
data/Rakefile
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "bundler/gem_tasks"
|
|
4
|
-
require "rspec/core/rake_task"
|
|
5
|
-
|
|
6
|
-
RSpec::Core::RakeTask.new(:spec)
|
|
7
|
-
|
|
8
|
-
require "rubocop/rake_task"
|
|
9
|
-
|
|
10
|
-
RuboCop::RakeTask.new
|
|
11
|
-
|
|
12
|
-
task default: %i[spec rubocop]
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rspec/core/rake_task"
|
|
5
|
+
|
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
7
|
+
|
|
8
|
+
require "rubocop/rake_task"
|
|
9
|
+
|
|
10
|
+
RuboCop::RakeTask.new
|
|
11
|
+
|
|
12
|
+
task default: %i[spec rubocop]
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative "lib/grft_test_simple_car/version"
|
|
4
|
-
|
|
5
|
-
Gem::Specification.new do |spec|
|
|
6
|
-
spec.name = "sdn_test_simple_car"
|
|
7
|
-
spec.version = GrftTestSimpleCar::VERSION
|
|
8
|
-
spec.authors = ["GraftCode"]
|
|
9
|
-
spec.email = ["info@graftcode.com"]
|
|
10
|
-
|
|
11
|
-
spec.summary = "A minimal SimpleCar model with runtime state management."
|
|
12
|
-
spec.description = "Provides a lightweight SimpleCar class with make/model/year data, auto-generated IDs, engine start/stop state, and a readable string representation."
|
|
13
|
-
spec.homepage = "https://graftcode.com"
|
|
14
|
-
spec.license = "MIT"
|
|
15
|
-
spec.required_ruby_version = ">= 3.1.0"
|
|
16
|
-
|
|
17
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
|
18
|
-
|
|
19
|
-
# Specify which files should be added to the gem when it is released.
|
|
20
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
21
|
-
gemspec = File.basename(__FILE__)
|
|
22
|
-
spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls|
|
|
23
|
-
ls.readlines("\x0", chomp: true).reject do |f|
|
|
24
|
-
f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
spec.bindir = "exe"
|
|
28
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
29
|
-
spec.require_paths = ["lib"]
|
|
30
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/grft_test_simple_car/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "sdn_test_simple_car"
|
|
7
|
+
spec.version = GrftTestSimpleCar::VERSION
|
|
8
|
+
spec.authors = ["GraftCode"]
|
|
9
|
+
spec.email = ["info@graftcode.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "A minimal SimpleCar model with runtime state management."
|
|
12
|
+
spec.description = "Provides a lightweight SimpleCar class with make/model/year data, auto-generated IDs, engine start/stop state, and a readable string representation."
|
|
13
|
+
spec.homepage = "https://graftcode.com"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
spec.required_ruby_version = ">= 3.1.0"
|
|
16
|
+
|
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
18
|
+
|
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
21
|
+
gemspec = File.basename(__FILE__)
|
|
22
|
+
spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls|
|
|
23
|
+
ls.readlines("\x0", chomp: true).reject do |f|
|
|
24
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
spec.bindir = "exe"
|
|
28
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
29
|
+
spec.require_paths = ["lib"]
|
|
30
|
+
end
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module GrftTestSimpleCar
|
|
4
|
-
VERSION = "0.2.
|
|
5
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GrftTestSimpleCar
|
|
4
|
+
VERSION = "0.2.2"
|
|
5
|
+
end
|
data/lib/simple_car.rb
CHANGED
|
@@ -1,107 +1,192 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "securerandom"
|
|
4
|
-
require_relative "vehicle"
|
|
5
|
-
|
|
6
|
-
# Represents a simplified car that uses a string identifier instead of a GUID.
|
|
7
|
-
# Includes the Vehicle module and covers:
|
|
8
|
-
# primitive fields, static members, constructors, primitive-array I/O,
|
|
9
|
-
# and methods that accept / return complex types from the same library.
|
|
10
|
-
class SimpleCar
|
|
11
|
-
include Vehicle
|
|
12
|
-
|
|
13
|
-
# ── static members
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
@
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
#
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
#
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def
|
|
73
|
-
@
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
@
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
#
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require_relative "vehicle"
|
|
5
|
+
|
|
6
|
+
# Represents a simplified car that uses a string identifier instead of a GUID.
|
|
7
|
+
# Includes the {Vehicle} module and covers:
|
|
8
|
+
# primitive fields, static members, constructors, primitive-array I/O,
|
|
9
|
+
# and methods that accept / return complex types from the same library.
|
|
10
|
+
class SimpleCar
|
|
11
|
+
include Vehicle
|
|
12
|
+
|
|
13
|
+
# ── static members ──────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
# Default number of wheels for a SimpleCar.
|
|
16
|
+
DEFAULT_WHEELS = 4
|
|
17
|
+
|
|
18
|
+
@total_cars_created = 0
|
|
19
|
+
|
|
20
|
+
class << self
|
|
21
|
+
# Returns how many SimpleCar instances have been constructed.
|
|
22
|
+
#
|
|
23
|
+
# The counter increments on every successful call to #initialize and is shared
|
|
24
|
+
# process-wide for the loaded module.
|
|
25
|
+
#
|
|
26
|
+
# @return [Integer] Total number of cars created since import.
|
|
27
|
+
# @author graftcode
|
|
28
|
+
# @since 0.2.1
|
|
29
|
+
attr_reader :total_cars_created
|
|
30
|
+
|
|
31
|
+
# Creates a car with placeholder make, model, and the current year.
|
|
32
|
+
#
|
|
33
|
+
# Useful for demos and graft smoke tests where explicit vehicle details are not important.
|
|
34
|
+
#
|
|
35
|
+
# @return [SimpleCar] A new car using DefaultMake / DefaultModel / 2024.
|
|
36
|
+
# @example
|
|
37
|
+
# car = SimpleCar.create_default
|
|
38
|
+
# car.make #=> "DefaultMake"
|
|
39
|
+
def create_default
|
|
40
|
+
new("DefaultMake", "DefaultModel", 2024)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Returns the list of default makes used in sample data.
|
|
44
|
+
#
|
|
45
|
+
# @return [Array<String>] Array of make names.
|
|
46
|
+
def get_default_makes
|
|
47
|
+
%w[Toyota Tesla Ford Honda Chevrolet]
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# ── instance accessors ──────────────────────────────────────
|
|
52
|
+
|
|
53
|
+
# @!attribute [r] id
|
|
54
|
+
# @return [String] The string identifier for the car.
|
|
55
|
+
# @!attribute [r] make
|
|
56
|
+
# @return [String] The make of the car.
|
|
57
|
+
# @!attribute [r] model
|
|
58
|
+
# @return [String] The model of the car.
|
|
59
|
+
# @!attribute [r] year
|
|
60
|
+
# @return [Integer] The year the car was made.
|
|
61
|
+
attr_reader :id, :make, :model, :year
|
|
62
|
+
|
|
63
|
+
# Initializes a new car with identity fields and default runtime state.
|
|
64
|
+
#
|
|
65
|
+
# When id is omitted or blank, a UUID string is generated automatically.
|
|
66
|
+
#
|
|
67
|
+
# @param make [String] Vehicle manufacturer, for example Toyota.
|
|
68
|
+
# @param model [String] Vehicle model name, for example Corolla.
|
|
69
|
+
# @param year [Integer] Four-digit production year.
|
|
70
|
+
# @param id [String, nil] Optional stable identifier; a UUID is assigned when empty.
|
|
71
|
+
# @raise [ArgumentError] If year is before 1886 or make/model are blank.
|
|
72
|
+
def initialize(make, model, year, id = nil)
|
|
73
|
+
@id = id.nil? || id.strip.empty? ? SecureRandom.uuid : id
|
|
74
|
+
@make = make
|
|
75
|
+
@model = model
|
|
76
|
+
@year = year
|
|
77
|
+
@is_running = false
|
|
78
|
+
@mileage_history = []
|
|
79
|
+
@tags = []
|
|
80
|
+
self.class.instance_variable_set(:@total_cars_created, self.class.total_cars_created + 1)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# ── properties (Vehicle) ────────────────────────────────────
|
|
84
|
+
|
|
85
|
+
# Gets the number of wheels on the car.
|
|
86
|
+
#
|
|
87
|
+
# @return [Integer] The number of wheels (always 4 for this sample type).
|
|
88
|
+
def wheels
|
|
89
|
+
DEFAULT_WHEELS
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Gets a value indicating whether the car is currently running (started).
|
|
93
|
+
#
|
|
94
|
+
# @return [Boolean] true if the car is running; false otherwise.
|
|
95
|
+
def is_running
|
|
96
|
+
@is_running
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# ── engine control (Vehicle) ────────────────────────────────
|
|
100
|
+
|
|
101
|
+
# Starts the engine and marks the car as running.
|
|
102
|
+
#
|
|
103
|
+
# Calling this method twice in a row leaves the car running; it does not raise when already started.
|
|
104
|
+
#
|
|
105
|
+
# @raise [RuntimeError] Reserved for future guard rails in stricter implementations.
|
|
106
|
+
# @return [void]
|
|
107
|
+
def start
|
|
108
|
+
@is_running = true
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Stops the engine and marks the car as not running.
|
|
112
|
+
#
|
|
113
|
+
# Idempotent: stopping an already stopped car is allowed.
|
|
114
|
+
#
|
|
115
|
+
# @return [void]
|
|
116
|
+
def stop
|
|
117
|
+
@is_running = false
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# ── primitive-array methods ─────────────────────────────────
|
|
121
|
+
|
|
122
|
+
# Appends a mileage reading to the in-memory history.
|
|
123
|
+
#
|
|
124
|
+
# @param miles [Integer] Non-negative distance traveled since the previous entry.
|
|
125
|
+
# @raise [ArgumentError] If miles is negative.
|
|
126
|
+
# @return [void]
|
|
127
|
+
def add_mileage_entry(miles)
|
|
128
|
+
@mileage_history << miles
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Returns the recorded mileage history for the car.
|
|
132
|
+
#
|
|
133
|
+
# @return [Array<Integer>] Mileage entries in insertion order.
|
|
134
|
+
def get_mileage_history
|
|
135
|
+
@mileage_history.dup
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Replaces the car's tags with the given list.
|
|
139
|
+
#
|
|
140
|
+
# @param tags [Array<String>, nil] Tags to assign; nil clears the list.
|
|
141
|
+
# @return [void]
|
|
142
|
+
def set_tags(tags)
|
|
143
|
+
@tags = tags ? tags.dup : []
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Returns a defensive copy of the current tags.
|
|
147
|
+
#
|
|
148
|
+
# @deprecated Prefer reading tags from domain-specific repositories in new code.
|
|
149
|
+
# @return [Array<String>] Tag values currently stored on the instance.
|
|
150
|
+
def get_tags
|
|
151
|
+
@tags.dup
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# ── complex-type methods ────────────────────────────────────
|
|
155
|
+
|
|
156
|
+
# Returns a deep copy of this car, including runtime collections.
|
|
157
|
+
#
|
|
158
|
+
# The duplicate shares the same id and field values as the source instance.
|
|
159
|
+
#
|
|
160
|
+
# @return [SimpleCar] Independent instance with identical state.
|
|
161
|
+
# @see #with_year
|
|
162
|
+
def duplicate
|
|
163
|
+
copy = SimpleCar.new(@make, @model, @year, @id)
|
|
164
|
+
copy.instance_variable_set(:@is_running, @is_running)
|
|
165
|
+
copy.instance_variable_set(:@mileage_history, @mileage_history.dup)
|
|
166
|
+
copy.instance_variable_set(:@tags, @tags.dup)
|
|
167
|
+
copy
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Return a copy with an updated model year.
|
|
171
|
+
#
|
|
172
|
+
# @param new_year [Integer] Replacement production year.
|
|
173
|
+
# @return [SimpleCar] New instance sharing identity fields with this car.
|
|
174
|
+
# @raise [ArgumentError] If new_year is before 1886.
|
|
175
|
+
# @see #duplicate
|
|
176
|
+
def with_year(new_year)
|
|
177
|
+
copy = SimpleCar.new(@make, @model, new_year, @id)
|
|
178
|
+
copy.instance_variable_set(:@is_running, @is_running)
|
|
179
|
+
copy.instance_variable_set(:@mileage_history, @mileage_history.dup)
|
|
180
|
+
copy.instance_variable_set(:@tags, @tags.dup)
|
|
181
|
+
copy
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# ── object overrides ────────────────────────────────────────
|
|
185
|
+
|
|
186
|
+
# Returns a string that represents the current car.
|
|
187
|
+
#
|
|
188
|
+
# @return [String] A string containing the id, year, make, model and type ("SimpleCar"), plus the running state.
|
|
189
|
+
def to_s
|
|
190
|
+
"#{@id} #{@year} #{@make} #{@model} (SimpleCar) #{@is_running ? "Running" : "Not Running"}"
|
|
191
|
+
end
|
|
192
|
+
end
|
data/lib/vehicle.rb
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# Contract (mixin) for any vehicle with basic identity, state, and engine control.
|
|
4
|
-
#
|
|
5
|
-
# Including classes must provide: id, make, model, year, wheels, is_running, start, stop.
|
|
6
|
-
module Vehicle
|
|
7
|
-
def id
|
|
8
|
-
raise NotImplementedError
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
def make
|
|
12
|
-
raise NotImplementedError
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def model
|
|
16
|
-
raise NotImplementedError
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def year
|
|
20
|
-
raise NotImplementedError
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def wheels
|
|
24
|
-
raise NotImplementedError
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def is_running
|
|
28
|
-
raise NotImplementedError
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def start
|
|
32
|
-
raise NotImplementedError
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def stop
|
|
36
|
-
raise NotImplementedError
|
|
37
|
-
end
|
|
38
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Contract (mixin) for any vehicle with basic identity, state, and engine control.
|
|
4
|
+
#
|
|
5
|
+
# Including classes must provide: id, make, model, year, wheels, is_running, start, stop.
|
|
6
|
+
module Vehicle
|
|
7
|
+
def id
|
|
8
|
+
raise NotImplementedError
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def make
|
|
12
|
+
raise NotImplementedError
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def model
|
|
16
|
+
raise NotImplementedError
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def year
|
|
20
|
+
raise NotImplementedError
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def wheels
|
|
24
|
+
raise NotImplementedError
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def is_running
|
|
28
|
+
raise NotImplementedError
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def start
|
|
32
|
+
raise NotImplementedError
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def stop
|
|
36
|
+
raise NotImplementedError
|
|
37
|
+
end
|
|
38
|
+
end
|