Class: Shiritori::Main

Inherits:
Object show all
Includes:
SearchMethod, View
Defined in:
lib/shiritori/shiritori.rb

Constant Summary collapse

RED =

ColorCode

31
GREEN =
32
EXIT_PATTERN =
/\A(exit|quit)\Z/.freeze
HELP_PATTERN =
/\A(help|h)\Z/.freeze
METHOD_PATTERN =
/[\w|\?|\>|\<|\=|\!|\[|\[|\]|\*|\/|\+|\-|\^|\~|\@|\%|\&|]+/.freeze
TIME_LIMIT =

timelimit is 1 second

1

Constants included from View

View::PADDING

Constants included from SearchMethod

SearchMethod::UNUSE_METHOD

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from View

#failed_message, #input_message, #new_line, #show_result, #show_status, #welcome_message

Methods included from SearchMethod

#get_all_method, #scan_method

Instance Attribute Details

#chain_countObject (readonly)

Returns the value of attribute chain_count.



6
7
8
# File 'lib/shiritori/shiritori.rb', line 6

def chain_count
  @chain_count
end

#current_objectObject (readonly)

Returns the value of attribute current_object.



6
7
8
# File 'lib/shiritori/shiritori.rb', line 6

def current_object
  @current_object
end

#error_countObject (readonly)

Returns the value of attribute error_count.



6
7
8
# File 'lib/shiritori/shiritori.rb', line 6

def error_count
  @error_count
end

#modeObject (readonly)

Returns the value of attribute mode.



6
7
8
# File 'lib/shiritori/shiritori.rb', line 6

def mode
  @mode
end

#use_method_listObject (readonly)

Returns the value of attribute use_method_list.



6
7
8
# File 'lib/shiritori/shiritori.rb', line 6

def use_method_list
  @use_method_list
end

Instance Method Details

#check_successObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/shiritori/shiritori.rb', line 89

def check_success
  if @success
    puts "\e[#{GREEN}mSuccess!\e[m"
    @success = false
  else
    puts "\e[#{RED}mFailed! : #{$error_message}\e[m"
  end

  new_line
end

#exec_method_chain(command, object) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/shiritori/shiritori.rb', line 148

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]
  when HELP_PATTERN
    return [:help]
  else
    begin
      Thread.new do
        raise NoMethodError unless object.respond_to?(method_name)

        timeout(TIME_LIMIT) do
          result << eval('object.' + command)
        end
      end.join
    rescue Exception => ex
      @error_count += 1
      $error_message = ex.message
      failed_message("Exec command #{[@current_object.to_ss, command].join('.')}")
      failed_message("Failed! : #{$error_message}")
      return false
    end
  end

  result
end

#get_command(message = 'Please input first object > ') ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/shiritori/shiritori.rb', line 100

def get_command(message = 'Please input first object > ')
  if Shiritori.env == :development
    print message
    $stdin.gets
  else
    Readline.readline(message, true)
  end
end

#init(mode) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/shiritori/shiritori.rb', line 52

def init(mode)
  @all_method = get_all_method
  @current_class = Object
  @current_chain = []
  @use_method_list = []
  @chain_count = 0
  @error_count = 0
  @success = false
  @mode = mode

  require 'timeout'
  require 'ripper'

  loop do
    command = get_command
    redo if command.empty?

    begin
      Thread.new do
        timeout(TIME_LIMIT) do
          @current_object = eval(command.chomp)
        end
      end.join

      @current_chain << @current_object.to_ss
      @success = true
      break
    rescue Exception => ex
      $error_message = ex.message
      puts "\e[#{RED}m#{$error_message}\e[m"
      redo
    end
  end

  update
end

#runObject



109
110
111
112
113
114
115
116
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
142
143
144
145
146
# File 'lib/shiritori/shiritori.rb', line 109

def run
  loop do
    command = get_command('Please input next method > ')

    break if command.nil? 
    redo if command.blank?

    command = command.chomp.sub(/^\./, '')

    begin
      result = exec_method_chain(command, current_object)
      
      redo unless result

      action = result.first
      object = result.last

      if action == :help
        help_me(current_object)
        redo
      elsif action == :exit
        break
      elsif @all_method.include?(action)
        puts "Exec command #{[@current_object.to_ss, command].join('.')}"
        @current_chain << command
        update(action: action, object: object)
      else
        $error_message = "#{action} is already used."
        raise Shiritori::UseSameMethodError
      end
    rescue Exception => ex
      @error_count += 1
      puts "\e[#{RED}m#{ex.message}\e[m"
    end

    check_success
  end
end

#start(mode: :normal) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/shiritori/shiritori.rb', line 21

def start(mode: :normal)
  welcome_message

  begin
    init(mode)
    run
  rescue Exception => ex
    puts ex.message
  ensure
    show_result
  end
end

#update(action: nil, object: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/shiritori/shiritori.rb', line 34

def update(action: nil, object: nil)
  if action
    @all_method.delete(action)
    @use_method_list << action
    @current_object = object
    @chain_count += 1
  end

  begin
    @current_class = current_object.class
  rescue NoMethodError => ex
    @current_class = 'Undefined'
  end

  show_status(@current_chain, @current_object, @current_class, @chain_count)
  @success = true
end