class Torque::DateSettings

Determines the date range for a release notes generation

Public Class Methods

new(options, last_run_path, fs) click to toggle source

@param options A hash of the command line options used to run Torque @param last_run_path The path to the lastRun file @param fs An instance of the FileSystem class

# File lib/torque/date_settings.rb, line 13
def initialize(options, last_run_path, fs)
  @options = options
  @last_run_path = last_run_path
  @fs = fs

  @current_date = Date.today
  @beginning_of_time = Date.new(1900, 1, 1)
  @date_format = "%Y-%m-%d"
end

Public Instance Methods

custom_date_range?() click to toggle source

@return True if the date range was set manually via command line, else false

# File lib/torque/date_settings.rb, line 34
def custom_date_range?
  generate_dates if !@custom_date_range
  return @custom_date_range
end
get_dates() click to toggle source

Generates the accept_from and accept_to dates if they don't already exist

@return [accept_from, accept_to]

# File lib/torque/date_settings.rb, line 27
def get_dates
  generate_dates if !@accept_from || !@accept_to
  return @accept_from, @accept_to
end

Private Instance Methods

generate_dates() click to toggle source

Generates values for @accept_from and @accept_to Returns [@accept_from, @accept_to]

# File lib/torque/date_settings.rb, line 44
def generate_dates
  
  # If custom date settings exist, takes dates from there
  if @options[:accept_from] || @options[:accept_to]
    get_dates_from_custom_range
    @custom_date_range = true

  # If not:
  # @accept_to = today
  # @accept_from is calculated from the dates of previous generations
  else
    get_dates_from_default
    @custom_date_range = false 
  end

  return @accept_from, @accept_to

end
get_dates_from_custom_range() click to toggle source

Handles date values that were set manually in the environment Returns [@accept_from, @accept_to]

# File lib/torque/date_settings.rb, line 65
def get_dates_from_custom_range
  begin
    @accept_from = Date.parse(@options[:accept_from].nil? ? "1900-01-01" : @options[:accept_from])
  rescue ArgumentError
    raise ArgumentError.new("Date '#{@options[:accept_from]}' could not be parsed")
  end

  begin
    @accept_to = (@options[:accept_to].nil? ? @current_date : Date.parse(@options[:accept_to]))
  rescue ArgumentError
    raise ArgumentError.new("Date '#{@options[:accept_to]}' could not be parsed")
  end

  if @accept_from > @accept_to
    raise ArgumentError.new("Date ACCEPT_FROM (#{@accept_from}) occurs after ACCEPT_TO (#{@accept_to})")
  end

  return @accept_from, @accept_to
end
get_dates_from_default() click to toggle source

dates: An array of the dates contained by .lastRun

Reads from the .last-run file for the project to figure out when notes were last generated Returns [@accept_from, @accept_to]

# File lib/torque/date_settings.rb, line 89
def get_dates_from_default

  dates = update_last_run_file

  @accept_to = @current_date
  @accept_from = dates[1] || @beginning_of_time

  return @accept_from, @accept_to
end
update_last_run_file() click to toggle source

Updates .last-run on the filesystem, returning the dates it contains after the update

# File lib/torque/date_settings.rb, line 100
def update_last_run_file

  # Structure of last-run file:
  #
  # <Most recent generation date>
  # <Second most recent generation date>
  
  # Creates the .last-run if none exists
  if !@fs.path_exist? @last_run_path
    @fs.file_create(@last_run_path)
  end

  # Generates and writes the new file contents
  new_file_str, dates = update_last_run_file_string(@fs.file_read(@last_run_path))
  @fs.file_write(@last_run_path, new_file_str)

  # Returns the new dates
  dates

end
update_last_run_file_string(file_str) click to toggle source

Takes in a string with the contents of the last-run file, outputs a string with its new contents

# File lib/torque/date_settings.rb, line 122
def update_last_run_file_string(file_str)

  # Parses the dates in .last-run
  date_strings = []
  file_str.each_line do |date_str|
    date_strings << date_str
  end
  dates = date_strings.map do |date_str|
    Date.strptime(date_str, @date_format)
  end

  #Generates new dates
  new_dates = []
        
  if dates.length == 0
    new_dates = [@current_date]
  
  elsif dates.length == 1
    if dates[0] == @current_date
      new_dates = [dates[0]]
    else
      new_dates = [@current_date, dates[0]]
    end

  else
    if dates[0] == @current_date
      new_dates = [dates[0], dates[1]]
    else
      new_dates = [@current_date, dates[0]]
    end
  end

  # Generates a new file string
  new_file_str = ""
  new_dates.each do
    |date|
    new_file_str += "#{date.strftime(@date_format)}\n"
  end

  # Returns [new_file_str, new_dates]
  return new_file_str, new_dates
end