Class: StandupMD::File

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, config: StandupMD.config.file) ⇒ StandupMD::File

Constructs the instance.

Parameters:



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.expand_path(::File.join(@config.directory, file_name))
  ensure_file

  @new = ::File.zero?(@name)
  @loaded = false
end

Instance Attribute Details

#entriesStandupMD::EntryList (readonly)

The list of entries in the file.



64
65
66
# File 'lib/standup_md/file.rb', line 64

def entries
  @entries
end

#nameString (readonly)

The name of the file.

Returns:

  • (String)


70
71
72
# File 'lib/standup_md/file.rb', line 70

def name
  @name
end

Class Method Details

.configStandupMD::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.

Parameters:

Returns:



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.

Parameters:

Returns:

Raises:

  • (ArgumentError)


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.

Parameters:

Returns:



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?

Returns:

  • (Boolean)

    true if exists



115
116
117
# File 'lib/standup_md/file.rb', line 115

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

#loadStandupMD::File

Loads the file’s contents.

Returns:

Raises:



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?

Returns:

  • (Boolean)

    true if loaded



107
108
109
# File 'lib/standup_md/file.rb', line 107

def loaded?
  @loaded
end

#new?Boolean

Was the file just now created?

Returns:

  • (Boolean)

    true if new



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.

Parameters:

  • {start_date: (Hash)

    Date, end_date: Date}

Returns:

  • (Boolean)

    true if successful

Raises:

  • (ArgumentError)


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