Class: Bartender::Page

Inherits:
Object
  • Object
show all
Includes:
ViewHelpers
Defined in:
lib/bartender/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ViewHelpers

#asset_url, #link_image, #link_script, #link_stylesheet, #partial

Constructor Details

#initialize(page, sprockets_env) ⇒ Page

Returns a new instance of Page.



7
8
9
10
11
12
13
14
# File 'lib/bartender/page.rb', line 7

def initialize(page, sprockets_env)
  @page = page
  @sprockets_env = sprockets_env
  return false unless File.exists?(@page) #does the page actual exist?
  @data = File.read(@page)
  self.read_yaml
  @klass = Tilt[page]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Function read_yaml



68
69
70
71
72
73
74
# File 'lib/bartender/page.rb', line 68

def method_missing(m, *args)
  if m =~ /^(\w+)=$/
    instance_variable_set "@#{$1}", args[0]
  else
    instance_variable_get "@#{m}"
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/bartender/page.rb', line 5

def config
  @config
end

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'lib/bartender/page.rb', line 5

def content
  @content
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/bartender/page.rb', line 5

def data
  @data
end

#outputObject (readonly)

Returns the value of attribute output.



5
6
7
# File 'lib/bartender/page.rb', line 5

def output
  @output
end

#pageObject (readonly)

Returns the value of attribute page.



5
6
7
# File 'lib/bartender/page.rb', line 5

def page
  @page
end

#sprockets_envObject (readonly)

Returns the value of attribute sprockets_env.



5
6
7
# File 'lib/bartender/page.rb', line 5

def sprockets_env
  @sprockets_env
end

Instance Method Details

#compileObject

Funciton initialize



16
17
18
19
20
21
22
23
24
25
# File 'lib/bartender/page.rb', line 16

def compile
  output_path = @page.split('/')
  output_path[0] =Bartender::DEFAULTS['output']
  file_name = output_path[-1].split('.')
  file_name.delete_at(-1)
  output_path[-1] = file_name.join('.')
  @output = output_path.join('/')
  File.delete @output if File.exists? @output
  File.open(@output, 'w') {|f| f.write self.render}
end

#find_layoutObject

Function render



39
40
41
42
43
44
45
46
47
# File 'lib/bartender/page.rb', line 39

def find_layout
  layouts = Dir.glob(Bartender::DEFAULTS['layouts'] + "/#{self.layout}.*")
  if layouts.length > 0
    return layouts[0]
  else
    $stderr.puts "ERROR: Could not find layout file '#{self.layout}' in #{Bartender::DEFAULTS['layouts']} for page #{@page} "
    exit 0
  end
end

#layout_specified?Boolean

Function find_layout

Returns:

  • (Boolean)


49
50
51
# File 'lib/bartender/page.rb', line 49

def layout_specified?
 self.layout
end

#read_yamlObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bartender/page.rb', line 53

def read_yaml
  begin
    if @data =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
      @config = YAML.load($1)
      @config.each_key do |k|
        instance_variable_set ('@'<<k).to_sym, @config[k]
      end
      @content = @data
      @content.gsub!(/^(---\s*\n.*?\n?)^(---\s*$\n?)/m, '')
    end
  rescue => e
    puts "YAML Exception reading #{@page}: #{e.message}"
  end
end

#renderObject

Funciton compile



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bartender/page.rb', line 27

def render
  return nil if @klass.nil? #make sure tilt knows what to render, otherwise return nil
  template = @klass.new {@content}
  compiled = template.render(self)
  if self.layout_specified?
    layout = Tilt.new self.find_layout
    return layout.render(self) {compiled}
  else
    return compiled
  end
end