Class: Contraption::Header

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Header

Returns a new instance of Header.



52
53
54
55
56
57
58
# File 'lib/contraption/header.rb', line 52

def initialize opts={}
  @defaults = { title: "",
                publication_date: "",
                summary: "",
                type: "",
                tags: []}.merge!(opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a, &b) ⇒ Object (private)



79
80
81
# File 'lib/contraption/header.rb', line 79

def method_missing m, *a, &b
  @defaults.fetch(m) { super }
end

Class Method Details

.from(string) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/contraption/header.rb', line 7

def from string
  Header.new (Hash[
      string.lines
            .map{|l| process l}
            .reject{|l| l == nil}
  ])
end

Instance Method Details

#filename(ext = '.md') ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/contraption/header.rb', line 60

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?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/contraption/header.rb', line 74

def new?
  publication_date == :now
end

#update(new_opts = {}) ⇒ Object



70
71
72
# File 'lib/contraption/header.rb', line 70

def update new_opts={}
  Header.new @defaults.merge(new_opts)
end