class Ruhoh::Views::MasterView

Attributes

page_data[RW]

Public Class Methods

new(ruhoh, pointer_or_data) click to toggle source
# File lib/ruhoh/views/master_view.rb, line 9
def initialize(ruhoh, pointer_or_data)
  @ruhoh = ruhoh
  define_resource_collection_namespaces(ruhoh)

  if pointer_or_data['id']
    @pointer = pointer_or_data
    @page = collection.find(pointer_or_data)
    unless @page
      raise "Could not find the page with pointer: #{ pointer_or_data }" +
        "Finding this page is required because an 'id' key is being passed."
    end

    @page_data = @page.data.dup # legacy...working on removing this..
  else
    @content = pointer_or_data['content']
    @page_data = pointer_or_data
    @pointer = pointer_or_data
  end
end

Public Instance Methods

collection() click to toggle source
# File lib/ruhoh/views/master_view.rb, line 48
def collection
  @pointer["resource"] ? __send__(@pointer["resource"]) : nil
end
compiled_path() click to toggle source

Public: Formats the path to the compiled file based on the URL.

Returns: [String] The relative path to the compiled file for this page.

# File lib/ruhoh/views/master_view.rb, line 120
def compiled_path
  path = @ruhoh.compiled_path(@page_data['url'])
  path = "index.html" if path.empty?
  path += '/index.html' unless path =~ /\.\w+$/
  path
end
content() click to toggle source
# File lib/ruhoh/views/master_view.rb, line 60
def content
  render(@content || page.content)
end
debug(sub_context) click to toggle source
# File lib/ruhoh/views/master_view.rb, line 86
def debug(sub_context)
  Ruhoh::Friend.say { 
    yellow "?debug:"
    magenta sub_context.class
    cyan sub_context.inspect
  }

  "<pre>#{sub_context.class}\n#{sub_context.pretty_inspect}</pre>"
end
gist() click to toggle source
# File lib/ruhoh/views/master_view.rb, line 108
def gist
  @gist ||= Ruhoh::Views::Helpers::SimpleProxy.new({
    matcher: /^[0-9]+$/,
    function: -> input {
      "<script src=\"https://gist.github.com/#{ input }.js\"></script>"
    }
  })
end
page() click to toggle source

Delegate page to the kind of resource this view is modeling.

# File lib/ruhoh/views/master_view.rb, line 44
def page
  collection ? collection.find(@pointer) : nil
end
page_collections() click to toggle source
# File lib/ruhoh/views/master_view.rb, line 72
def page_collections
  @ruhoh.collections.acting_as_pages.map do |a|
    @ruhoh.collection(a)
  end
end
partial(name) click to toggle source

NOTE: newline ensures proper markdown rendering.

# File lib/ruhoh/views/master_view.rb, line 65
def partial(name)
  partial = partials.find(name.to_s)
  partial ?
    partial.process.to_s + "\n" :
    Ruhoh::Friend.say { yellow "partial not found: '#{name}'" } 
end
raw_code(sub_context) click to toggle source
# File lib/ruhoh/views/master_view.rb, line 96
def raw_code(sub_context)
  code = sub_context.gsub('{', '&#123;').gsub('}', '&#125;').gsub('<', '&lt;').gsub('>', '&gt;').gsub('_', "&#95;")
  "<pre><code>#{code}</code></pre>\n"
end
render_content() click to toggle source
# File lib/ruhoh/views/master_view.rb, line 39
def render_content
  render('{{{page.content}}}')
end
render_full() click to toggle source
# File lib/ruhoh/views/master_view.rb, line 29
def render_full
  if page_layouts.empty?
    render_content
  else
    page_layouts.drop(1).reduce(render(page_layouts.first.content)) do |c, l|
      render(l.content, :content => c)
    end
  end
end
to_json(sub_context) click to toggle source
# File lib/ruhoh/views/master_view.rb, line 78
def to_json(sub_context)
  sub_context.to_json
end
to_pretty_json(sub_context) click to toggle source
# File lib/ruhoh/views/master_view.rb, line 82
def to_pretty_json(sub_context)
  JSON.pretty_generate(sub_context)
end
to_slug(sub_context) click to toggle source

My Post Title ===> my-post-title Handy for transforming ids into css-classes in your views. @returns

# File lib/ruhoh/views/master_view.rb, line 104
def to_slug(sub_context)
  Ruhoh::StringFormat.clean_slug(sub_context)
end
urls() click to toggle source
# File lib/ruhoh/views/master_view.rb, line 52
def urls
  @ruhoh.collections.url_endpoints.merge({
    'base_path' => @ruhoh.config.base_path,
    'production' => @ruhoh.config["production_url"],
    'production_url' => @ruhoh.config["production_url"]
  })
end

Protected Instance Methods

page_layouts() click to toggle source
# File lib/ruhoh/views/master_view.rb, line 129
def page_layouts
  return @page_layouts unless @page_layouts.nil?

  layout = if @page_data['layout']
    layouts.find(@page_data['layout'], :all => true) or raise "Layout does not exist: #{@page_data['layout']}"
  elsif @page_data['layout'] != false
    # try default
    layouts.find(@pointer['resource'], :all => true)
  end

  @page_layouts = if layout.nil?
    []
  else
    page_layouts = [layout]
    until layout.layout.nil?
      layout = layouts.find(layout.layout) or raise "Layout does not exist: #{layout.layout}"

      raise "Layout cycle detected when rendering #{@pointer}: \n #{
        (page_layouts<<layout).map{|l| l.pointer["realpath"]}.join("\n")
      }" if page_layouts.include?(layout)

      page_layouts << layout
    end
    page_layouts
  end
end

Private Instance Methods

define_resource_collection_namespaces(ruhoh) click to toggle source

Dynamically add method proxies to resource collections This is how collections are accessed throughout mustache’s global context. Also support calling ?to_<resource> contextual block helpers

# File lib/ruhoh/views/master_view.rb, line 161
def define_resource_collection_namespaces(ruhoh)
  ruhoh.collections.all.each do |method_name|
    (class << self; self; end).class_eval do
      define_method(method_name) do
        load_collection_view_for(method_name.to_s)
      end

      define_method("to_#{method_name}") do |*args|
        resource_generator_for(method_name, *args)
      end
    end
  end
end
load_collection_view_for(resource) click to toggle source

Load collection views dynamically when calling a resources name. Uses method_missing to catch calls to resource namespace. @returns for the calling resource.

# File lib/ruhoh/views/master_view.rb, line 178
def load_collection_view_for(resource)
  view = @ruhoh.collection(resource)
  view.master = self
  view
end
resource_generator_for(resource, sub_context) click to toggle source

Transforms an Array or String of resource ids into their corresponding resource objects. Uses method_missing to catch calls to ‘to_<resource>` contextual helper. @returns the resource modelView objects or raw data hash.

# File lib/ruhoh/views/master_view.rb, line 187
def resource_generator_for(resource, sub_context)
  collection_view = load_collection_view_for(resource)
  Array(sub_context).map { |id|
    collection_view.find(id)
  }.compact
end