class Torque::TorqueInfoParser
Parses the raw contents of the torque info file Stores each of the fields in .torqueinfo.yaml as publicly accessible fields
Attributes
The email_address
field (String)
The email_password
field (String)
The email_to
field (String or Array)
The format string to use for the project
The output_dir
field (String)
The project field (Fixnum)
The token field (String)
Public Class Methods
@param file_path The path to the .torqueinfo.yaml file @param file_system An instance of the FileSystem
class
Creates a new parser. Does not parse the file
# File lib/torque/torque_info_parser.rb, line 45 def initialize(file_path = "./.torqueinfo.yaml", file_system = FileSystem.new) @file_path = file_path @fs = file_system end
Public Instance Methods
@param field The field to add to @param values A list of values to add to the field
# File lib/torque/torque_info_parser.rb, line 93 def add(field, values) file_string = @fs.file_read(@file_path) new_file_string = add_string(field, values, file_string) @fs.file_write(@file_path, new_file_string) end
@param field The field to add to @param values A list of values to add to the field
@return A list of all values that were added (ie that were not duplicates)
# File lib/torque/torque_info_parser.rb, line 104 def add_no_duplicates(field, values) file_string = @fs.file_read(@file_path) values_copy = values.clone new_file_string = add_no_duplicates_string(field, values_copy, file_string) @fs.file_write(@file_path, new_file_string) values_copy end
@return The TorqueInfoParser
(self)
Parses the file, storing the results as public instnance fields Stores each field with no corresponding value in the file as 'nil'
# File lib/torque/torque_info_parser.rb, line 56 def parse if !@fs.path_exist?(@file_path) directory = File.expand_path(File.dirname(@file_path)) raise MissingTorqueInfoFileError.new "'#{directory}' is not configured for Torque" end torque_info_hash = {} begin torque_info_hash = YAML::load(@fs.file_read(@file_path)) || {} rescue Psych::SyntaxError # TODO Implement custom error, raise/rescue paths to catch this problem end @email_address = torque_info_hash["email_address"] @email_password = torque_info_hash["email_password"] @email_to = torque_info_hash["email_to"] @format = torque_info_hash["format"] @project = torque_info_hash["project"] @output_dir = torque_info_hash["output_dir"] @token = torque_info_hash["token"] self end
@param field The field to remove from @param values A list of values to remove from the field
@return A list of the values that were removed (ie that could be found)
# File lib/torque/torque_info_parser.rb, line 118 def rm(field, values) file_string = @fs.file_read(@file_path) values_copy = values.clone new_file_string = rm_string(field, values_copy, file_string) @fs.file_write(@file_path, new_file_string) values_copy end
@param field The field to set @param value The value to set the field to
# File lib/torque/torque_info_parser.rb, line 84 def set(field, value) file_string = @fs.file_read(@file_path) new_file_string = set_string(field, value, file_string) @fs.file_write(@file_path, new_file_string) end
Private Instance Methods
Alters the string contents of a file, adding all elements of 'values' to field 'field' ignoring duplicates At finish, 'values' contains all values that were added (were not duplicates) Returns the new string contents
# File lib/torque/torque_info_parser.rb, line 170 def add_no_duplicates_string(field, values, file_string) return file_string if values.empty? file_yaml = YAML.load(file_string) || {} file_yaml[field] = to_array(file_yaml[field]) file_yaml[field].delete(nil) values.uniq! index = 0 while index < values.length value = values[index] if file_yaml[field].member? value values.delete_at(index) index-=1 else file_yaml[field] << value end index+=1 end file_yaml.to_yaml end
Alters the string contents of a file, adding all elements of 'values' to field 'field' Returns the new string contents
# File lib/torque/torque_info_parser.rb, line 156 def add_string(field, values, file_string) return file_string if values.empty? file_yaml = YAML.load(file_string) || {} file_yaml[field] = to_array(file_yaml[field]) file_yaml[field].concat(values) file_yaml.to_yaml end
Alters the string contents of a file so that value is added to a sequence belonging to field At finish, 'values' contains all values that were removed (could be found) Returns the new string contents
# File lib/torque/torque_info_parser.rb, line 201 def rm_string(field, values, file_string) return file_string if values.empty? file_yaml = YAML.load(file_string) || {} # The field does not exist if file_yaml[field].is_a? NilClass set_array_in_place(values, []) return file_string # The field is a single value elsif file_yaml[field].is_a? String if values.member? file_yaml[field] set_array_in_place(values, [ file_yaml[field] ]) file_yaml.delete(field) else set_array_in_place(values, []) end # The field is a sequence else index = 0; while index < values.length value = values[index] if file_yaml[field].member? value file_yaml[field].delete(value) else values.delete_at(index) index -= 1 end index += 1 end file_yaml.delete(field) if file_yaml[field].empty? end file_yaml.to_yaml end
Util method. Changes the first array to the second array in place
# File lib/torque/torque_info_parser.rb, line 131 def set_array_in_place(array1, array2) array1.delete_if {true} array1.concat(array2) end
Alters the string contents of a file, setting the specified field to the specified value
# File lib/torque/torque_info_parser.rb, line 146 def set_string(field, value, file_string) file_yaml = YAML.load(file_string) || {} file_yaml[field] = value file_yaml.to_yaml end
Util method. Converts a yaml field to an applicable array
# File lib/torque/torque_info_parser.rb, line 137 def to_array(value) if value.is_a? Array; value elsif value.is_a? String; [value] elsif value.is_a? NilClass; [] else; [] end end