Class: DataDoc::Document
- Inherits:
-
Object
- Object
- DataDoc::Document
- Defined in:
- lib/data_doc/document.rb
Overview
Class for processing and rendering data_docs.
Manages processing and formatting of the document.
Defined Under Namespace
Classes: IsolatedLayoutContext
Constant Summary collapse
- OUTPUT_TYPES =
Available mime types that can be generated.
['html', 'pdf']
Instance Attribute Summary collapse
-
#connection ⇒ Object
ActiveRecord connection.
-
#data_only ⇒ Object
do not change schema; truncates tables.
-
#format ⇒ Object
MIME-type for output.
-
#read_only ⇒ Object
do not change schema or data.
-
#verbose ⇒ Object
display verbose output during processing.
Class Method Summary collapse
-
.default_layout ⇒ Object
Default layout if no layout file provided.
Instance Method Summary collapse
-
#generate(content_io) ⇒ Object
Generate the output for the given input content.
-
#initialize ⇒ Document
constructor
Sets up so that default options can be read.
-
#layout=(filename) ⇒ Object
Sets the layout file option.
-
#link(attrs = {}) ⇒ Object
Adds a link tag to the headers.
-
#meta(attrs = {}) ⇒ Object
Adds a meta tag to the headers.
-
#present(arel_or_str, &blk) ⇒ Object
Present a table.
-
#prince=(prince_path) ⇒ Object
Sets path to find the pdf generator.
-
#script(attrs = {}) ⇒ Object
Adds a script tag to the headers, yielding for body of tag.
-
#set_connection(settings) ⇒ Object
Specifies ActiveRecord connection settings.
-
#set_layout_file(filename) ⇒ Object
Set layout file (from the filename, not the template content itself).
-
#store(name, opts = {}, &blk) ⇒ Object
Define a table store.
-
#title(text) ⇒ Object
Sets the title tag and emits the title in a classed div.title.
Constructor Details
#initialize ⇒ Document
Sets up so that default options can be read.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/data_doc/document.rb', line 25 def initialize @format = 'html' @prince_path = 'prince' @verbose = false @read_only = false @data_only = false @connection = nil @layout_filename = nil @headers = Array.new @stores = Hash.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (protected)
Allow use of relation names as calls for adding or querying.
If no args then returns an arel for querying, otherwise assumes add.
269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/data_doc/document.rb', line 269 def method_missing(name, *args, &block) table_name = name.to_s if @stores.has_key?(table_name) if args.empty? return @stores[table_name].arel else @stores[table_name].insert(*args) unless @read_only end else super end end |
Instance Attribute Details
#connection ⇒ Object
ActiveRecord connection
54 55 56 |
# File 'lib/data_doc/document.rb', line 54 def connection @connection end |
#data_only ⇒ Object
do not change schema; truncates tables
51 52 53 |
# File 'lib/data_doc/document.rb', line 51 def data_only @data_only end |
#format ⇒ Object
MIME-type for output
39 40 41 |
# File 'lib/data_doc/document.rb', line 39 def format @format end |
#read_only ⇒ Object
do not change schema or data
48 49 50 |
# File 'lib/data_doc/document.rb', line 48 def read_only @read_only end |
#verbose ⇒ Object
display verbose output during processing
45 46 47 |
# File 'lib/data_doc/document.rb', line 45 def verbose @verbose end |
Class Method Details
.default_layout ⇒ Object
Default layout if no layout file provided.
Simplistic valid html which accepts headers and puts content directly into body tag.
142 143 144 |
# File 'lib/data_doc/document.rb', line 142 def self.default_layout "<!DOCTYPE html>\n<html>\n<head><%= yield :head %></head>\n<body>\n<%= yield %>\n</body>\n</html>" end |
Instance Method Details
#generate(content_io) ⇒ Object
Generate the output for the given input content.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/data_doc/document.rb', line 95 def generate(content_io) erb_content = content_io.read begin # @store.taint # self.untrust mark_down = ERB.new(erb_content, 0).result(binding.taint) # TODO: $SAFE = 4 ensure # self.trust # @store.untaint end content_html = RDiscount.new(mark_down).to_html html = wrap_in_layout(content_html) case @format when 'pdf' html_to_pdf(html) else html end end |
#layout=(filename) ⇒ Object
Sets the layout file option.
See #set_layout_file below for format.
77 78 79 |
# File 'lib/data_doc/document.rb', line 77 def layout=(filename) @layout_filename = filename end |
#link(attrs = {}) ⇒ Object
Adds a link tag to the headers
178 179 180 |
# File 'lib/data_doc/document.rb', line 178 def link(attrs = {}) add_header "<link #{html_attrs(attrs)} />" end |
#meta(attrs = {}) ⇒ Object
Adds a meta tag to the headers
164 165 166 |
# File 'lib/data_doc/document.rb', line 164 def (attrs = {}) add_header "<meta #{html_attrs(attrs)} />" end |
#present(arel_or_str, &blk) ⇒ Object
Present a table. Pass a block to set options for display. For more information see DataDoc::Present
211 212 213 |
# File 'lib/data_doc/document.rb', line 211 def present(arel_or_str, &blk) DataDoc::Present.present(self, arel_or_str, &blk) end |
#prince=(prince_path) ⇒ Object
Sets path to find the pdf generator.
84 85 86 |
# File 'lib/data_doc/document.rb', line 84 def prince=(prince_path) @prince_path = prince_path end |
#script(attrs = {}) ⇒ Object
Adds a script tag to the headers, yielding for body of tag.
171 172 173 |
# File 'lib/data_doc/document.rb', line 171 def script(attrs = {}) add_header "<script #{html_attrs(attrs)}>#{"\n"+yield+"\n" if block_given?}</script>" end |
#set_connection(settings) ⇒ Object
Specifies ActiveRecord connection settings.
190 191 192 193 |
# File 'lib/data_doc/document.rb', line 190 def set_connection(settings) ActiveRecord::Base.establish_connection(settings) @connection = ActiveRecord::Base.connection end |
#set_layout_file(filename) ⇒ Object
Set layout file (from the filename, not the template content itself)
<% set_layout_file 'myfile.html.erb' %>
Content is html, with ERB placeholders to yield to add content at appropriate points.
- <%= yield :head %>
-
marks where to place any headers defined (see #meta below for an example)
- <%= yield :content %> or <%= yield %>
-
marks where to place the processed content from the input file.
133 134 135 |
# File 'lib/data_doc/document.rb', line 133 def set_layout_file(filename) self.layout=(filename) end |
#store(name, opts = {}, &blk) ⇒ Object
Define a table store.
198 199 200 201 |
# File 'lib/data_doc/document.rb', line 198 def store(name, opts = {}, &blk) @stores[name.to_s] = DataDoc::Store.store(self, name, opts, &blk) name end |
#title(text) ⇒ Object
Sets the title tag and emits the title in a classed div.title.
156 157 158 159 |
# File 'lib/data_doc/document.rb', line 156 def title(text) add_header "<title>#{text}</title>" "<div class=\"title\">#{text}</div>" end |