Class: Metro::View

Inherits:
Object
  • Object
show all
Defined in:
lib/metro/views/view.rb

Overview

A view represents the representation of the content found within the view file. A view has the ability to save/load the content as well.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#formatObject

Return the format of the view. By default the format of the view is dictated by the format of the content that is parsed.



97
98
99
# File 'lib/metro/views/view.rb', line 97

def format
  @format || parser.format
end

#nameObject

The name of the view, which is used to influence the file path.



16
17
18
# File 'lib/metro/views/view.rb', line 16

def name
  @name
end

Instance Method Details

#contentObject

The content contained within the view.



21
22
23
24
25
26
27
# File 'lib/metro/views/view.rb', line 21

def content
  @content ||= begin
    parsed_content = parse
    parsed_content.default = {}
    parsed_content
  end
end

#content=(value) ⇒ Object

Set the content of the view.

Parameters:

  • value (Hash)

    the hash content that will represent this view



34
35
36
37
# File 'lib/metro/views/view.rb', line 34

def content=(value)
  value.default = {}
  @content = value
end

#default_writerObject



127
128
129
# File 'lib/metro/views/view.rb', line 127

def default_writer
  Views::Writers.default_writer
end

#parseObject

Parse the content found at the view path for the view.

Returns:

  • the hash of content stored within the view file.



66
67
68
# File 'lib/metro/views/view.rb', line 66

def parse
  parser.parse(view_path)
end

#parserObject

The parser for this view is one of the supported parsers. A parser is selected if the parser is capable of finding the content to load.



75
76
77
# File 'lib/metro/views/view.rb', line 75

def parser
  @parser ||= supported_parsers.find { |parser| parser.exists? view_path }
end

#saveObject

Ask the parser to save the current content of the view at the view path



89
90
91
# File 'lib/metro/views/view.rb', line 89

def save
  writer.write(view_path,content)
end

#supported_parsersObject

Supported view formats



82
83
84
# File 'lib/metro/views/view.rb', line 82

def supported_parsers
  Views::Parsers.parsers_with_no_view_fallback
end

#supported_writersObject



123
124
125
# File 'lib/metro/views/view.rb', line 123

def supported_writers
  Views::Writers.writers
end

#view_pathObject

A Scene view path is based on the view name.

Examples:

Standard View Path


class OpeningScene < Metro::Scene
end

OpeniningScene.view_path # => views/opening

Custom View Path


class ClosingScene < Metro::Scene
  view_name 'alternative'
end

ClosingScene.view_path # => views/alternative


57
58
59
# File 'lib/metro/views/view.rb', line 57

def view_path
  File.join "views", name
end

#writerObject

The writer for this view. If the view has already been parsed then use



116
117
118
119
120
121
# File 'lib/metro/views/view.rb', line 116

def writer
  @writer ||= begin
    writer_matching_existing_parser = supported_writers.find { |writer| writer.format == format }
    writer_matching_existing_parser || default_writer
  end
end