class Contraption::Header

Public Class Methods

from(string) click to toggle source
# File lib/contraption/header.rb, line 7
def from string
  Header.new (Hash[
      string.lines
            .map{|l| process l}
            .reject{|l| l == nil}
  ])
end
new(opts={}) click to toggle source
# File lib/contraption/header.rb, line 52
def initialize opts={}
  @defaults = { title: "",
                external: nil,
                publication_date: "",
                summary: "",
                type: "",
                tags: []}.merge!(opts)
end

Private Class Methods

determine_date(raw_value) click to toggle source
# File lib/contraption/header.rb, line 43
def determine_date raw_value
  if raw_value.downcase == "now"
    :now
  else
    DateTime.parse(raw_value)
  end
end
process(line) click to toggle source
# File lib/contraption/header.rb, line 16
def process line
  return nil if /^=+$/.match line
  splitted = line.strip.split /^(\w+):\s+/, 2
  return [:title, line.strip.chomp] if splitted.count == 1

  key = splitted[1].downcase.to_sym
  raw_value = splitted[2].strip
  case key
  when :external
    [:external, raw_value]
  when :filename
    [:filename, raw_value]
  when :published
    [:publication_date, determine_date(raw_value)]
  when :summary
    [:summary, raw_value]
  when :tags
    [:tags, raw_value.split(/,\s+/).map{|t| Tag.new t}]
  when :type
    [:type, raw_value.downcase.to_sym]
  when :title
    [:title, raw_value]
  else
    nil
  end
end

Public Instance Methods

filename(ext='.md') click to toggle source
# File lib/contraption/header.rb, line 61
def filename ext='.md'
  fn = @defaults.fetch(:filename) do
    title.downcase
         .gsub(/[^\w\s-]+/, ' ')
         .strip
         .gsub(/\s+/, '-') + ext
  end.split ext
  fn.join + ext
end
new?() click to toggle source
# File lib/contraption/header.rb, line 75
def new?
  publication_date == :now
end
update(new_opts={}) click to toggle source
# File lib/contraption/header.rb, line 71
def update new_opts={}
  Header.new @defaults.merge(new_opts)
end

Private Instance Methods

method_missing(m, *a, &b) click to toggle source
Calls superclass method
# File lib/contraption/header.rb, line 80
def method_missing m, *a, &b
  @defaults.fetch(m) { super }
end