Class: Eve::Messenger

Inherits:
Object
  • Object
show all
Defined in:
lib/eve/messenger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Messenger

Returns a new instance of Messenger.



7
8
9
10
11
12
13
# File 'lib/eve/messenger.rb', line 7

def initialize(data = {})
  @display = data[:display] || :stdio
  @piggy_backs = []
  @ok_filters = []
  @nok_filters = []
  reset
end

Instance Attribute Details

#displayObject

Returns the value of attribute display.



5
6
7
# File 'lib/eve/messenger.rb', line 5

def display
  @display
end

#history(flag = nil) ⇒ Object

Returns the value of attribute history.



5
6
7
# File 'lib/eve/messenger.rb', line 5

def history
  @history
end

#nok_filtersObject

Returns the value of attribute nok_filters.



5
6
7
# File 'lib/eve/messenger.rb', line 5

def nok_filters
  @nok_filters
end

#ok_filtersObject

Returns the value of attribute ok_filters.



5
6
7
# File 'lib/eve/messenger.rb', line 5

def ok_filters
  @ok_filters
end

#piggy_backsObject

Returns the value of attribute piggy_backs.



5
6
7
# File 'lib/eve/messenger.rb', line 5

def piggy_backs
  @piggy_backs
end

Instance Method Details

#add_filter(raw_input) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/eve/messenger.rb', line 15

def add_filter(raw_input)
  raw_input = "#{raw_input}"
  if raw_input.start_with?("!")
    @nok_filters << raw_input[1..-1]
  else
    @ok_filters << raw_input
  end
end

#event_machineObject



32
33
34
# File 'lib/eve/messenger.rb', line 32

def event_machine
  @display[:event_machine]
end

#event_machine?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/eve/messenger.rb', line 28

def event_machine?
  @display.kind_of?(Hash) && !@display[:event_machine].nil?
end

#file?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/eve/messenger.rb', line 36

def file?
  @display.kind_of?(Hash) && !@display[:filename].nil?
end

#has_flag?(flag) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/eve/messenger.rb', line 59

def has_flag?(flag)
  return !@flagged_history[flag].nil?
end

#is_data_only?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/eve/messenger.rb', line 44

def is_data_only?
  @display.kind_of?(Hash) && @display[:mode] == "data_only"
end

#overwrite_file?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/eve/messenger.rb', line 40

def overwrite_file?
  file? && @display[:mode] == :overwrite
end


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/eve/messenger.rb', line 90

def print(output, options = {})
  @piggy_backs.each { |m| m.print(output,options.clone) }
  return unless should_print?(options)
  add_history(output,options.delete(:flag))
  if @display == :stdio && options.empty?
    STDOUT.print output
  elsif @display == :stdio
    Kernel.ap output, options
  elsif event_machine?
    event_machine.send_data(output)
  elsif file?
    system("mkdir -p #{Pathname.new(@display[:filename]).parent}")
    open(@display[:filename], overwrite_file? ? "w" : "a+") { |f| f.puts(output) }
  else
    output
  end
end

#resetObject



48
49
50
51
52
# File 'lib/eve/messenger.rb', line 48

def reset
  @flagged_history = {}
  @history = []
  @piggy_backs.each { |m| m.reset }
end

#was_printed?(msg) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/eve/messenger.rb', line 63

def was_printed?(msg)
  @history.include?(msg)
end