Class: Shiritori::Main
Constant Summary
collapse
- RED =
31
- GREEN =
32
- EXIT_PATTERN =
/(exit|quit)/.freeze
- METHOD_PATTERN =
/[\w|\?|\>|\<|\=|\!|\[|\[|\]|\*|\/|\+|\-|\^|\~|\@|\%|\&|]+/.freeze
Constants included
from View
View::PADDING
SearchMethod::UNUSE_METHOD
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from View
#new_line, #show_status
#get_all_method, #scan_method, #singleton
Instance Attribute Details
#chain_count ⇒ Object
Returns the value of attribute chain_count.
6
7
8
|
# File 'lib/shiritori/shiritori.rb', line 6
def chain_count
@chain_count
end
|
#current_object ⇒ Object
Returns the value of attribute current_object.
6
7
8
|
# File 'lib/shiritori/shiritori.rb', line 6
def current_object
@current_object
end
|
Instance Method Details
#exec_method_chain(command, object) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/shiritori/shiritori.rb', line 117
def exec_method_chain(command, object)
method_name = command.scan(METHOD_PATTERN).first.to_sym
result = [ method_name ]
case command
when EXIT_PATTERN
return [:exit]
else
begin
Thread.new do
raise NoMethodError unless object.respond_to?(method_name)
result << object.instance_eval{ eval("self."+command) }
end.join
rescue Exception => ex
puts ex.message
return false
end
end
result
end
|
#get_command ⇒ Object
68
69
70
71
72
73
74
75
|
# File 'lib/shiritori/shiritori.rb', line 68
def get_command
if Shiritori::ENV == :development
print "Please input first object > "
$stdin.gets
else
Readline.readline("Please input first object > ", true)
end
end
|
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
|
# File 'lib/shiritori/shiritori.rb', line 37
def init
@all_method = get_all_method
@current_object = nil
@current_class = Object
@current_chain = []
@chain_count = 0
@success = false
loop do
command = get_command
begin
@current_object = eval(command.chomp)
@current_chain << @current_object.to_ss
@success = true
break
rescue Exception => ex
new_line
puts "\e[#{RED}mUndefined object error\e[m"
redo
end
end
update
end
|
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
108
109
110
111
112
113
114
115
|
# File 'lib/shiritori/shiritori.rb', line 77
def run
loop do
show_status
if success?
puts "\e[#{GREEN}mSuccess!\e[m"
@success = false
else
puts "\e[#{RED}mFailed!\e[m"
end
new_line
command = get_command
break if command.nil?
redo if command.blank?
command = command.chomp.sub(/^\./,"")
puts "Exec command #{[@current_object.to_ss, command].join('.')}"
begin
if result = exec_method_chain(command, @current_object)
if @all_method.include?(result.first)
update(result)
@current_chain << command
elsif result.first == :exit
break
else
puts "Already used method."
end
end
rescue Exception => ex
puts "\e[#{RED}m#{ex.message}\e[m"
end
end
end
|
#start(option = []) ⇒ Object
16
17
18
19
|
# File 'lib/shiritori/shiritori.rb', line 16
def start(option = [])
init
run
end
|
#success? ⇒ Boolean
64
65
66
|
# File 'lib/shiritori/shiritori.rb', line 64
def success?
@success
end
|
#update(result = nil) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/shiritori/shiritori.rb', line 21
def update(result = nil)
if result
@all_method.delete(result.first)
@current_object = result.last
@chain_count += 1
end
begin
@current_class = @current_object.class
rescue Exception => ex
@current_class = "Undefined"
end
@success = true
end
|