Class: Village::Generators::ArticlesGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/generators/village/articles/articles_generator.rb

Instance Method Summary collapse

Methods inherited from Base

banner, source_root

Instance Method Details

#check_dateObject



17
18
19
20
21
22
# File 'lib/generators/village/articles/articles_generator.rb', line 17

def check_date
  if options.date && options.date !~ /^\d{4}-\d{2}-\d{2}$/
    puts 'Invalid date - please use the following format: YYYY-MM-DD, eg. 2011-01-01.'
    exit
  end
end

#check_slugObject



10
11
12
13
14
15
# File 'lib/generators/village/articles/articles_generator.rb', line 10

def check_slug
  unless slug =~ /^[A-Za-z0-9\-\.]+$/
    puts 'Invalid slug - valid characters include letters, digits and dashes.'
    exit
  end
end

#generate_articleObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/village/articles/articles_generator.rb', line 24

def generate_article
  @date_str, @slug, @extension = File.basename(filename).match(/^(\d+-\d+-\d+)-(.*)(\.[^.]+)$/).captures
  create_file "app/views/articles/#{@date_str}-#{@slug}#{@extension}" do
    "---\n" +
    "title: #{@slug.titleize}\n" +
    "summary: summary goes here ...\n" +
    "categories: ['category']\n" +
    "tags: ['tag']\n" +
    "---\n\n" +
    "CONTENT GOES HERE ..."
  end
end