digimon_sleuth 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ module DigimonSleuth
2
+ class DigimonError < StandardError; end
3
+ class DigimonNotFoundError < DigimonError; end
4
+ end
@@ -0,0 +1,89 @@
1
+ module DigimonSleuth
2
+ class PathSearcher
3
+
4
+ def self.execute(line)
5
+ args = line.split(" -- ")
6
+ from, to = args.shift.gsub(/\s((to)|(To)|(tO))\s/, " TO ").split(" TO ")
7
+ opts = {}
8
+
9
+ args.each do |option|
10
+ opt, value = option.split(" ", 2)
11
+ case opt.upcase
12
+ when "DEPTH"
13
+ opts[:max_depth] = value.to_i
14
+ when "ALL"
15
+ opts[:all] = true
16
+ end
17
+ end
18
+
19
+ new(from, to, opts).call
20
+ end
21
+
22
+ def initialize(from, to, opts = {})
23
+ @from = Digimon.find(from)
24
+ @to = Digimon.find(to)
25
+
26
+ @mark = { from => true }
27
+ @max_depth = opts[:max_depth]
28
+ @allowed = opts[:allowed] || []
29
+ @ban = opts[:ban] || []
30
+ @all = opts[:all]
31
+ end
32
+
33
+ def call
34
+ result = []
35
+ depth = 1
36
+ queue = [[@from, @from.name]]
37
+ next_queue = []
38
+
39
+ while (value = queue.shift)
40
+ current, path = value
41
+
42
+ if current == to
43
+ result << path
44
+ else
45
+ mark[current.name] = true
46
+ end
47
+
48
+ traverse(next_queue, current.digivolve, path)
49
+ traverse(next_queue, current.dedigivolve, path)
50
+
51
+ unless queue.last
52
+ queue = next_queue
53
+ next_queue = []
54
+ depth += 1
55
+
56
+ return result if too_depth? || finish?(result)
57
+ end
58
+ end
59
+
60
+ return result
61
+ end
62
+
63
+ private
64
+
65
+ attr_reader :from, :to, :mark, :max_depth, :allowed, :ban, :all
66
+
67
+ def add(queue, digimon, path = "")
68
+ queue << [digimon, "#{path} -> #{digimon.name}"]
69
+ end
70
+
71
+ def allowed?(digimon)
72
+ !mark[digimon.name] && (allowed.empty? || allowed.include?(digimon.klass.downcase)) && (ban.empty? || !ban.include?(digimon.klass.downcase))
73
+ end
74
+
75
+ def too_depth?
76
+ max_depth && depth >= max_depth
77
+ end
78
+
79
+ def finish?(result)
80
+ !result.empty? && !all
81
+ end
82
+
83
+ def traverse(queue, options, path)
84
+ options.each do |option|
85
+ add(queue, option, path) if allowed?(option)
86
+ end
87
+ end
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: digimon_sleuth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Roland Rifandi Utama
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple gem
14
+ email: roland_hawk@yahoo.com
15
+ executables:
16
+ - digimon_sleuth
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".codeclimate.yml"
21
+ - ".editorconfig"
22
+ - ".gitignore"
23
+ - ".rubocop.yml"
24
+ - ".ruby-version"
25
+ - README.md
26
+ - bin/digimon_sleuth
27
+ - dat/digivolve.dat
28
+ - digimon_sleuth.gemspec
29
+ - lib/digimon_sleuth.rb
30
+ - lib/digimon_sleuth/digimon.rb
31
+ - lib/digimon_sleuth/errors.rb
32
+ - lib/digimon_sleuth/path_searcher.rb
33
+ homepage:
34
+ licenses: []
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.3.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.5.1
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Digimon Cyber Sleuth!
56
+ test_files: []