Module: DbAgile::Environment::Interactions

Included in:
DbAgile::Environment
Defined in:
lib/dbagile/environment/interactions.rb

Overview

Environment’s interactions contract.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#asking_bufferObject

The buffer to use for asking questions



12
13
14
# File 'lib/dbagile/environment/interactions.rb', line 12

def asking_buffer
  @asking_buffer
end

#message_bufferObject

The buffer to use for displaying messages



9
10
11
# File 'lib/dbagile/environment/interactions.rb', line 9

def message_buffer
  @message_buffer
end

Instance Method Details

#ask(question, answer_type = String, non_interactive_value = nil, &continuation) ⇒ ...

Asks something to the user/oracle and returns the result.

This method is provided when something needs to be asked to the user. It simply returns non_interactive_value if the interaction flag is set to false. Otherwise, it delegates the call to the interaction highline instance.

Parameters:

  • question (String)

    a prompt for the question

  • continuation (Proc)

    a optional continuation procedure

  • non_interactive_value (...) (defaults to: nil)

    value to return in non-interactive mode

Returns:

  • (...)

    non_interactive_value in non-interactive mode, the result of continuation proc otherwise.



110
111
112
113
114
115
116
# File 'lib/dbagile/environment/interactions.rb', line 110

def ask(question, answer_type = String, non_interactive_value = nil, &continuation)
  if interactive?
    interaction_highline.ask(question, answer_type, &continuation)
  else
    nil
  end
end

#ask!(question, answer_type = String, &continuation) ⇒ Object

Same specification as ask, but immediately raises an InteractiveModeRequiredError if the interactive mode is not set!



122
123
124
125
# File 'lib/dbagile/environment/interactions.rb', line 122

def ask!(question, answer_type = String, &continuation)
  interactive!
  ask(question, answer_type, nil, &continuation)
end

#color(str, color) ⇒ Object

When interaction mode is set, colorizes a string with highline. Simply returns str otherwise.



177
178
179
# File 'lib/dbagile/environment/interactions.rb', line 177

def color(str, color)
  interactive? ? interaction_highline.color(str, color) : str
end

#console_widthObject

Returns width of the console. Width set with console_width= is returned in priority. Otherwise, return highline’s output_cols in interactive mode. Returns 80 in all other cases.



196
197
198
# File 'lib/dbagile/environment/interactions.rb', line 196

def console_width
  @console_width ||= infer_console_width
end

#console_width=(width) ⇒ Object

Forces the console width.

When a width is set, it will always be returned by console_width, bypassing any attempt to use highline to infer it.



187
188
189
# File 'lib/dbagile/environment/interactions.rb', line 187

def console_width=(width)
  @console_width = width
end

#display(something, color = nil) ⇒ void

This method returns an undefined value.

Displays something on the message buffer.

Does nothing when interactive mode is disabled. Delegates the call to highline otherwise.

Parameters:

  • something (Object)

    to write on environment output



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/dbagile/environment/interactions.rb', line 157

def display(something, color = nil)
  if interactive?
    if something.kind_of?(String)
      say(something, color)
    elsif something.kind_of?(Enumerable)
      something.each{|v| display(v, color)}
    else
      display(something.to_s, color)
    end
  end
end

#interaction_highlineObject

Returns an instance of HighLine for interactions, creating it if required.

This method factors an HighLine instance even if interactive is set to false. Checking the interactive? flag should be made upstream.



86
87
88
89
90
91
92
93
# File 'lib/dbagile/environment/interactions.rb', line 86

def interaction_highline
  if @interaction_highline.nil?
    in_buffer  = asking_buffer  || input_buffer
    out_buffer = message_buffer || output_buffer
    @interaction_highline = HighLine.new(in_buffer, out_buffer)
  end
  @interaction_highline
end

#interactive!(msg = nil) ⇒ Object

Asserts that the interactive mode is enabled. Raises a InteractiveModeRequiredError otherwise.



22
23
24
25
26
27
# File 'lib/dbagile/environment/interactions.rb', line 22

def interactive!(msg = nil)
  unless interactive?
    msg = "The interactive mode is required, but disabled."
    raise InteractiveModeRequiredError, msg
  end
end

#interactive=(value) ⇒ Object

Sets the interactive mode.

In all cases, the highline instance for interaction is reset to nil.



47
48
49
50
# File 'lib/dbagile/environment/interactions.rb', line 47

def interactive=(value)
  @interactive = value
  @interaction_highline = nil
end

#interactive?Boolean

Returns true if the environment is interactive, false otherwise

Returns:

  • (Boolean)


37
38
39
# File 'lib/dbagile/environment/interactions.rb', line 37

def interactive?
  @interactive
end

#say(something, color = nil) ⇒ void

This method returns an undefined value.

Prints an information message on the appropriate buffer. An optional color may be provided if the environment supports colors.

Does nothing when interactive mode is disabled. Delegates the call to highline otherwise.

The something argument is expected to be a String. Use display to show complex objects, Enumerable in particular

Parameters:

  • something (String)

    a message to print

  • an (Symbol)

    optional color



141
142
143
144
145
146
# File 'lib/dbagile/environment/interactions.rb', line 141

def say(something, color = nil)
  if interactive?
    h = interaction_highline
    color.nil? ? h.say(something) : h.say(h.color(something, color))
  end
end