Class: Prawn::ManualBuilder::ExampleFile

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/manual_builder/example_file.rb

Overview

The Prawn::ManualBuilder ExampleFile class is a utility class to ease the manipulation and extraction of source code and comments from the actual example files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, filename, options = {}) ⇒ ExampleFile

Stores the file data, filename and parent, which will be either an ExampleSection or an ExamplePackage.

Available boolean options are:

:eval_source

Evals the example source code (default: true)

:full_source

Extract the full source code when true. Extract

only the code between the generate block when false (default: false)



21
22
23
24
25
26
27
28
# File 'lib/prawn/manual_builder/example_file.rb', line 21

def initialize(parent, filename, options={})
  @parent   = parent.is_a?(String) ? ExamplePackage.new(parent) : parent
  
  @filename = filename
  @data     = read_file(@parent.folder_name, filename)
  
  @options  = {:eval_source => true, :full_source => false}.merge(options)
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



10
11
12
# File 'lib/prawn/manual_builder/example_file.rb', line 10

def filename
  @filename
end

#packageObject (readonly)

Returns the value of attribute package.



10
11
12
# File 'lib/prawn/manual_builder/example_file.rb', line 10

def package
  @package
end

Instance Method Details

#eval?Boolean

Return true if the example source should be evaluated inline within the manual according to the options

Returns:

  • (Boolean)


58
59
60
# File 'lib/prawn/manual_builder/example_file.rb', line 58

def eval?
  @options[:eval_source]
end

#full_sourceObject

Return the example source code excluding the initial comments and require calls



33
34
35
# File 'lib/prawn/manual_builder/example_file.rb', line 33

def full_source
  @data.gsub(/# encoding.*?\n.*require.*?\n\n/m, "\n").strip
end

#generate_block_sourceObject

Return the example source contained inside the first generate block or the full source if no generate block is found



40
41
42
43
44
45
46
# File 'lib/prawn/manual_builder/example_file.rb', line 40

def generate_block_source
  block = @data.slice(/\w+\.generate.*? do\n(.*)end/m, 1)
    
  return full_source unless block
  
  block.gsub(/^( ){2}/, "")
end

#introduction_textObject

Retrieve the comments between the encoding declaration and the require call for example_helper.rb

Then removes the ‘#’ signs, reflows the line breaks and return the result



67
68
69
70
71
72
73
74
# File 'lib/prawn/manual_builder/example_file.rb', line 67

def introduction_text
  intro = @data.lines.grep(/^#/).join
  intro.gsub!(/\n# (?=\S)/m, ' ')
  intro.gsub!(/^#/, '')
  intro.gsub!("\n", "\n\n")
  intro.rstrip!
  intro
end

#nameObject

Returns a human friendly version of the example file name



78
79
80
# File 'lib/prawn/manual_builder/example_file.rb', line 78

def name
  @name ||= @filename[/(.*)\.rb/, 1].gsub("_", " ").capitalize
end

#parent_folder_nameObject

Returns this example’s parent original folder name



84
85
86
# File 'lib/prawn/manual_builder/example_file.rb', line 84

def parent_folder_name
  @parent.folder_name
end

#parent_nameObject

Returns the human friendly version of this example parent name



90
91
92
# File 'lib/prawn/manual_builder/example_file.rb', line 90

def parent_name
  @parent.name
end

#render(pdf) ⇒ Object

Renders this example to a pdf



96
97
98
# File 'lib/prawn/manual_builder/example_file.rb', line 96

def render(pdf)
  pdf.render_example(self)
end

#sourceObject

Return either the full_source or the generate_block_source according to the options



51
52
53
# File 'lib/prawn/manual_builder/example_file.rb', line 51

def source
  @options[:full_source] ? full_source : generate_block_source
end