Class: TChatter::Entry
- Inherits:
-
Object
- Object
- TChatter::Entry
- Defined in:
- lib/t_chatter/entry.rb
Instance Attribute Summary collapse
-
#chat ⇒ Object
readonly
Returns the value of attribute chat.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#threads ⇒ Object
readonly
Returns the value of attribute threads.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #commands ⇒ Object
- #control_exit_strategy ⇒ Object
- #help ⇒ Object
-
#initialize ⇒ Entry
constructor
A new instance of Entry.
- #peep ⇒ Object
- #quit ⇒ Object
- #send_messages ⇒ Object
- #setup_threads ⇒ Object
- #setup_user ⇒ Object
- #start_chat ⇒ Object
- #start_threads ⇒ Object
- #stream_messages ⇒ Object
- #substitute_variables(message) ⇒ Object
- #variables_subs ⇒ Object
- #want_to_receive? ⇒ Boolean
- #want_to_send? ⇒ Boolean
Constructor Details
#initialize ⇒ Entry
5 6 7 8 9 10 |
# File 'lib/t_chatter/entry.rb', line 5 def initialize @config = ConfigSetup.instance setup_user @chat = Chat.new(@user, @config) @threads = [] end |
Instance Attribute Details
#chat ⇒ Object (readonly)
Returns the value of attribute chat.
3 4 5 |
# File 'lib/t_chatter/entry.rb', line 3 def chat @chat end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/t_chatter/entry.rb', line 3 def config @config end |
#threads ⇒ Object (readonly)
Returns the value of attribute threads.
3 4 5 |
# File 'lib/t_chatter/entry.rb', line 3 def threads @threads end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
3 4 5 |
# File 'lib/t_chatter/entry.rb', line 3 def user @user end |
Instance Method Details
#commands ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/t_chatter/entry.rb', line 74 def commands { '#all_users' => :peep, '#help' => :help, '#quit' => :quit } end |
#control_exit_strategy ⇒ Object
96 97 98 99 100 |
# File 'lib/t_chatter/entry.rb', line 96 def control_exit_strategy trap('INT') do quit end end |
#help ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/t_chatter/entry.rb', line 57 def help var_help = variables_subs.keys.join(', ') command_help = commands.keys.join(', ') puts " Use the following variables in any message to refer to its inferred\n meaning in any message: \#{var_help}\n You can use any of these commands to also call the inferred action\n \#{command_help}\n EOS\nend\n" |
#peep ⇒ Object
53 54 55 |
# File 'lib/t_chatter/entry.rb', line 53 def peep chat.show_all_users end |
#quit ⇒ Object
68 69 70 71 72 |
# File 'lib/t_chatter/entry.rb', line 68 def quit puts "Gracefully shutting down the chat" chat.quit @threads.each(&:exit) end |
#send_messages ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/t_chatter/entry.rb', line 82 def Thread.new do loop do = gets.chomp.strip unless commands.keys.include? = substitute_variables() if .match(/@\w+/) chat.() unless .blank? else send(commands[]) end end end end |
#setup_threads ⇒ Object
102 103 104 105 |
# File 'lib/t_chatter/entry.rb', line 102 def setup_threads @threads << if want_to_receive? @threads << if want_to_send? end |
#setup_user ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/t_chatter/entry.rb', line 26 def setup_user @user = config.configuration[:user] unless @user print 'Would you mind telling me your name? : ' @user = gets.chomp end end |
#start_chat ⇒ Object
12 13 14 15 16 |
# File 'lib/t_chatter/entry.rb', line 12 def start_chat setup_threads control_exit_strategy start_threads end |
#start_threads ⇒ Object
107 108 109 |
# File 'lib/t_chatter/entry.rb', line 107 def start_threads @threads.each(&:join) end |
#stream_messages ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/t_chatter/entry.rb', line 34 def Thread.new do loop do chat. end end end |
#substitute_variables(message) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/t_chatter/entry.rb', line 46 def substitute_variables() variables_subs.each do |var, val| = .gsub(var, val) end end |
#variables_subs ⇒ Object
42 43 44 |
# File 'lib/t_chatter/entry.rb', line 42 def variables_subs { '@my_name' => chat.user.to_s, '@last_message' => chat..last.to_s, '@each_user' => chat.each_user } end |
#want_to_receive? ⇒ Boolean
22 23 24 |
# File 'lib/t_chatter/entry.rb', line 22 def want_to_receive? @config.configuration.blank? ? true : @config.configuration[:receive] end |
#want_to_send? ⇒ Boolean
18 19 20 |
# File 'lib/t_chatter/entry.rb', line 18 def want_to_send? @config.configuration.blank? ? true : @config.configuration[:send] end |