Class: Torque::TorqueInfoParser

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

Overview

Parses the raw contents of the torque info file Stores each of the fields in .torqueinfo.yaml as publicly accessible fields

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path = "./.torqueinfo.yaml", file_system = FileSystem.new) ⇒ TorqueInfoParser

Creates a new parser. Does not parse the file

Parameters:

  • file_path (defaults to: "./.torqueinfo.yaml")

    The path to the .torqueinfo.yaml file

  • file_system (defaults to: FileSystem.new)

    An instance of the FileSystem class



45
46
47
48
49
# 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

Instance Attribute Details

#email_addressObject (readonly)

The email_address field (String)



14
15
16
# File 'lib/torque/torque_info_parser.rb', line 14

def email_address
  @email_address
end

#email_passwordObject (readonly)

The email_password field (String)



18
19
20
# File 'lib/torque/torque_info_parser.rb', line 18

def email_password
  @email_password
end

#email_toObject (readonly)

The email_to field (String or Array)



22
23
24
# File 'lib/torque/torque_info_parser.rb', line 22

def email_to
  @email_to
end

#formatObject (readonly)

The format string to use for the project



26
27
28
# File 'lib/torque/torque_info_parser.rb', line 26

def format
  @format
end

#output_dirObject (readonly)

The output_dir field (String)



34
35
36
# File 'lib/torque/torque_info_parser.rb', line 34

def output_dir
  @output_dir
end

#projectObject (readonly)

The project field (Fixnum)



30
31
32
# File 'lib/torque/torque_info_parser.rb', line 30

def project
  @project
end

#tokenObject (readonly)

The token field (String)



38
39
40
# File 'lib/torque/torque_info_parser.rb', line 38

def token
  @token
end

Instance Method Details

#add(field, values) ⇒ Object

Parameters:

  • field

    The field to add to

  • values

    A list of values to add to the field



93
94
95
96
97
# 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

#add_no_duplicates(field, values) ⇒ Object

Returns A list of all values that were added (ie that were not duplicates).

Parameters:

  • field

    The field to add to

  • values

    A list of values to add to the field

Returns:

  • A list of all values that were added (ie that were not duplicates)



104
105
106
107
108
109
110
111
# 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

#parseObject

Parses the file, storing the results as public instnance fields Stores each field with no corresponding value in the file as ‘nil’

Returns:

  • The TorqueInfoParser (self)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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

#rm(field, values) ⇒ Object

Returns A list of the values that were removed (ie that could be found).

Parameters:

  • field

    The field to remove from

  • values

    A list of values to remove from the field

Returns:

  • A list of the values that were removed (ie that could be found)



118
119
120
121
122
123
124
125
# 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

#set(field, value) ⇒ Object

Parameters:

  • field

    The field to set

  • value

    The value to set the field to



84
85
86
87
88
# 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