Class: CLI::Replayer

Inherits:
Object
  • Object
show all
Defined in:
lib/clir/Replayer.cls.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.isonObject (readonly)

Returns the value of attribute ison.



5
6
7
# File 'lib/clir/Replayer.cls.rb', line 5

def ison
  @ison
end

Class Method Details

.add_input(input) ⇒ Object

Add a Any input value (a Hash, a string, an object, etc.)



35
36
37
# File 'lib/clir/Replayer.cls.rb', line 35

def add_input(input)
  @inputs << input
end

.commands_folder_pathObject



84
85
86
# File 'lib/clir/Replayer.cls.rb', line 84

def commands_folder_path
  @commands_folder_path ||= mkdir(File.join(ENV["HOME"] || ENV["USERPROFILE"],'.chir_last_commands'))
end

.dataObject



59
60
61
62
63
64
65
# File 'lib/clir/Replayer.cls.rb', line 59

def data
  @data ||= begin
    load.tap do |table|
      CLI.set_command_line_data(table[:data])
    end
  end
end

.get_inputObject



29
30
31
# File 'lib/clir/Replayer.cls.rb', line 29

def get_input
  @inputs.pop 
end

.init_for_recordingObject

When the command is not replayed (ie not ‘_’), CLI initialize the recordings of inputs



11
12
13
# File 'lib/clir/Replayer.cls.rb', line 11

def init_for_recording
  @inputs = []
end

.inputsObject

Use ‘get_input’ to get the input in order with ‘pop’

Returns:

  • last inputs for the same command.



42
43
44
45
46
47
# File 'lib/clir/Replayer.cls.rb', line 42

def inputs
  @inputs ||= begin
    replayable? || raise('Can’t load inputs values: this command is not replayable.')
    data[:inputs].reverse
  end
end

.last_command_file(command_name = nil) ⇒ Object



79
80
81
82
# File 'lib/clir/Replayer.cls.rb', line 79

def last_command_file(command_name = nil)
  command_name ||= CLI.command_name
  File.join(commands_folder_path, command_name)
end

.loadObject



67
68
69
# File 'lib/clir/Replayer.cls.rb', line 67

def load
  Marshal.load(last_command_file)
end

.on?Boolean

(note : it’s running when CLI.main_command is ‘_’)

Returns:

  • (Boolean)

    true if replayer is running



19
20
21
# File 'lib/clir/Replayer.cls.rb', line 19

def on?
  :TRUE == ison
end

.replayable?Boolean

Note: it’s a replayable command if it exists a ‘.<app>_lastcommand’ file in current folder

Returns:

  • (Boolean)

    TRUE if there is a replayable command



75
76
77
# File 'lib/clir/Replayer.cls.rb', line 75

def replayable?
  File.exist?(last_command_file(CLI.command_name))
end

.saveObject

Save inputs at the end of the script



51
52
53
54
55
56
57
# File 'lib/clir/Replayer.cls.rb', line 51

def save
  table_replay = {
    inputs:       inputs,
    data:         CLI::get_command_line_data
  }
  File.write(last_command_file, Marshal.dump(table_replay))
end

.startObject

Start running replayer (with ‘_’ main command)



25
26
27
# File 'lib/clir/Replayer.cls.rb', line 25

def start
  @ison = replayable? ? :TRUE : :FALSE
end