Class: StandupMD::File

Inherits:
Object
  • Object
show all
Defined in:
lib/standup_md/file.rb

Overview

Class for handling reading and writing standup files.

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)


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.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)


73
74
75
# File 'lib/standup_md/file.rb', line 73

def entries
  @entries
end

#nameString (readonly)

The name of the file.

Returns:

  • (String)


79
80
81
# File 'lib/standup_md/file.rb', line 79

def name
  @name
end

Class Method Details

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

Parameters:

  • File_naem (String)


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.

Parameters:

  • date (Date)

Raises:

  • (ArgumentError)


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

Parameters:

  • file_name (String)

Returns:



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?

Returns:

  • (Boolean)

    true if exists



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

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

#loadStandupMD::FileList

Loads the file’s contents.

Returns:

  • (StandupMD::FileList)


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?

Returns:

  • (Boolean)

    true if loaded



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

def loaded?
  @loaded
end

#new?Boolean

Was the file just now created?

Returns:

  • (Boolean)

    true if new



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.

Parameters:

  • {start_date: (Hash)

    Date, end_date: Date}

Returns:

  • (Boolean)

    true if successful



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