class StandupMD::File
Class for handling reading and writing standup files.
Attributes
The list of entries in the file.
@return [StandupMD::EntryList]
The name of the file.
@return [String]
Public Class Methods
Source
# File lib/standup_md/file.rb, line 20 def config StandupMD.config.file end
Access to the class’s configuration.
@return [StandupMD::Config::File]
Source
# File lib/standup_md/file.rb, line 42 def find(file_name, config: StandupMD.config.file) new(file_name, config: config) end
Find standup file in directory by file name.
@param [String] file_name @param [StandupMD::Config::File] config
@return [StandupMD::File]
Source
# File lib/standup_md/file.rb, line 53 def find_by_date(date, config: StandupMD.config.file) raise ArgumentError, "Must be a Date object" unless date.is_a?(Date) find(date.strftime(config.name_format), config: config) end
Find standup file in directory by Date object.
@param [Date] date @param [StandupMD::Config::File] config
@return [StandupMD::File]
Source
# File lib/standup_md/file.rb, line 31 def load(file_name, config: StandupMD.config.file) new(file_name, config: config).load end
Convenience method for calling File.find(file_name).load.
@param [String] file_name @param [StandupMD::Config::File] config
@return [StandupMD::File]
Source
# File lib/standup_md/file.rb, line 79 def initialize(file_name, config: StandupMD.config.file) @config = config @parser = StandupMD::Parsers::Markdown.new(@config) if file_name.include?(::File::SEPARATOR) raise ArgumentError, "#{file_name} contains directory. Configure the file directory separately." end ensure_directory @name = ::File.expand_path(::File.join(@config.directory, file_name)) ensure_file @new = ::File.zero?(@name) @loaded = false end
Constructs the instance.
@param [String] file_name @param [StandupMD::Config::File] config
@return [StandupMD::File]
Public Instance Methods
Source
# File lib/standup_md/file.rb, line 115 def exist? ::File.exist?(name) end
Does the file exist?
@return [Boolean] true if exists
Source
# File lib/standup_md/file.rb, line 123 def load raise NotFoundError, "File #{name} does not exist." unless ::File.file?(name) @loaded = true @entries = @parser.parse(::File.read(name)) self end
Loads the file’s contents.
@return [StandupMD::File]
Source
# File lib/standup_md/file.rb, line 107 def loaded? @loaded end
Has the file been loaded?
@return [Boolean] true if loaded
Source
# File lib/standup_md/file.rb, line 99 def new? @new end
Was the file just now created?
@return [Boolean] true if new
Source
# File lib/standup_md/file.rb, line 138 def write(**dates) raise ArgumentError, "No entries loaded for #{name}" if entries.nil? || entries.empty? sorted_entries = entries.sort start_date = dates.fetch(:start_date, sorted_entries.first.date) end_date = dates.fetch(:end_date, sorted_entries.last.date) ::File.write( name, @parser.render(sorted_entries, start_date: start_date, end_date: end_date) ) true end
Writes entries to disk. This method is destructive; existing file contents are replaced by the rendered entries in the requested date range.
@param [Hash] {start_date: Date, end_date: Date}
@return [Boolean] true if successful