Class: Artifact::MarkdownFile

Inherits:
WritableFile show all
Defined in:
lib/artifact.rb

Instance Attribute Summary

Attributes inherited from ReadableFile

#full_path, #path

Instance Method Summary collapse

Methods inherited from WritableFile

#delete, #save

Methods inherited from ReadableFile

#body, #dirname, #exists?, #filename, #initialize, #last_modified, #meta, #slug

Constructor Details

This class inherits a constructor from Artifact::ReadableFile

Instance Method Details

#authorObject



360
361
362
# File 'lib/artifact.rb', line 360

def author
  data['author']
end

#categoryObject



364
365
366
# File 'lib/artifact.rb', line 364

def category
  data['category']
end

#contentObject



385
386
387
# File 'lib/artifact.rb', line 385

def content
  parts[1..-1].join("---\n\n")
end

#dataObject



389
390
391
392
393
# File 'lib/artifact.rb', line 389

def data
  @data ||= YAML.load(parts[0])
rescue Psych::SyntaxError => e
  raise "Invalid YAML at #{path}: #{parts[0]}"
end

#dateObject



356
357
358
# File 'lib/artifact.rb', line 356

def date
  data['date']
end

#last_updated_byObject



368
369
370
# File 'lib/artifact.rb', line 368

def last_updated_by
  data['last_updated_by']
end

#partsObject



395
396
397
# File 'lib/artifact.rb', line 395

def parts
  read.split("---\n\n")
end

#sanitized_meta(hash) ⇒ Object

include existing date and author in yaml hash in case they’re not set



400
401
402
403
404
405
406
407
408
# File 'lib/artifact.rb', line 400

def sanitized_meta(hash)
  hash.stringify_keys!
  if exists?
    %w(date author).each do |key|
      hash[key] = self.send(key) if !hash[key] and self.send(key)
    end
  end
  hash.to_yaml
end

#titleObject



352
353
354
# File 'lib/artifact.rb', line 352

def title
  data['title']
end

#update(content, meta = {}) ⇒ Object



372
373
374
375
376
377
# File 'lib/artifact.rb', line 372

def update(content, meta = {})
  yaml_data = sanitized_meta(meta) # insert date and author if missing
  body = "#{yaml_data}---\n\n#{content}"
  super(body)
  @data = nil # force reload
end

#update_meta(key, val) ⇒ Object



379
380
381
382
383
# File 'lib/artifact.rb', line 379

def update_meta(key, val)
  hash = {}
  hash[key.to_s] = val
  update(content, data.merge(hash))
end