Class: OrigenDocHelpers::ModelPageGenerator
- Inherits:
-
Object
- Object
- OrigenDocHelpers::ModelPageGenerator
- Defined in:
- lib/origen_doc_helpers/model_page_generator.rb
Class Attribute Summary collapse
-
.layout ⇒ Object
Returns the value of attribute layout.
-
.layout_options ⇒ Object
Returns the value of attribute layout_options.
-
.pages ⇒ Object
readonly
Returns the value of attribute pages.
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
- .index_page_template ⇒ Object
- .page(options) ⇒ Object
- .run(options) {|_self| ... } ⇒ Object
- .write_index ⇒ Object
Instance Method Summary collapse
- #create_page(model, options = {}) ⇒ Object
- #heading ⇒ Object
- #id ⇒ Object
-
#initialize(options) ⇒ ModelPageGenerator
constructor
A new instance of ModelPageGenerator.
- #model_page_template ⇒ Object
- #output_path ⇒ Object
- #run ⇒ Object
- #search_id ⇒ Object
-
#temporary_file ⇒ Object
Returns a Pathname to a uniquely named temporary file.
- #write_out(file, content) ⇒ Object
Constructor Details
#initialize(options) ⇒ ModelPageGenerator
Returns a new instance of ModelPageGenerator.
52 53 54 55 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 52 def initialize() = @model = [:model] end |
Class Attribute Details
.layout ⇒ Object
Returns the value of attribute layout.
5 6 7 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 5 def layout @layout end |
.layout_options ⇒ Object
Returns the value of attribute layout_options.
4 5 6 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 4 def end |
.pages ⇒ Object (readonly)
Returns the value of attribute pages.
6 7 8 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 6 def pages @pages end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
50 51 52 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 50 def model @model end |
Class Method Details
.index_page_template ⇒ Object
45 46 47 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 45 def index_page_template @index_page_template ||= "#{Origen.root!}/templates/model_index.md.erb" end |
.page(options) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 23 def page() unless [:model] fail 'The following options are required: :model' end p = new() pages[[:group]] ||= [] pages[[:group]] << p p.run end |
.run(options) {|_self| ... } ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 8 def run() @pages = {} unless [:layout] fail 'You must pass a :layout option providing an absolute path to the layout file to be used' end unless File.exist?([:layout].to_s) fail "This layout file does not exist: #{options[:layout]}" end self.layout = .delete(:layout) self. = yield self write_index unless @pages.size == 0 @pages = nil end |
.write_index ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 33 def write_index f = "#{Origen.root}/web/content/models.md" Origen.log.info "Building models index page: #{f}" t = Origen.compile index_page_template, layout: layout, layout_options: , no_group_pages: pages.delete(nil), pages: pages File.write(f, t) end |
Instance Method Details
#create_page(model, options = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 61 def create_page(model, = {}) base = "#{Origen.root}/web/content/#{options[:path]}" output_file = base + '.md' json_file = base + '_data.json' Origen.log.info "Building model page: #{output_file}" t = Origen.compile model_page_template, layout: self.class.layout, layout_options: self.class., heading: heading, search_id: search_id, model: model, breadcrumbs: [:breadcrumbs], path: [:path], origen_path: [:origen_path] write_out(output_file, t) Origen.log.info "Building JSON page: #{json_file}" File.open(json_file, 'w') do |f| f.puts model.to_json end model.sub_blocks.each do |name, block| path = [:path] + "/#{name}" if [:origen_path].empty? origen_path = name.to_s else origen_path = [:origen_path] + ".#{name}" end create_page block, breadcrumbs: [:breadcrumbs] + [[name, path]], path: path, origen_path: origen_path end end |
#heading ⇒ Object
145 146 147 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 145 def heading model.class.to_s end |
#id ⇒ Object
141 142 143 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 141 def id @id ||= model.class.to_s.symbolize.to_s.gsub('::', '_') end |
#model_page_template ⇒ Object
153 154 155 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 153 def model_page_template @model_page_template ||= "#{Origen.root!}/templates/model_page.md.erb" end |
#output_path ⇒ Object
149 150 151 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 149 def output_path "models/#{id}" end |
#run ⇒ Object
57 58 59 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 57 def run create_page(@model, top: true, breadcrumbs: [['Top', output_path]], path: output_path, origen_path: '') end |
#search_id ⇒ Object
137 138 139 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 137 def search_id "model_#{id}" end |
#temporary_file ⇒ Object
Returns a Pathname to a uniquely named temporary file
132 133 134 135 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 132 def temporary_file # Ensure this is unique so that is doesn't clash with parallel compile processes Pathname.new "#{Origen.root}/tmp/model_#{id}_compiler_#{Process.pid}_#{Time.now.to_f}" end |
#write_out(file, content) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/origen_doc_helpers/model_page_generator.rb', line 97 def write_out(file, content) d = Pathname.new(file).dirname.to_s FileUtils.mkdir_p(d) unless File.exist?(d) yaml = '' # Remove any frontmatter inherited from the caller's layout, this is being done via disk for # large files due to previous issues with sub'ing on large files that contain many registers File.open(file, 'w') do |f| frontmatter_done = false frontmatter_open = false content.each_line do |line| if frontmatter_done f.puts line elsif frontmatter_open if line =~ /^\s*---\s*$/ frontmatter_done = true else yaml += line end else if line =~ /^\s*---\s*$/ frontmatter_open = true elsif !line.strip.empty? frontmatter_done = true end end end end # Write out an attribute file containing the search ID to pass it to nanoc yaml += "search_id: model_#{id}" File.write(file.sub(/\.[^\.]*$/, '.yaml'), yaml) end |