Class: Brite::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/brite/controller.rb

Overview

The Controller class is the primary Brite class, handling the generation of site files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Controller

New Controller.

Parameters:

  • options (Hash) (defaults to: {})

    Controller options.

Options Hash (options):

  • :location (String)

    Location of brite project.

  • :output (String)

    Redirect all output to this directory.



27
28
29
30
31
32
33
34
# File 'lib/brite/controller.rb', line 27

def initialize(options={})
  @location = options[:location] || Dir.pwd
  @output   = options[:output]
  #@dryrun   = options[:dryrun]
  #@trace    = options[:trace]

  @site = Site.new(location, :url=>options[:url])
end

Instance Attribute Details

#locationObject (readonly)

Directory of site files on disk.



40
41
42
# File 'lib/brite/controller.rb', line 40

def location
  @location
end

#outputObject (readonly)

Where to save results. Usually the templates are shadowed, which means the ouput is the same a location.



44
45
46
# File 'lib/brite/controller.rb', line 44

def output
  @output
end

#siteObject (readonly)

Returns an instance of Site.



37
38
39
# File 'lib/brite/controller.rb', line 37

def site
  @site
end

Instance Method Details

#buildObject

Build the site.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/brite/controller.rb', line 72

def build
  if trace?
    puts "Layouts: " + site.layouts.map{ |layout| layout.name }.join(", ")
    puts "Parts:   " + site.parts.map{ |part| part.name }.join(", ")
    puts "Pages:   " + site.pages.map{ |page| page.file }.join(", ")
    puts "Posts:   " + site.posts.map{ |post| post.file }.join(", ")
    puts
  end

  Dir.chdir(location) do
    site.posts.each do |post|
      puts "Rendering #{post.file}" if $DEBUG
      save(post)
    end
    site.pages.each do |page|
      puts "Rendering #{page.file}" if $DEBUG
      save(page)
    end
  end
  puts "\n#{site.pages.size + site.posts.size} Files: #{site.pages.size} Pages, #{site.posts.size} Posts"
end

#configObject

Access to configuration file data.



52
53
54
# File 'lib/brite/controller.rb', line 52

def config
  site.config
end

#dryrun?Boolean

Is ‘dryrun` mode on? Checks the global variable `$DRYRUN`.

Returns:

  • (Boolean)


62
63
64
# File 'lib/brite/controller.rb', line 62

def dryrun?
  $DRYRUN #@dryrun
end

#save(model) ⇒ Object

Save page/post redering to disk.

Parameters:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/brite/controller.rb', line 99

def save(model)
  file = output ? File.join(output, model.output) : model.output
  text = model.to_s

  if File.exist?(file)
    current = File.read(file)
  else
    current = nil
  end

  if current != text or $FORCE
    if dryrun?
      puts "    dry run: #{file}"
    else
      puts "      write: #{file}"
      File.open(file, 'w'){ |f| f << text }
    end
  else
    puts "  unchanged: #{file}"
  end
end

#trace?Boolean

Is ‘trace` mode on? Checks global variable `$TRACE`.

Returns:

  • (Boolean)


67
68
69
# File 'lib/brite/controller.rb', line 67

def trace?
  $TRACE #@trace
end

#urlObject

URL of site as set in initializer or configuration file.



57
58
59
# File 'lib/brite/controller.rb', line 57

def url
  site.url
end