class Noticent::View

Constants

FRONTMATTER

Attributes

content[R]
data[R]

these are the attributes we should use in most cases

filename[R]
raw_content[R]
raw_data[R]
rendered_data[R]
template_content[R]
view_content[R]

these are mostly for debug and testing purposes

Public Class Methods

new(filename, template_filename: "", channel:) click to toggle source
# File lib/noticent/view.rb, line 19
def initialize(filename, template_filename: "", channel:)
  raise ViewNotFound, "view #{filename} not found" unless File.exist?(filename)
  raise ViewNotFound, "template #{template_filename} not found" if template_filename != "" && !File.exist?(template_filename)
  raise ArgumentError, "channel is nil" if channel.nil?

  @filename = filename
  @view_content = File.read(filename)
  @template_content = template_filename != "" ? File.read(template_filename) : "<%= @content %>"
  @template_filename = template_filename != "" ? template_filename : ""
  @channel = channel
end

Public Instance Methods

process(context) click to toggle source
# File lib/noticent/view.rb, line 31
def process(context)
  parse
  render_data(context)
  read_data
  # TODO this is nasty. we need to refactor to have an independent render context which somehow merges the binding with the channel.
  @channel.data = @data
  render_content(context)
end

Private Instance Methods

parse() click to toggle source
# File lib/noticent/view.rb, line 55
def parse
  result = {}

  # is there front matter?
  match = FRONTMATTER.match(@view_content)
  if !match.nil?
    result[:frontmatter] = match[1]
    result[:content] = match[2]
  else
    result[:content] = @view_content
  end

  @raw_data = result[:frontmatter]
  @raw_content = result[:content]
end
read_data() click to toggle source
# File lib/noticent/view.rb, line 71
def read_data
  if @raw_data.nil?
    @data = nil
  else
    raise ArgumentError, "read_data was called before rendering" if @rendered_data.nil?

    data = ::YAML.safe_load(@rendered_data)
    @data = data.deep_symbolize_keys
  end
end
render_content(context) click to toggle source
# File lib/noticent/view.rb, line 42
def render_content(context)
  @content = @channel.render_within_context(template: @template_content, content: @raw_content, context: context)
end
render_data(context) click to toggle source
# File lib/noticent/view.rb, line 46
def render_data(context)
  if @raw_data.nil?
    @rendered_data = nil
    return
  end

  @rendered_data = @channel.render_within_context(template: nil, content: @raw_data, context: context)
end