Class: Stasis

Inherits:
Object
  • Object
show all
Defined in:
lib/stasis.rb,
lib/stasis/scope.rb,
lib/stasis/server.rb,
lib/stasis/plugin.rb,
lib/stasis/options.rb,
lib/stasis/dev_mode.rb,
lib/stasis/scope/action.rb,
lib/stasis/plugins/ignore.rb,
lib/stasis/plugins/before.rb,
lib/stasis/plugins/render.rb,
lib/stasis/plugins/layout.rb,
lib/stasis/plugins/helpers.rb,
lib/stasis/plugins/instead.rb,
lib/stasis/plugins/priority.rb,
lib/stasis/scope/controller.rb

Overview

`Controller` provides a scope for `controller.rb` files.

Stasis will still create a `Controller` instance for directories without a `controller.rb` file, mostly for the purposes of resolving paths and triggering plugin events.

Defined Under Namespace

Classes: Action, Before, Controller, DevMode, Helpers, Ignore, Instead, Layout, Options, Plugin, Priority, Render, Scope, Server

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Stasis) initialize(root, *args)

A new instance of Stasis



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/stasis.rb', line 74

def initialize(root, *args)
  @options = {}
  @options = args.pop if args.last.is_a?(::Hash)
  
  @root = File.expand_path(root)
  @destination = args[0] || @root + '/public'
  @destination = File.expand_path(@destination, @root)

  load_paths unless options[:development]

  # Create plugin instances.
  @plugins = Plugin.plugins.collect { |klass| klass.new(self) }

  self.class.register_instance(self)
  load_controllers
end

Instance Attribute Details

- (Object) action

`Action` -- changes with each iteration of the main loop within `Stasis#render`.



51
52
53
# File 'lib/stasis.rb', line 51

def action
  @action
end

- (Object) controller

`Controller` -- set to the same instance for the lifetime of the `Stasis` instance.



54
55
56
# File 'lib/stasis.rb', line 54

def controller
  @controller
end

- (Object) destination

`String` -- the destination path passed to `Stasis.new`.



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

def destination
  @destination
end

- (Object) options

`Options` -- options passed to `Stasis.new`.



66
67
68
# File 'lib/stasis.rb', line 66

def options
  @options
end

- (Object) path

`String` -- changes with each iteration of the main loop within `Stasis#render`.



60
61
62
# File 'lib/stasis.rb', line 60

def path
  @path
end

- (Object) paths

`Array` -- all paths in the project that Stasis will act upon.



63
64
65
# File 'lib/stasis.rb', line 63

def paths
  @paths
end

- (Object) plugins

`Array` -- `Plugin` instances.



69
70
71
# File 'lib/stasis.rb', line 69

def plugins
  @plugins
end

- (Object) root

`String` -- the root path passed to `Stasis.new`.



72
73
74
# File 'lib/stasis.rb', line 72

def root
  @root
end

Class Method Details

+ (Object) register(plugin)

Add a plugin to all existing controller instances. This method should be called by all external plugins.



287
288
289
290
291
# File 'lib/stasis.rb', line 287

def self.register(plugin)
  @instances.each do |stasis|
    stasis.add_plugin(plugin)
  end
end

+ (Object) register_instance(inst)



274
275
276
277
# File 'lib/stasis.rb', line 274

def self.register_instance(inst)
  @instances ||= []
  @instances << inst
end

Instance Method Details

- (Object) add_plugin(plugin)



279
280
281
282
283
# File 'lib/stasis.rb', line 279

def add_plugin(plugin)
  plugin = plugin.new(self)
  plugins << plugin
  controller._bind_plugin(plugin, :controller_method)
end

- (Object) load_controllers



110
111
112
113
114
115
116
117
118
# File 'lib/stasis.rb', line 110

def load_controllers
  # Create a controller instance.
  @controller = Controller.new(self)

  # Reload controllers
  Dir["#{@root}/**/controller.rb"].each do |path|
    @controller._add(path) unless path[0..@destination.length-1] == @destination
  end
end

- (Object) load_paths



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/stasis.rb', line 91

def load_paths
  # Create an `Array` of paths that Stasis will act upon.
  @paths = Dir.glob("#{@root}/**/*", File::FNM_DOTMATCH)

  # Reject paths that are directories or within the destination directory.
  @paths.reject! do |path|
    !File.file?(path) || path[0..@destination.length-1] == @destination
  end

  # Reject paths that are controllers.
  @paths.reject! do |path|
    if File.basename(path) == 'controller.rb'
      true
    else
      false
    end
  end
end

- (Object) render(*only)



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/stasis.rb', line 120

def render(*only)
  collect = {}
  render_options = {}

  if only.last.is_a?(::Hash)
    render_options = only.pop
  end

  # Resolve paths given via the `only` parameter.
  only = only.inject([]) do |array, path|
    # If `path` is a regular expression...
    if path.is_a?(::Regexp)
      array << path
    # If `root + path` exists...
    elsif (path = File.expand_path(path, root)) && File.exists?(path)
      array << path
    # If `path` exists...
    elsif File.exists?(path)
      array << path
    end
    array
  end

  if only.empty?
    # Remove old generated files.
    FileUtils.rm_rf(destination)
  end
  
  # Trigger all plugin `before_all` events.
  trigger(:before_all)

  @paths.uniq.each do |path|
    @path = path

    # If `only` parameters given...
    unless only.empty?
      # Skip iteration unless there is a match.
      next unless only.any? do |o|
        # Regular expression match.
        (o.is_a?(::Regexp) && @path =~ o) ||
        (
          o.is_a?(::String) && (
            # File match.
            @path == o ||
            # Directory match.
            @path[0..o.length-1] == o
          )
        )
      end
    end

    # Create an `Action` instance, the scope for rendering the view.
    @action = Action.new(self, :params => render_options[:params])

    # Set the extension if the `@path` extension is supported by [Tilt][ti].
    ext =
      Tilt.mappings.keys.detect do |ext|
        File.extname(@path)[1..-1] == ext
      end
    
    # Trigger all plugin `before_render` events.
    trigger(:before_render)

    # Skip if `@path` set to `nil`.
    next unless @path

    # Change current working directory.
    Dir.chdir(File.dirname(@path))

    # Render the view.
    view =
      # If the path has an extension supported by [Tilt][ti]...
      if ext
        # If the controller calls `render` within the `before` block for this
        # path, receive output from `@action._render`.
        #
        # Otherwise, render the file located at `@path`.
        render_opts = {:callback => false}.merge(:template => Options.get_template_option(ext))
        begin
          output = @action._render || @action.render(@path, render_opts)
        rescue
          # If rendering the view caused an exception write the path out before exiting.
          puts "Exception rendering view #{@path}"
          raise
        end

        # If a layout was specified via the `layout` method...
        if @action._layout
          # Render the layout with a block for the layout to `yield` to.
          @action.render(@action._layout) { output }
        # If a layout was not specified...
        else
          output
        end
      # If the path does not have an extension supported by [Tilt][ti] and `render` was
      # called within the `before` block for this path...
      elsif @action._render
        @action._render
      end
    
    # Trigger all plugin `after_render` events.
    trigger(:after_render)

    # Cut the `root` out of the `path` to get the relative destination.
    relative = @path[root.length..-1]

    # Add `destination` (as specified from `Stasis.new`) to front of relative
    # destination.
    dest = "#{destination}#{relative}"

    # Cut off the extension if the extension is supported by [Tilt][ti].
    dest =
      if ext && File.extname(dest) == ".#{ext}"
        dest[0..-1*ext.length-2]
      else
        dest
      end

    # Create the directories leading up to the destination.
    if render_options[:write] != false
      FileUtils.mkdir_p(File.dirname(dest))
    end

    # If markup was rendered...
    if view
      # Write the rendered markup to the destination.
      if render_options[:write] != false
        File.open(dest, 'w') do |f|
          f.write(view)
        end
      end
      # Collect render output.
      if render_options[:collect]
        collect[relative[1..-1]] = view
      end
    # If markup was not rendered and the path exists...
    elsif File.exists?(@path)
      # Copy the file located at the path to the destination path.
      if render_options[:write] != false
        FileUtils.cp(@path, dest)
      end
    end
  end

  # Trigger all plugin `after_all` events, passing the `Stasis` instance.
  trigger(:after_all)

  # Unset class-level instance variables.
  @action, @path = nil, nil

  # Respond with collected render output if `collect` option given.
  collect if render_options[:collect]
end

- (Object) trigger(type)

Trigger an event on every plugin in the controller.



294
295
296
297
298
# File 'lib/stasis.rb', line 294

def trigger(type)
  each_priority do |priority|
    @controller._send_to_plugin(priority, type)
  end
end