Class: Vedeu::Input::Read Private

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/vedeu/input/read.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Directly read from the terminal.

Examples:

Vedeu.read(input, options)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?

Constructor Details

#initialize(input = nil, options = {}) ⇒ Vedeu::Input::Read

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Vedeu::Input::Read.



30
31
32
33
# File 'lib/vedeu/input/read.rb', line 30

def initialize(input = nil, options = {})
  @input   = input
  @options = options
end

Class Method Details

.read(input = nil, options = {}) ⇒ Object

See Also:



20
21
22
# File 'lib/vedeu/input/read.rb', line 20

def self.read(input = nil, options = {})
  new(input, options).read
end

Instance Method Details

#consoleIO (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
# File 'lib/vedeu/input/read.rb', line 54

def console
  @_console ||= Vedeu::Terminal.console
end

#cooked?Boolean (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



59
60
61
# File 'lib/vedeu/input/read.rb', line 59

def cooked?
  mode == :cooked
end

#defaultsHash<Symbol => void> (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The default options/attributes for a new instance of this class.



64
65
66
67
68
# File 'lib/vedeu/input/read.rb', line 64

def defaults
  {
    mode: :cooked,
  }
end

#fake?Boolean (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



71
72
73
# File 'lib/vedeu/input/read.rb', line 71

def fake?
  mode == :fake
end

#inputString (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vedeu/input/read.rb', line 76

def input
  @_input ||= if input?
                @input

              elsif cooked?
                console.gets.chomp

              elsif raw?
                Vedeu::Input::Translator.translate(Vedeu::Input::Raw.read)

              elsif fake?
                console.cooked do
                  Vedeu::Terminal.debugging!

                  console.gets.chomp
                end

              end
end

#input?Boolean (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



97
98
99
# File 'lib/vedeu/input/read.rb', line 97

def input?
  present?(@input)
end

#modeSymbol (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (Vedeu::Error::InvalidSyntax)

    When the value given for an argument or parameter cannot be used because it is not valid for the use case, unsupported or the method expects a different type.



103
104
105
106
107
108
109
110
# File 'lib/vedeu/input/read.rb', line 103

def mode
  unless valid_mode?
    raise Vedeu::Error::InvalidSyntax,
          ':mode must be `:raw`, `:fake` or `:cooked`.'
  end

  options[:mode]
end

#modesArray<Symbol> (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



113
114
115
116
117
118
119
# File 'lib/vedeu/input/read.rb', line 113

def modes
  [
    :cooked,
    :fake,
    :raw,
  ]
end

#optionsHash<Symbol => Symbol> (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



122
123
124
# File 'lib/vedeu/input/read.rb', line 122

def options
  defaults.merge!(@options)
end

#raw?Boolean (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



127
128
129
# File 'lib/vedeu/input/read.rb', line 127

def raw?
  mode == :raw
end

#readString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vedeu/input/read.rb', line 36

def read
  if cooked?
    Vedeu.trigger(:_command_, input)

  elsif raw?
    Vedeu.trigger(:_keypress_, input)

  elsif fake?
    input

  end

  input
end

#valid_mode?Boolean (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



132
133
134
# File 'lib/vedeu/input/read.rb', line 132

def valid_mode?
  modes.include?(options[:mode])
end