Class: StandupMD::File
- Inherits:
-
Object
- Object
- StandupMD::File
- Defined in:
- lib/standup_md/file.rb
Overview
Class for handling reading and writing standup files.
Instance Attribute Summary collapse
-
#entries ⇒ StandupMP::EntryList
readonly
The list of entries in the file.
-
#name ⇒ String
readonly
The name of the file.
Class Method Summary collapse
-
.config ⇒ StandupMD::Config::EntryList
Access to the class’s configuration.
-
.find(file_name) ⇒ Object
Find standup file in directory by file name.
-
.find_by_date(date) ⇒ Object
Find standup file in directory by Date object.
-
.load(file_name) ⇒ StandupMD::File
Convenience method for calling File.find(file_name).load.
Instance Method Summary collapse
-
#exist? ⇒ Boolean
Does the file exist?.
-
#initialize(file_name) ⇒ StandupMP::File
constructor
Constructs the instance.
-
#load ⇒ StandupMD::FileList
Loads the file’s contents.
-
#loaded? ⇒ Boolean
Has the file been loaded?.
-
#new? ⇒ Boolean
Was the file just now created?.
-
#write(**dates) ⇒ Boolean
Writes a new entry to the file if the first entry in the file isn’t today.
Constructor Details
#initialize(file_name) ⇒ StandupMP::File
Constructs the instance.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/standup_md/file.rb', line 87 def initialize(file_name) @config = self.class.config @parser = StandupMD::Parsers::Markdown.new(@config) if file_name.include?(::File::SEPARATOR) raise ArgumentError, "#{file_name} contains directory. Please use `StandupMD.config.file.directory=`" end unless ::File.directory?(@config.directory) raise "Dir #{@config.directory} not found." unless @config.create FileUtils.mkdir_p(@config.directory) end @name = ::File.(::File.join(@config.directory, file_name)) unless ::File.file?(@name) raise "File #{@name} not found." unless @config.create FileUtils.touch(@name) end @new = ::File.zero?(@name) @loaded = false end |
Instance Attribute Details
#entries ⇒ StandupMP::EntryList (readonly)
The list of entries in the file.
73 74 75 |
# File 'lib/standup_md/file.rb', line 73 def entries @entries end |
#name ⇒ String (readonly)
The name of the file.
79 80 81 |
# File 'lib/standup_md/file.rb', line 79 def name @name end |
Class Method Details
.config ⇒ StandupMD::Config::EntryList
Access to the class’s configuration.
16 17 18 |
# File 'lib/standup_md/file.rb', line 16 def config @config ||= StandupMD.config.file end |
.find(file_name) ⇒ Object
Find standup file in directory by file name.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/standup_md/file.rb', line 39 def find(file_name) unless ::File.directory?(config.directory) raise "Dir #{config.directory} not found." unless config.create FileUtils.mkdir_p(config.directory) end file_path = ::File.join(config.directory, file_name) unless ::File.file?(file_path) || config.create raise "File #{file_name} not found." end new(file_name) end |
.find_by_date(date) ⇒ Object
Find standup file in directory by Date object.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/standup_md/file.rb', line 57 def find_by_date(date) raise ArgumentError, "Must be a Date object" unless date.is_a?(Date) unless ::File.directory?(config.directory) raise "Dir #{config.directory} not found." unless config.create FileUtils.mkdir_p(config.directory) end find(date.strftime(config.name_format)) end |
.load(file_name) ⇒ StandupMD::File
Convenience method for calling File.find(file_name).load
26 27 28 29 30 31 32 33 |
# File 'lib/standup_md/file.rb', line 26 def load(file_name) unless ::File.directory?(config.directory) raise "Dir #{config.directory} not found." unless config.create FileUtils.mkdir_p(config.directory) end new(file_name).load end |
Instance Method Details
#exist? ⇒ Boolean
Does the file exist?
133 134 135 |
# File 'lib/standup_md/file.rb', line 133 def exist? ::File.exist?(name) end |
#load ⇒ StandupMD::FileList
Loads the file’s contents.
141 142 143 144 145 146 147 |
# File 'lib/standup_md/file.rb', line 141 def load raise "File #{name} does not exist." unless ::File.file?(name) @loaded = true @entries = @parser.read(name) self end |
#loaded? ⇒ Boolean
Has the file been loaded?
125 126 127 |
# File 'lib/standup_md/file.rb', line 125 def loaded? @loaded end |
#new? ⇒ Boolean
Was the file just now created?
117 118 119 |
# File 'lib/standup_md/file.rb', line 117 def new? @new end |
#write(**dates) ⇒ Boolean
Writes a new entry to the file if the first entry in the file isn’t today. This method is destructive; if a file for entries in the date range already exists, it will be clobbered with the entries in the range.
157 158 159 160 161 162 |
# File 'lib/standup_md/file.rb', line 157 def write(**dates) sorted_entries = entries.sort start_date = dates.fetch(:start_date, sorted_entries.first.date) end_date = dates.fetch(:end_date, sorted_entries.last.date) @parser.write(name, sorted_entries, start_date: start_date, end_date: end_date) end |