Class: Potassium::TextSpinner

Inherits:
Object
  • Object
show all
Defined in:
lib/potassium/text_spinner.rb

Overview

TODO: I don’t know if this is a concern of this gem. Maybe we should move this later

Constant Summary collapse

DEFAULT_ATTRIBUTES =
{
  wait_condition: -> {},
  base_message: "",
  interval: 0.4,
  message_continuations: ["", ".", "..", "..."]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ TextSpinner

Returns a new instance of TextSpinner.



14
15
16
17
18
19
20
# File 'lib/potassium/text_spinner.rb', line 14

def initialize(attributes = {})
  DEFAULT_ATTRIBUTES.merge(attributes).each do |key, value|
    public_send("#{key}=", value)
  end
  self.started = false
  self.counter = 0
end

Instance Attribute Details

#base_messageObject

Returns the value of attribute base_message.



4
5
6
# File 'lib/potassium/text_spinner.rb', line 4

def base_message
  @base_message
end

#counterObject

Returns the value of attribute counter.



4
5
6
# File 'lib/potassium/text_spinner.rb', line 4

def counter
  @counter
end

#intervalObject

Returns the value of attribute interval.



4
5
6
# File 'lib/potassium/text_spinner.rb', line 4

def interval
  @interval
end

#message_continuationsObject

Returns the value of attribute message_continuations.



4
5
6
# File 'lib/potassium/text_spinner.rb', line 4

def message_continuations
  @message_continuations
end

#startedObject

Returns the value of attribute started.



4
5
6
# File 'lib/potassium/text_spinner.rb', line 4

def started
  @started
end

#wait_conditionObject

Returns the value of attribute wait_condition.



4
5
6
# File 'lib/potassium/text_spinner.rb', line 4

def wait_condition
  @wait_condition
end

Instance Method Details

#startObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/potassium/text_spinner.rb', line 22

def start
  fail already_started_message if started
  self.started = true

  Thread.new do
    loop do
      break if wait_condition.call(counter)
      print_message
      sleep interval
      self.counter += 1
    end
    self.started = false
  end
end