Class: StandupMD::File
- Inherits:
-
Object
- Object
- StandupMD::File
- Defined in:
- lib/standup_md/file.rb
Overview
Class for handling reading and writing standup files.
Defined Under Namespace
Classes: NotFoundError
Instance Attribute Summary collapse
-
#entries ⇒ StandupMD::EntryList
readonly
The list of entries in the file.
-
#name ⇒ String
readonly
The name of the file.
Class Method Summary collapse
-
.config ⇒ StandupMD::Config::File
Access to the class’s configuration.
-
.find(file_name, config: StandupMD.config.file) ⇒ StandupMD::File
Find standup file in directory by file name.
-
.find_by_date(date, config: StandupMD.config.file) ⇒ StandupMD::File
Find standup file in directory by Date object.
-
.load(file_name, config: StandupMD.config.file) ⇒ StandupMD::File
Convenience method for calling File.find(file_name).load.
Instance Method Summary collapse
-
#exist? ⇒ Boolean
Does the file exist?.
-
#initialize(file_name, config: StandupMD.config.file) ⇒ StandupMD::File
constructor
Constructs the instance.
-
#load ⇒ StandupMD::File
Loads the file’s contents.
-
#loaded? ⇒ Boolean
Has the file been loaded?.
-
#new? ⇒ Boolean
Was the file just now created?.
-
#write(**dates) ⇒ Boolean
Writes entries to disk.
Constructor Details
#initialize(file_name, config: StandupMD.config.file) ⇒ StandupMD::File
Constructs the instance.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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.(::File.join(@config.directory, file_name)) ensure_file @new = ::File.zero?(@name) @loaded = false end |
Instance Attribute Details
#entries ⇒ StandupMD::EntryList (readonly)
The list of entries in the file.
64 65 66 |
# File 'lib/standup_md/file.rb', line 64 def entries @entries end |
#name ⇒ String (readonly)
The name of the file.
70 71 72 |
# File 'lib/standup_md/file.rb', line 70 def name @name end |
Class Method Details
.config ⇒ StandupMD::Config::File
Access to the class’s configuration.
20 21 22 |
# File 'lib/standup_md/file.rb', line 20 def config StandupMD.config.file end |
.find(file_name, config: StandupMD.config.file) ⇒ StandupMD::File
Find standup file in directory by file name.
42 43 44 |
# File 'lib/standup_md/file.rb', line 42 def find(file_name, config: StandupMD.config.file) new(file_name, config: config) end |
.find_by_date(date, config: StandupMD.config.file) ⇒ StandupMD::File
Find standup file in directory by Date object.
53 54 55 56 57 |
# 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 |
.load(file_name, config: StandupMD.config.file) ⇒ StandupMD::File
Convenience method for calling File.find(file_name).load.
31 32 33 |
# File 'lib/standup_md/file.rb', line 31 def load(file_name, config: StandupMD.config.file) new(file_name, config: config).load end |
Instance Method Details
#exist? ⇒ Boolean
Does the file exist?
115 116 117 |
# File 'lib/standup_md/file.rb', line 115 def exist? ::File.exist?(name) end |
#load ⇒ StandupMD::File
Loads the file’s contents.
123 124 125 126 127 128 129 |
# 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 |
#loaded? ⇒ Boolean
Has the file been loaded?
107 108 109 |
# File 'lib/standup_md/file.rb', line 107 def loaded? @loaded end |
#new? ⇒ Boolean
Was the file just now created?
99 100 101 |
# File 'lib/standup_md/file.rb', line 99 def new? @new end |
#write(**dates) ⇒ Boolean
Writes entries to disk. This method is destructive; existing file contents are replaced by the rendered entries in the requested date range.
138 139 140 141 142 143 144 145 146 147 148 149 |
# 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 |