Class: Mmailer::ServerHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/mmailer/server_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServerHelper

Returns a new instance of ServerHelper.



5
6
7
8
9
10
11
12
# File 'lib/mmailer/server_helper.rb', line 5

def initialize
  @stream = $stdout
  self.machine = MicroMachine.new(:stopped)
  machine.when(:start, :stopped => :started)
  machine.when(:resume, :paused => :started)
  machine.when(:stop, :started => :stopped, :paused => :stopped)
  machine.when(:pause, :started => :paused)
end

Instance Attribute Details

#machineObject

Returns the value of attribute machine.



3
4
5
# File 'lib/mmailer/server_helper.rb', line 3

def machine
  @machine
end

#streamObject

Returns the value of attribute stream.



3
4
5
# File 'lib/mmailer/server_helper.rb', line 3

def stream
  @stream
end

#workerObject

Returns the value of attribute worker.



3
4
5
# File 'lib/mmailer/server_helper.rb', line 3

def worker
  @worker
end

Instance Method Details

#config(options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/mmailer/server_helper.rb', line 61

def config(options)
  if options.empty?
    puts "I will send emails every #{Mmailer.configuration.time_interval} seconds. After #{Mmailer.configuration.mail_interval} emails, I will sleep for #{Mmailer.configuration.sleep_time} seconds."
  else
    Mmailer.configuration.sleep_time = options.fetch("sleep_time", Mmailer.configuration.sleep_time)
    Mmailer.configuration.mail_interval = options.fetch("mail_interval", Mmailer.configuration.mail_interval)
    Mmailer.configuration.time_interval = options.fetch("time_interval", Mmailer.configuration.time_interval)
    puts "#{options}. OK."
  end
end

#display_stateObject



14
15
16
# File 'lib/mmailer/server_helper.rb', line 14

def display_state
  stream.puts state
end

#pauseObject



44
45
46
47
48
49
# File 'lib/mmailer/server_helper.rb', line 44

def pause
  if machine.trigger(:pause)
    display_state
    machine.state
  end
end

#puts(str) ⇒ Object



22
23
24
# File 'lib/mmailer/server_helper.rb', line 22

def puts(str)
  stream.puts(str)
end

#resumeObject



30
31
32
33
34
35
# File 'lib/mmailer/server_helper.rb', line 30

def resume
  if machine.trigger(:resume)
    display_state
    machine.state
  end
end

#start(from = 0) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/mmailer/server_helper.rb', line 51

def start(from=0)
  if machine.trigger(:start)
    puts "starting from #{from}"
    Thread.abort_on_exception = true
    @worker = Thread.new(from) do |from|
      Worker.new(from)
    end
  end
end

#stateObject



18
19
20
# File 'lib/mmailer/server_helper.rb', line 18

def state
  machine.state
end

#stopObject



37
38
39
40
41
42
# File 'lib/mmailer/server_helper.rb', line 37

def stop
  if machine.trigger(:stop)
    display_state
    machine.state
  end
end

#up?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/mmailer/server_helper.rb', line 26

def up?
  true
end