Class: SeeingIsBelieving::EvaluateByMovingFiles

Inherits:
Object
  • Object
show all
Defined in:
lib/seeing_is_believing/evaluate_by_moving_files.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program, filename, options = {}) ⇒ EvaluateByMovingFiles

Returns a new instance of EvaluateByMovingFiles.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 30

def initialize(program, filename,  options={})
  options = options.dup
  self.program           = program
  self.filename          = filename
  self.encoding          = options.delete(:encoding)
  self.timeout_seconds   = options.delete(:timeout_seconds)    || 0 # 0 is the new infinity
  self.provided_input    = options.delete(:provided_input)     || String.new
  self.event_handler     = options.delete(:event_handler)      || raise(ArgumentError, "must provide an :event_handler")
  self.load_path_flags   = (options.delete(:load_path_dirs)    || []).map { |dir| ['-I', dir] }.flatten
  self.require_flags     = (options.delete(:require_files)     || ['seeing_is_believing/the_matrix']).map { |filename| ['-r', filename] }.flatten
  self.max_line_captures = (options.delete(:max_line_captures) || Float::INFINITY) # (optimization: child stops producing results at this number, even though it might make more sense for the consumer to stop emitting them)
  options.any? && raise(ArgumentError, "Unknown options: #{options.inspect}")
end

Instance Attribute Details

#debuggerObject

Returns the value of attribute debugger.



28
29
30
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 28

def debugger
  @debugger
end

#encodingObject

Returns the value of attribute encoding.



28
29
30
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 28

def encoding
  @encoding
end

#event_handlerObject

Returns the value of attribute event_handler.



28
29
30
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 28

def event_handler
  @event_handler
end

#filenameObject

Returns the value of attribute filename.



28
29
30
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 28

def filename
  @filename
end

#load_path_flagsObject

Returns the value of attribute load_path_flags.



28
29
30
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 28

def load_path_flags
  @load_path_flags
end

#max_line_capturesObject

Returns the value of attribute max_line_captures.



28
29
30
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 28

def max_line_captures
  @max_line_captures
end

#programObject

Returns the value of attribute program.



28
29
30
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 28

def program
  @program
end

#provided_inputObject

Returns the value of attribute provided_input.



28
29
30
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 28

def provided_input
  @provided_input
end

#require_flagsObject

Returns the value of attribute require_flags.



28
29
30
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 28

def require_flags
  @require_flags
end

#timeout_secondsObject

Returns the value of attribute timeout_seconds.



28
29
30
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 28

def timeout_seconds
  @timeout_seconds
end

Class Method Details

.call(*args) ⇒ Object



24
25
26
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 24

def self.call(*args)
  new(*args).call
end

Instance Method Details

#backup_filenameObject



61
62
63
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 61

def backup_filename
  File.join file_directory, "seeing_is_believing_backup.#{File.basename filename}"
end

#callObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 44

def call
  HardCoreEnsure.call \
    code: -> {
      we_will_not_overwrite_existing_backup_file!
      backup_existing_file
      write_program_to_file
      evaluate_file
    },
    ensure: -> {
      set_back_to_initial_conditions
    }
end

#file_directoryObject



57
58
59
# File 'lib/seeing_is_believing/evaluate_by_moving_files.rb', line 57

def file_directory
  File.dirname filename
end