class Prawn::ManualBuilder::ExampleFile

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

Attributes

filename[R]
package[R]

Public Class Methods

new(parent, filename, options={}) click to toggle source

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)

# File lib/prawn/manual_builder/example_file.rb, line 20
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

Public Instance Methods

eval?() click to toggle source

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

# File lib/prawn/manual_builder/example_file.rb, line 57
def eval?
  @options[:eval_source]
end
full_source() click to toggle source

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

# File lib/prawn/manual_builder/example_file.rb, line 32
def full_source
  @data.gsub(/# encoding.*?\n.*require.*?\n\n/m, "\n").strip
end
generate_block_source() click to toggle source

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

# File lib/prawn/manual_builder/example_file.rb, line 39
def generate_block_source
  block = @data.slice(/\w+\.generate.*? do\n(.*)end/m, 1)
    
  return full_source unless block
  
  block.gsub(/^( ){2}/, "")
end
introduction_text() click to toggle source

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

# File lib/prawn/manual_builder/example_file.rb, line 66
def introduction_text
  intro = @data.lines.grep(/^#/)

  intro.shift if intro.first =~ /^#!/
  intro.shift if intro.first =~ /coding:/

  intro = intro.join

  intro.gsub!(/\n# (?=\S)/m, ' ')
  intro.gsub!(/^#/, '')
  intro.gsub!("\n", "\n\n")
  intro.rstrip!
  intro
end
name() click to toggle source

Returns a human friendly version of the example file name

# File lib/prawn/manual_builder/example_file.rb, line 83
def name
  @name ||= @filename[/(.*)\.rb/, 1].gsub("_", " ").capitalize
end
parent_folder_name() click to toggle source

Returns this example's parent original folder name

# File lib/prawn/manual_builder/example_file.rb, line 89
def parent_folder_name
  @parent.folder_name
end
parent_name() click to toggle source

Returns the human friendly version of this example parent name

# File lib/prawn/manual_builder/example_file.rb, line 95
def parent_name
  @parent.name
end
render(pdf) click to toggle source

Renders this example to a pdf

# File lib/prawn/manual_builder/example_file.rb, line 101
def render(pdf)
  pdf.render_example(self)
end
source() click to toggle source

Return either the #full_source or the #generate_block_source according to the options

# File lib/prawn/manual_builder/example_file.rb, line 50
def source
  @options[:full_source] ? full_source : generate_block_source
end

Private Instance Methods

read_file(folder_name, filename) click to toggle source

Read the data from a file in a given package

# File lib/prawn/manual_builder/example_file.rb, line 109
def read_file(folder_name, filename)
  data = File.read(File.expand_path(File.join(
    Prawn::ManualBuilder.manual_dir, folder_name, filename)))

  data.encode(::Encoding::UTF_8)
end