Class: Timebomb::BombFile

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

Overview

Handles reading and writing to a Timebomb file ‘*.tb`.

Constant Summary collapse

EXTENSION =
".tb".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ BombFile

Returns a new instance of BombFile.



175
176
177
178
# File 'lib/timebomb.rb', line 175

def initialize(path)
  @path = Pathname.new(path)
  @bomb = Bomb.new
end

Instance Attribute Details

#bombObject (readonly)

Returns the value of attribute bomb.



173
174
175
# File 'lib/timebomb.rb', line 173

def bomb
  @bomb
end

#pathObject (readonly)

Returns the value of attribute path.



173
174
175
# File 'lib/timebomb.rb', line 173

def path
  @path
end

Instance Method Details

#readObject



180
181
182
183
184
185
186
187
188
# File 'lib/timebomb.rb', line 180

def read
  File.open(path, 'r') do |file|
    data = file.read
    frontmatter = Frontmatter.new(data)
    bomb.title = frontmatter.data.fetch("title")
    bomb.date = frontmatter.data.fetch("date")
    bomb.description = frontmatter.body
  end
end

#writeObject



190
191
192
193
194
195
196
197
198
199
# File 'lib/timebomb.rb', line 190

def write
  data = { "title" => bomb.title, "date" => bomb.date }

  File.open(path, 'w') do |file|
    file.puts data.to_yaml
    file.puts "---"
    file.puts
    file.puts bomb.description
  end
end