Class: DataDoc::Document

Inherits:
Object
  • Object
show all
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']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocument

Sets up so that default options can be read.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/data_doc/document.rb', line 24

def initialize
  @format = 'html'
  @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.



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/data_doc/document.rb', line 254

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

#connectionObject

ActiveRecord connection



52
53
54
# File 'lib/data_doc/document.rb', line 52

def connection
  @connection
end

#data_onlyObject

do not change schema; truncates tables



49
50
51
# File 'lib/data_doc/document.rb', line 49

def data_only
  @data_only
end

#formatObject

MIME-type for output



37
38
39
# File 'lib/data_doc/document.rb', line 37

def format
  @format
end

#read_onlyObject

do not change schema or data



46
47
48
# File 'lib/data_doc/document.rb', line 46

def read_only
  @read_only
end

#verboseObject

display verbose output during processing



43
44
45
# File 'lib/data_doc/document.rb', line 43

def verbose
  @verbose
end

Class Method Details

.default_layoutObject

Default layout if no layout file provided.

Simplistic valid html which accepts headers and puts content directly into body tag.



127
128
129
# File 'lib/data_doc/document.rb', line 127

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.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/data_doc/document.rb', line 86

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
  wrap_in_layout(content_html)
end

#layout=(filename) ⇒ Object

Sets the layout file option.

See #set_layout_file below for format.



75
76
77
# File 'lib/data_doc/document.rb', line 75

def layout=(filename)
  @layout_filename = filename
end

Adds a link tag to the headers



163
164
165
# File 'lib/data_doc/document.rb', line 163

def link(attrs = {})
  add_header "<link #{html_attrs(attrs)}>"
end

#meta(attrs = {}) ⇒ Object

Adds a meta tag to the headers



149
150
151
# File 'lib/data_doc/document.rb', line 149

def meta(attrs = {})
  add_header "<meta #{html_attrs(attrs)}>"    
end

#present(arel_or_str) ⇒ Object

Present a table. Pass a block to set options for display. For more information see DataDoc::Present



196
197
198
# File 'lib/data_doc/document.rb', line 196

def present(arel_or_str)
  DataDoc::Present.present(self, arel_or_str)
end

#script(attrs = {}) ⇒ Object

Adds a script tag to the headers, yielding for body of tag.



156
157
158
# File 'lib/data_doc/document.rb', line 156

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.



175
176
177
178
# File 'lib/data_doc/document.rb', line 175

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.



118
119
120
# File 'lib/data_doc/document.rb', line 118

def set_layout_file(filename)
  self.layout=(filename)
end

#store(name, opts = {}, &blk) ⇒ Object

Define a table store.



183
184
185
186
# File 'lib/data_doc/document.rb', line 183

def store(name, opts = {}, &blk)
  @stores[name.to_s] = DataDoc::Store.new(self, name, opts, &blk)
  name
end

#title(text) ⇒ Object

Sets the title tag and emits the title in a classed div.title.



141
142
143
144
# File 'lib/data_doc/document.rb', line 141

def title(text)
  add_header "<title>#{text}</title>"
  "<div class=\"title\">#{text}</div>"
end