Class: Rayo::Models::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/rayo/models/page.rb

Direct Known Subclasses

StatusPage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage, parent, lang, path) ⇒ Page

Returns a new instance of Page.



11
12
13
14
15
16
17
18
19
# File 'lib/rayo/models/page.rb', line 11

def initialize( storage, parent, lang, path )
  @storage = storage
  @parent = parent
  @lang = lang
  @path = path

  @parts = {}
  @layouts = {}
end

Instance Attribute Details

#langObject (readonly)

Returns the value of attribute lang.



9
10
11
# File 'lib/rayo/models/page.rb', line 9

def lang
  @lang
end

#parentObject (readonly)

Returns the value of attribute parent.



9
10
11
# File 'lib/rayo/models/page.rb', line 9

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/rayo/models/page.rb', line 9

def path
  @path
end

#storageObject (readonly)

Returns the value of attribute storage.



9
10
11
# File 'lib/rayo/models/page.rb', line 9

def storage
  @storage
end

Instance Method Details

#[](key) ⇒ Object



114
115
116
117
118
# File 'lib/rayo/models/page.rb', line 114

def []( key )
  key = key.to_s

  context[ key ] || params[ key ]
end

#child(slug) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/rayo/models/page.rb', line 45

def child( slug )
  @children_cache ||= {}
  return @children_cache[ slug ] if @children_cache.include? slug

  page = Rayo::Models::Page.new( @storage, self, @lang, @path + [slug] )
  page = nil if page.file.nil? && page.directories.empty?

  @children_cache[ slug ] = page
end

#childrenObject



29
30
31
# File 'lib/rayo/models/page.rb', line 29

def children
  @children ||= @storage.find_pages( directories, @lang ).map{|name| child( name ) }
end

#contextObject



102
103
104
105
106
107
108
# File 'lib/rayo/models/page.rb', line 102

def context
  return @context if @context

  @context = @parent ? @parent.context.merge({ 'status' => 200 }) : { 'status' => 200 }
  @context.merge! load_context( file ) if file
  @context
end

#descendant(relative_path) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rayo/models/page.rb', line 21

def descendant( relative_path )
  relative_path.inject( self ) do |page,slug|
    return nil unless page

    page.child( slug )
  end
end

#directoriesObject



55
56
57
# File 'lib/rayo/models/page.rb', line 55

def directories
  @directories ||= @storage.find_page_dirs( @parent.directories, @lang, @path.last )
end

#fileObject



59
60
61
# File 'lib/rayo/models/page.rb', line 59

def file
  @file ||= @storage.find_page_file( @parent.directories, @lang, @path.last )
end

#find_part(format, part_name, inherit = false) ⇒ List<Rayo::Models::Renderable>

Find page part for given format

Parameters:

  • part (String, Symbol)

    format

  • part (String, Symbol)

    name

  • search (Boolean)

    in super classes

Returns:



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rayo/models/page.rb', line 75

def find_part( format, part_name, inherit = false )
  page = self
  part = self.parts(format)[part_name]

  while inherit && !part && page.parent do
    page = page.parent
    part = page.parts(format)[part_name]
  end

  part
end

#layout(format) ⇒ Object



110
111
112
# File 'lib/rayo/models/page.rb', line 110

def layout(format)
  @layouts[format.to_s] ||= @storage.layout( @lang, context['layout'], format )
end

#paramsObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rayo/models/page.rb', line 87

def params
  return @params if @params

  segments = file[0..-(File.extname(file).length+1)].split(/[\/\\]/)[-@path.size..-1] || raise( "File doesn't correspond to path" )

  @params = {}

  segments.each_with_index do |segment,i|
    @params[segment[1..-1]] = @path[i] if segment[0..0] == '%'
  end

  @params['path'] = path
  @params
end

#parserObject



128
129
130
# File 'lib/rayo/models/page.rb', line 128

def parser
  @parser ||= Radius::Parser.new( Rayo::TagContext.new( self ), :tag_prefix => 'r' )
end

#parts(format) ⇒ List<Rayo::Models::Renderable>

Get page parts for given format

Parameters:

  • part (String, Symbol)

    format

Returns:



66
67
68
# File 'lib/rayo/models/page.rb', line 66

def parts( format )
  @parts[format.to_s] ||= file ? @storage.find_page_parts( file, @lang, format.to_s ) : {}
end

#relative(url) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rayo/models/page.rb', line 33

def relative( url )
  path = url.split '/'

  if path.empty?
    @storage.root_page( @lang )
  elsif path.first && path.first.empty?
    @storage.root_page( @lang ).descendant( path[1..-1] )
  else
    descendant( path )
  end
end

#render(format) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/rayo/models/page.rb', line 120

def render( format )
  if l = layout( format )
    l.render( parser )
  else
    "Layout: '#{context['layout']}'' not found with format '#{format}'"
  end
end