OTB-test 0.1.1
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +43 -0
- data/LICENSE.txt +21 -0
- data/OTB-test-0.1.0.gem +0 -0
- data/OTB-test.gemspec +28 -0
- data/README.md +131 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exercise_speficiation.md +59 -0
- data/lib/OTB/exception.rb +16 -0
- data/lib/OTB/job.rb +31 -0
- data/lib/OTB/queue.rb +68 -0
- data/lib/OTB/version.rb +5 -0
- data/lib/otb.rb +12 -0
- metadata +121 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: cd3142710223bad92e3994da0491fb510acc0315
|
|
4
|
+
data.tar.gz: 597241e932c5283d1126cf243ae182e4548fb1d9
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: d4222b067a4ac9c312c3f33b77e8987a1293fd6d966ea32f8a24ce6109df9783ca72fafb36ed355b39b5f840da6fffd958dc59649f7f701bd4119208a931c717
|
|
7
|
+
data.tar.gz: c95cee4c677f795b653ab53d82c350d81e741e617c8aba032b49b3f8fc4c8d16428c7860a98c7738202e735693f79163626064fd4d12b069155885310b735a69
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at filipe.p@soltroiavillas.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
OTB-test (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
coderay (1.1.2)
|
|
10
|
+
diff-lcs (1.3)
|
|
11
|
+
method_source (0.8.2)
|
|
12
|
+
pry (0.10.4)
|
|
13
|
+
coderay (~> 1.1.0)
|
|
14
|
+
method_source (~> 0.8.1)
|
|
15
|
+
slop (~> 3.4)
|
|
16
|
+
rake (10.5.0)
|
|
17
|
+
rspec (3.8.0)
|
|
18
|
+
rspec-core (~> 3.8.0)
|
|
19
|
+
rspec-expectations (~> 3.8.0)
|
|
20
|
+
rspec-mocks (~> 3.8.0)
|
|
21
|
+
rspec-core (3.8.0)
|
|
22
|
+
rspec-support (~> 3.8.0)
|
|
23
|
+
rspec-expectations (3.8.2)
|
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
25
|
+
rspec-support (~> 3.8.0)
|
|
26
|
+
rspec-mocks (3.8.0)
|
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
28
|
+
rspec-support (~> 3.8.0)
|
|
29
|
+
rspec-support (3.8.0)
|
|
30
|
+
slop (3.6.0)
|
|
31
|
+
|
|
32
|
+
PLATFORMS
|
|
33
|
+
ruby
|
|
34
|
+
|
|
35
|
+
DEPENDENCIES
|
|
36
|
+
OTB-test!
|
|
37
|
+
bundler (~> 1.17)
|
|
38
|
+
pry (~> 0.10.4)
|
|
39
|
+
rake (~> 10.0)
|
|
40
|
+
rspec (~> 3.0)
|
|
41
|
+
|
|
42
|
+
BUNDLED WITH
|
|
43
|
+
1.17.2
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Filipe Paiva
|
|
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/OTB-test-0.1.0.gem
ADDED
|
Binary file
|
data/OTB-test.gemspec
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "OTB/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "OTB-test"
|
|
8
|
+
spec.version = OTB::Test::VERSION
|
|
9
|
+
spec.authors = ["Filipe Paiva"]
|
|
10
|
+
spec.email = ["filipe.p@soltroiavillas.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{OTB job queue algorithem}
|
|
13
|
+
spec.description = %q{This gem organizes jobs according to it's dependencies. It takes in a string of jobs sperated by a commas and organizes them from 'a' tp 'z' according the dependencies.}
|
|
14
|
+
spec.homepage = "https://github.com/Filipe-p/OTB-test"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
19
|
+
end
|
|
20
|
+
spec.bindir = "exe"
|
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
22
|
+
spec.require_paths = ["lib"]
|
|
23
|
+
|
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
27
|
+
spec.add_development_dependency "pry", "~> 0.10.4"
|
|
28
|
+
end
|
data/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# OTB Job Queuer
|
|
2
|
+
|
|
3
|
+
Welcome to your new gem from OTB! It is a job queuer and can be user on the terminal or a Gem in your projects.
|
|
4
|
+
The gem takes in a string in the format of:
|
|
5
|
+
```
|
|
6
|
+
a =>
|
|
7
|
+
b => c
|
|
8
|
+
c =>
|
|
9
|
+
```
|
|
10
|
+
or
|
|
11
|
+
```
|
|
12
|
+
a => \nb => \nc => c
|
|
13
|
+
```
|
|
14
|
+
And respondes with an ordered sequence of the jobs that takes into accoun the dependecies. For example in this case: `a, c, b`
|
|
15
|
+
|
|
16
|
+
Read usage to see how to call methods.
|
|
17
|
+
|
|
18
|
+
## OTB Job Queuer Structure
|
|
19
|
+
|
|
20
|
+
Most of the code is in one of two places:
|
|
21
|
+
`lib/OTB/`
|
|
22
|
+
or
|
|
23
|
+
`spec/OTB/`
|
|
24
|
+
|
|
25
|
+
In the 'lib/' you will find the methods and loggic that parses jobs and sorts queues based on their dependencies.
|
|
26
|
+
In the 'spec/' you will find the tests make sure everything is running correctly.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
Add this line to your application's Gemfile:
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
gem 'OTB-test'
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
And then execute:
|
|
37
|
+
|
|
38
|
+
$ bundle
|
|
39
|
+
|
|
40
|
+
Or install it yourself as:
|
|
41
|
+
|
|
42
|
+
$ gem install OTB-test
|
|
43
|
+
|
|
44
|
+
## Loading to terminal
|
|
45
|
+
|
|
46
|
+
From terminal open your ruby console:
|
|
47
|
+
|
|
48
|
+
$ irb
|
|
49
|
+
|
|
50
|
+
Then load the file `lib/otb.rb`:
|
|
51
|
+
|
|
52
|
+
$ pry(main)> load 'lib/otb.rb'
|
|
53
|
+
|
|
54
|
+
It should return `true`.
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
The usage of the gem methods is the same in terminal or in project. Check above how to load to terminal.
|
|
59
|
+
|
|
60
|
+
You can also run the tests to see everything is passing! Skip to section ##Test to do this.
|
|
61
|
+
|
|
62
|
+
###Queue Class Usage
|
|
63
|
+
The purpose of this class is to create queues and then sort them.
|
|
64
|
+
As such there are two public methods:
|
|
65
|
+
- Initialization with `new`
|
|
66
|
+
and
|
|
67
|
+
- sorting according to dependecies `sort_sequence` on a Queue object
|
|
68
|
+
|
|
69
|
+
####Creating a new Queue
|
|
70
|
+
```
|
|
71
|
+
OTB::Queue.new(string: 'a =>')
|
|
72
|
+
```
|
|
73
|
+
returns:
|
|
74
|
+
```
|
|
75
|
+
=> #<OTB::Queue:0x00007f8433d04260 @jobs="a =>", @jobs_parsed={"a"=>""}>
|
|
76
|
+
```
|
|
77
|
+
An OTB::Queue with @jobs and @jobs_parsed available
|
|
78
|
+
|
|
79
|
+
####Sorting a Queue
|
|
80
|
+
```
|
|
81
|
+
new_queue = OTB::Queue.new(string: 'a =>')
|
|
82
|
+
new_queue.sort_sequence
|
|
83
|
+
```
|
|
84
|
+
returns:
|
|
85
|
+
```
|
|
86
|
+
=> "a"
|
|
87
|
+
```
|
|
88
|
+
A string witht the sequence of the jobs ordered.
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
###Job Class - Currently only parse implemented
|
|
92
|
+
Job Class exists to parse strings of jobs into Hashes of jobs with dependencies.
|
|
93
|
+
Further releases will allow you to store individial job objects and create several from a string.
|
|
94
|
+
####Parsing jobs
|
|
95
|
+
```
|
|
96
|
+
OTB::Job.parse('a => \nb => \nc =>')
|
|
97
|
+
```
|
|
98
|
+
returns:
|
|
99
|
+
```
|
|
100
|
+
=> {"a"=>"", "b"=>"", "c"=>""}
|
|
101
|
+
```
|
|
102
|
+
A Hash with keys being the jobs and values the dependencies.
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
## Testes
|
|
106
|
+
|
|
107
|
+
There are 17 test.
|
|
108
|
+
- 8 testing the Job class, mostly the `parse` method.
|
|
109
|
+
- 9 testing the Queue class, checking that exections are raised and that the queuer
|
|
110
|
+
|
|
111
|
+
Inside the OTB-test directory execute:
|
|
112
|
+
|
|
113
|
+
$ rake spec
|
|
114
|
+
|
|
115
|
+
## Development
|
|
116
|
+
|
|
117
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
118
|
+
|
|
119
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
120
|
+
|
|
121
|
+
## Contributing
|
|
122
|
+
|
|
123
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/OTB-test. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
124
|
+
|
|
125
|
+
## License
|
|
126
|
+
|
|
127
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
128
|
+
|
|
129
|
+
## Code of Conduct
|
|
130
|
+
|
|
131
|
+
Everyone interacting in the OTB::Test project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/OTB-test/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "OTB/test"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#On The Beach - Coding Exercise Specification
|
|
2
|
+
|
|
3
|
+
##Intro
|
|
4
|
+
The purpose of this exercise is to see how you approach a problem, and how you solve it. We’re interested to see how you structure your Ruby code, your command of the language and good design and testing principles, bear this in mind throughout.
|
|
5
|
+
HINT: Start with a method that accepts a single string argument and returns a string (or a collection) which represents the ordered sequence of jobs (since each job is a single character).
|
|
6
|
+
HINT: Brownie points will be given for showing us your working (notes, commit history, some kind of idea how you approached the problem).
|
|
7
|
+
HINT: We’re pretty keen on tested code. Have Fun.
|
|
8
|
+
|
|
9
|
+
##The Challenge
|
|
10
|
+
Imagine we have a list of jobs, each represented by a character. Because certain jobs must be done before others, a job may have a
|
|
11
|
+
dependency on another job. For example, a may depend on b, meaning the final sequence of jobs should place b before a. If a has no
|
|
12
|
+
dependency, the position of a in the final sequence does not matter.
|
|
13
|
+
*Given you’re passed an empty string (no jobs), the result should be an empty sequence.
|
|
14
|
+
*Given the following job structure:
|
|
15
|
+
```
|
|
16
|
+
a =>
|
|
17
|
+
```
|
|
18
|
+
The result should be a sequence consisting of a single job a.
|
|
19
|
+
*Given the following job structure:
|
|
20
|
+
```
|
|
21
|
+
a =>
|
|
22
|
+
b =>
|
|
23
|
+
c =>
|
|
24
|
+
```
|
|
25
|
+
The result should be a sequence containing all three jobs abc in no significant order.
|
|
26
|
+
*Given the following job structure:
|
|
27
|
+
```
|
|
28
|
+
a =>
|
|
29
|
+
b => c
|
|
30
|
+
c =>
|
|
31
|
+
```
|
|
32
|
+
The result should be a sequence that positions c before b, containing all three jobs abc.
|
|
33
|
+
*Given the following job structure:
|
|
34
|
+
```
|
|
35
|
+
a =>
|
|
36
|
+
b => c
|
|
37
|
+
c => f
|
|
38
|
+
d => a
|
|
39
|
+
e => b
|
|
40
|
+
f =>
|
|
41
|
+
```
|
|
42
|
+
The result should be a sequence that positions f before c, c before b, b before e and a before d containing all six jobs abcdef.
|
|
43
|
+
*Given the following job structure:
|
|
44
|
+
```
|
|
45
|
+
a =>
|
|
46
|
+
b =>
|
|
47
|
+
c => c
|
|
48
|
+
```
|
|
49
|
+
The result should be an error stating that jobs can’t depend on themselves.
|
|
50
|
+
*Given the following job structure:
|
|
51
|
+
```
|
|
52
|
+
a =>
|
|
53
|
+
b => c
|
|
54
|
+
c => f
|
|
55
|
+
d => a
|
|
56
|
+
e =>
|
|
57
|
+
f => b
|
|
58
|
+
```
|
|
59
|
+
The result should be an error stating that jobs can’t have circular dependencies.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
class JobQueueError < StandardError;
|
|
2
|
+
|
|
3
|
+
def self.self_dependency_error
|
|
4
|
+
exception message: "Self dependency error - A job cannot depend on itself", internal_code: 4009, code:409
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def self.circular_dependecy_error
|
|
8
|
+
exception message: "Circular dependency error - Jobs cannot depen on themselfves circularly creating an infinate loop", internal_code: 4009, code:409
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def self.no_input_error
|
|
13
|
+
exception message: "No input passed. jobs: was nil" , internal_code: 2004, code: 204
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
end
|
data/lib/OTB/job.rb
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module OTB
|
|
2
|
+
class Job
|
|
3
|
+
|
|
4
|
+
attr_accessor :job
|
|
5
|
+
|
|
6
|
+
def initialize(job)
|
|
7
|
+
# Further version of the code might want to change the initialization and create several Job objects.
|
|
8
|
+
# These could then be saved in the DB as such or passed to the queuer.
|
|
9
|
+
@job = job
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.parse(jobs)
|
|
13
|
+
jobs_array = jobs.split('\n')
|
|
14
|
+
result_hash = {}
|
|
15
|
+
if jobs == ''
|
|
16
|
+
result_hash[''] =''
|
|
17
|
+
result_hash
|
|
18
|
+
else
|
|
19
|
+
array_job_dependencies_sanitized = jobs_array.each do |job_dependency|
|
|
20
|
+
array_job_dependency = []
|
|
21
|
+
array_job_dependency << job_dependency.split('=>')
|
|
22
|
+
array_job_dependency.each do |job, dependency|
|
|
23
|
+
if dependency.nil?; dependency = '' else dependency.strip! end
|
|
24
|
+
result_hash[job.strip] = dependency
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
result_hash
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
data/lib/OTB/queue.rb
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
module OTB
|
|
2
|
+
class Queue
|
|
3
|
+
attr_accessor :jobs, :jobs_parsed
|
|
4
|
+
|
|
5
|
+
#usign job:nil for stability & latter flexibity
|
|
6
|
+
# Idea being you could also have string:nil
|
|
7
|
+
# then you could initialized with Job objects in the jobs:
|
|
8
|
+
# or you could initialized with String objects in the string:
|
|
9
|
+
def initialize(string:nil)
|
|
10
|
+
@jobs = string
|
|
11
|
+
@jobs_parsed = OTB::Job.parse(@jobs) unless string.nil?
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def sort_sequence
|
|
15
|
+
case @jobs
|
|
16
|
+
when ''
|
|
17
|
+
''
|
|
18
|
+
when nil
|
|
19
|
+
raise JobQueueError.no_input_error
|
|
20
|
+
else
|
|
21
|
+
check_self_dependency(@jobs_parsed)
|
|
22
|
+
check_circular_dependency(@jobs_parsed)
|
|
23
|
+
sort_jobs_to_sequence(@jobs_parsed)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def check_self_dependency(jobs_parsed)
|
|
30
|
+
jobs_parsed.each do |job, dependency|
|
|
31
|
+
if job == dependency
|
|
32
|
+
raise JobQueueError.self_dependency_error
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def check_circular_dependency(jobs_parsed)
|
|
38
|
+
hash_jobs_with_depencecies = {}
|
|
39
|
+
|
|
40
|
+
jobs_parsed.each_with_index do |job_dependency, index|
|
|
41
|
+
[job_dependency].each do |job, dependency|
|
|
42
|
+
if hash_jobs_with_depencecies.keys.include?(dependency) && hash_jobs_with_depencecies.values.include?(job)
|
|
43
|
+
raise JobQueueError.circular_dependecy_error
|
|
44
|
+
end
|
|
45
|
+
hash_jobs_with_depencecies[job] = dependency unless dependency.empty?
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def sort_jobs_to_sequence(jobs_parsed)
|
|
51
|
+
sequence = []
|
|
52
|
+
|
|
53
|
+
jobs_parsed.each do |job, dependency|
|
|
54
|
+
if dependency.empty?
|
|
55
|
+
sequence << job unless sequence.include?(job)
|
|
56
|
+
elsif sequence.include?(job)
|
|
57
|
+
index = sequence.find_index(job)
|
|
58
|
+
sequence.insert(index, dependency)
|
|
59
|
+
else
|
|
60
|
+
sequence << dependency unless sequence.include?(dependency)
|
|
61
|
+
sequence << job unless sequence.include?(job)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
sequence.join(', ')
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
data/lib/OTB/version.rb
ADDED
data/lib/otb.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: OTB-test
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Filipe Paiva
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-01-07 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.17'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.17'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: pry
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 0.10.4
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 0.10.4
|
|
69
|
+
description: This gem organizes jobs according to it's dependencies. It takes in a
|
|
70
|
+
string of jobs sperated by a commas and organizes them from 'a' tp 'z' according
|
|
71
|
+
the dependencies.
|
|
72
|
+
email:
|
|
73
|
+
- filipe.p@soltroiavillas.com
|
|
74
|
+
executables: []
|
|
75
|
+
extensions: []
|
|
76
|
+
extra_rdoc_files: []
|
|
77
|
+
files:
|
|
78
|
+
- ".gitignore"
|
|
79
|
+
- ".rspec"
|
|
80
|
+
- ".travis.yml"
|
|
81
|
+
- CODE_OF_CONDUCT.md
|
|
82
|
+
- Gemfile
|
|
83
|
+
- Gemfile.lock
|
|
84
|
+
- LICENSE.txt
|
|
85
|
+
- OTB-test-0.1.0.gem
|
|
86
|
+
- OTB-test.gemspec
|
|
87
|
+
- README.md
|
|
88
|
+
- Rakefile
|
|
89
|
+
- bin/console
|
|
90
|
+
- bin/setup
|
|
91
|
+
- exercise_speficiation.md
|
|
92
|
+
- lib/OTB/exception.rb
|
|
93
|
+
- lib/OTB/job.rb
|
|
94
|
+
- lib/OTB/queue.rb
|
|
95
|
+
- lib/OTB/version.rb
|
|
96
|
+
- lib/otb.rb
|
|
97
|
+
homepage: https://github.com/Filipe-p/OTB-test
|
|
98
|
+
licenses:
|
|
99
|
+
- MIT
|
|
100
|
+
metadata: {}
|
|
101
|
+
post_install_message:
|
|
102
|
+
rdoc_options: []
|
|
103
|
+
require_paths:
|
|
104
|
+
- lib
|
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - ">="
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '0'
|
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
|
+
requirements:
|
|
112
|
+
- - ">="
|
|
113
|
+
- !ruby/object:Gem::Version
|
|
114
|
+
version: '0'
|
|
115
|
+
requirements: []
|
|
116
|
+
rubyforge_project:
|
|
117
|
+
rubygems_version: 2.6.14.1
|
|
118
|
+
signing_key:
|
|
119
|
+
specification_version: 4
|
|
120
|
+
summary: OTB job queue algorithem
|
|
121
|
+
test_files: []
|