Class: StandupMD::File

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/standup_md/file.rb,
lib/standup_md/file/helpers.rb

Overview

Class for handling reading and writing standup files.

Defined Under Namespace

Modules: Helpers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ StandupMP::File

Constructs the instance.

Parameters:

  • file_name (String)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/standup_md/file.rb', line 89

def initialize(file_name)
  @config = self.class.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.expand_path(::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

#entriesStandupMP::EntryList (readonly)

The list of entries in the file.

Returns:

  • (StandupMP::EntryList)


75
76
77
# File 'lib/standup_md/file.rb', line 75

def entries
  @entries
end

#nameString (readonly)

The name of the file.

Returns:

  • (String)


81
82
83
# File 'lib/standup_md/file.rb', line 81

def name
  @name
end

Class Method Details

.configStandupMD::Config::EntryList

Access to the class’s configuration.



18
19
20
# File 'lib/standup_md/file.rb', line 18

def config
  @config ||= StandupMD.config.file
end

.find(file_name) ⇒ Object

Find standup file in directory by file name.

Parameters:

  • File_naem (String)


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/standup_md/file.rb', line 41

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.

Parameters:

  • date (Date)

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
# File 'lib/standup_md/file.rb', line 59

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

Parameters:

  • file_name (String)

Returns:



28
29
30
31
32
33
34
35
# File 'lib/standup_md/file.rb', line 28

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?

Returns:

  • (Boolean)

    true if exists



134
135
136
# File 'lib/standup_md/file.rb', line 134

def exist?
  ::File.exist?(name)
end

#loadStandupMD::FileList

Loads the file’s contents. TODO clean up this method.

Returns:

  • (StandupMD::FileList)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/standup_md/file.rb', line 143

def load
  raise "File #{name} does not exist." unless ::File.file?(name)

  entry_list = EntryList.new
  record = {}
  section_type = ""
  ::File.foreach(name) do |line|
    line.chomp!
    next if line.strip.empty?

    if header?(line)
      unless record.empty?
        entry_list << new_entry(record)
        record = {}
      end
      record["header"] = line.sub(/^\#{#{@config.header_depth}}\s*/, "")
      section_type = @config.notes_header
      record[section_type] = []
    elsif sub_header?(line)
      section_type = determine_section_type(line)
      record[section_type] = []
    else
      record[section_type] << line.sub(bullet_character_regex, "")
    end
  end
  entry_list << new_entry(record) unless record.empty?
  @loaded = true
  @entries = entry_list.sort
  self
rescue => e
  raise "File malformation: #{e}"
end

#loaded?Boolean

Has the file been loaded?

Returns:

  • (Boolean)

    true if loaded



126
127
128
# File 'lib/standup_md/file.rb', line 126

def loaded?
  @loaded
end

#new?Boolean

Was the file just now created?

Returns:

  • (Boolean)

    true if new



118
119
120
# File 'lib/standup_md/file.rb', line 118

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.

Parameters:

  • {start_date: (Hash)

    Date, end_date: Date}

Returns:

  • (Boolean)

    true if successful



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/standup_md/file.rb', line 184

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)
  ::File.open(name, "w") do |f|
    sorted_entries.filter(start_date, end_date).sort_reverse.each do |entry|
      f.puts header(entry.date)
      @config.sub_header_order.each do |attr|
        tasks = entry.public_send(attr)
        next if !tasks || tasks.empty?

        f.puts sub_header(@config.public_send("#{attr}_header").capitalize)
        tasks.each { |task| f.puts "#{@config.bullet_character} #{task}" }
      end
      f.puts
    end
  end
  true
end