Class: Carta::CLI::Chapter

Inherits:
Object
  • Object
show all
Defined in:
lib/carta/cli/chapter.rb

Overview

A class for chapter generation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thor, chapter_number, *chapter_name) ⇒ Chapter

Returns a new instance of Chapter.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/carta/cli/chapter.rb', line 11

def initialize(thor, chapter_number, *chapter_name)
  @thor = thor
  @chapters = chapter_number.to_s.split('.')
  @chapters[0] = Carta::Util.pad(@chapters[0])

  if @chapters.length > 1
    @chapters[1] = Carta::Util.pad(@chapters[1])
  else
    @chapters[1] = '01'
  end

  @chapter_name = chapter_name.join(' ')
  @dasherized_name = Carta::Util.slug(@chapter_name)
  @handy_chapter_name    = "#{@chapters[0]}-#{@dasherized_name}"
  @handy_subchapter_name = "#{@chapters[1]}-#{@dasherized_name}"
end

Instance Attribute Details

#chapter_nameObject (readonly)

Returns the value of attribute chapter_name.



4
5
6
# File 'lib/carta/cli/chapter.rb', line 4

def chapter_name
  @chapter_name
end

#chapter_numberObject (readonly)

Returns the value of attribute chapter_number.



4
5
6
# File 'lib/carta/cli/chapter.rb', line 4

def chapter_number
  @chapter_number
end

#levelObject (readonly)

Returns the value of attribute level.



4
5
6
# File 'lib/carta/cli/chapter.rb', line 4

def level
  @level
end

#pretty_numberObject (readonly)

Returns the value of attribute pretty_number.



4
5
6
# File 'lib/carta/cli/chapter.rb', line 4

def pretty_number
  @pretty_number
end

#sub_chapter_nameObject (readonly)

Returns the value of attribute sub_chapter_name.



4
5
6
# File 'lib/carta/cli/chapter.rb', line 4

def sub_chapter_name
  @sub_chapter_name
end

#thorObject (readonly)

Returns the value of attribute thor.



4
5
6
# File 'lib/carta/cli/chapter.rb', line 4

def thor
  @thor
end

Instance Method Details

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/carta/cli/chapter.rb', line 28

def run
  if Dir.exists?('manuscript')
    target_dir = Dir.glob("manuscript/#{@chapters[0]}*")
    if target_dir.empty?
      path = "manuscript/#{@handy_chapter_name}/#{@handy_subchapter_name}.md"
    else
      path = "#{target_dir[0]}/#{@handy_subchapter_name}.md"
    end
    thor.create_file path do
      level = @chapters[2].nil? ? '#' : '#' * (@chapters[2].to_i + 1)
      contents = "#{level} #{chapter_name}"
      contents << "\nLorem ipsum dolor sit amet, consectetur adipisicing elit."
    end
  else
    thor.say('Please create a book first.',:red)
    thor.help
  end
end