Class: Neapolitan::Part

Inherits:
Object
  • Object
show all
Defined in:
lib/neapolitan/part.rb

Overview

A part is a section of a template. Templates can be segmented into parts using the ‘— FORMAT’ notation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, text, format) ⇒ Part

Setup new Part instance.

Parameters:

  • text (String)

    The parts body.

  • formats (Array)

    The template formats to apply to the body text.



34
35
36
37
38
# File 'lib/neapolitan/part.rb', line 34

def initialize(template, text, format)
  @template = template
  @text     = text
  @format   = format
end

Instance Attribute Details

#templateObject (readonly)

The template to which the part belongs.



21
22
23
# File 'lib/neapolitan/part.rb', line 21

def template
  @template
end

#textObject (readonly)

Body of text as given in the part.



24
25
26
# File 'lib/neapolitan/part.rb', line 24

def text
  @text
end

Class Method Details

.parse(template, body) ⇒ Object

Parse text body and create new part.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/neapolitan/part.rb', line 8

def self.parse(template, body)
  if body.start_with?('---')
    index   = body.index("\n")
    format  = body[0...index].strip.sub(/^---/,'').strip
    text    = body[index+1..-1].strip
  else
    text   = body
    format = ''
  end
  new(template, text, format)
end

Instance Method Details

#defaultObject

Template default format split into array.



51
52
53
# File 'lib/neapolitan/part.rb', line 51

def default
  @_default ||= split_format(template.default)
end

#finishObject

Template default format split into array.



61
62
63
# File 'lib/neapolitan/part.rb', line 61

def finish
  @_finish ||= split_format(template.finish)
end

#formatObject

Rendering format as given in the template document.



41
42
43
# File 'lib/neapolitan/part.rb', line 41

def format
  @format
end

#formatting(&custom) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/neapolitan/part.rb', line 66

def formatting(&custom)
  if custom
    custom.call(self)
  else
    if specific.empty?
      stencil + default + finish
    else
      stencil + specific + finish
    end
  end
end

#specificObject

Part specific format split into array.



46
47
48
# File 'lib/neapolitan/part.rb', line 46

def specific
 @_specific ||= split_format(format)
end

#stencilObject

Template default format split into array.



56
57
58
# File 'lib/neapolitan/part.rb', line 56

def stencil
  @_stencil ||= split_format(template.stencil)
end