Class: StateReader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell) ⇒ StateReader

Returns a new instance of StateReader.



10
11
12
13
14
# File 'lib/state_reader.rb', line 10

def initialize shell
  @filename_formatter = FilenameFormatter.new
  @shell = shell
reset
end

Instance Attribute Details

#next_stepObject

Returns the value of attribute next_step.



8
9
10
# File 'lib/state_reader.rb', line 8

def next_step
  @next_step
end

#session_dirObject

Returns the value of attribute session_dir.



8
9
10
# File 'lib/state_reader.rb', line 8

def session_dir
  @session_dir
end

#session_idObject

Returns the value of attribute session_id.



8
9
10
# File 'lib/state_reader.rb', line 8

def session_id
  @session_id
end

Instance Method Details

#enough_states?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/state_reader.rb', line 29

def enough_states?
  state_count >= states_needed_for_one_move
end

#fill_state_with_properties(state, properties) ⇒ Object



93
94
95
# File 'lib/state_reader.rb', line 93

def fill_state_with_properties state, properties
	state.return_code = properties[InfoPropertyFile.RETURN_CODE_PROPERTY]
end

#get_state_dir(step = @next_step) ⇒ Object



37
38
39
# File 'lib/state_reader.rb', line 37

def get_state_dir step=@next_step
  @filename_formatter.state_dir(@session_id, step)
end

#goto_last_stateObject



49
50
51
52
53
# File 'lib/state_reader.rb', line 49

def goto_last_state
	step = 0
	while state_exist?(step) do step += 1 end
	@next_step = step-1
end

#has_next_stateObject



41
42
43
# File 'lib/state_reader.rb', line 41

def has_next_state
state_exist? @next_step
end

#read_next_stateObject



67
68
69
70
71
# File 'lib/state_reader.rb', line 67

def read_next_state
 state = read_state
  @next_step += 1
  state
end

#read_previous_stateObject



73
74
75
76
77
# File 'lib/state_reader.rb', line 73

def read_previous_state
state = read_state
@next_step -= 1
state
end

#read_source_file_contents(source_files) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/state_reader.rb', line 84

def read_source_file_contents source_files
	marker = "=========="
	source_files.collect {|file| 
		filename = Filename.new(file).extract_last_path_item.to_s
		code = @shell.read_file(file)
		"#{marker} #{filename} #{marker}\n\n#{code}"
	}
end

#read_source_files(state_dir) ⇒ Object



79
80
81
82
# File 'lib/state_reader.rb', line 79

def read_source_files state_dir
	files = @shell.files_in_dir_tree state_dir
	files.find_all{|file| @filename_formatter.source_file_in_state_dir? file}
end

#read_stateObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/state_reader.rb', line 55

def read_state
		state = State.new
 state_dir = get_state_dir
 state.time = @shell.creation_time state_dir
		state.files = read_source_files state_dir
 state.file_contents = read_source_file_contents state.files
		state.result = @shell.read_file @filename_formatter.result_file(state_dir)
		properties = @shell.read_properties @filename_formatter.info_file(state_dir)
		fill_state_with_properties state, properties
		state
end

#resetObject



16
17
18
# File 'lib/state_reader.rb', line 16

def reset
	@next_step = 0
end

#state_countObject



25
26
27
# File 'lib/state_reader.rb', line 25

def state_count
 @shell.real_dir_entries(@filename_formatter.session_dir @session_id).count
end

#state_exist?(step = @next_step) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/state_reader.rb', line 45

def state_exist? step=@next_step
	File.exist?(get_state_dir step)
end

#states_needed_for_one_moveObject



33
34
35
# File 'lib/state_reader.rb', line 33

def states_needed_for_one_move
	2
end