Class: Stasis

Inherits:
Object
  • Object
show all
Defined in:
lib/stasis.rb,
lib/stasis/scope.rb,
lib/stasis/plugin.rb,
lib/stasis/options.rb,
lib/stasis/dev_mode.rb,
lib/stasis/scope/action.rb,
lib/stasis/plugins/before.rb,
lib/stasis/plugins/ignore.rb,
lib/stasis/plugins/layout.rb,
lib/stasis/plugins/render.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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, *args) ⇒ Stasis

Returns a new instance of Stasis.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/stasis.rb', line 82

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

#actionObject

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



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

def action
  @action
end

#controllerObject

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



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

def controller
  @controller
end

#destObject

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



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

def dest
  @dest
end

#destinationObject

‘String` – the destination path passed to `Stasis.new`.



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

def destination
  @destination
end

#optionsObject

‘Options` – options passed to `Stasis.new`.



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

def options
  @options
end

#outputObject

‘String` – the view output from Tilt.



80
81
82
# File 'lib/stasis.rb', line 80

def output
  @output
end

#pathObject

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



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

def path
  @path
end

#pathsObject

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



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

def paths
  @paths
end

#pluginsObject

‘Array` – `Plugin` instances.



74
75
76
# File 'lib/stasis.rb', line 74

def plugins
  @plugins
end

#rootObject

‘String` – the root path passed to `Stasis.new`.



77
78
79
# File 'lib/stasis.rb', line 77

def root
  @root
end

Class Method Details

.register(plugin) ⇒ Object

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



301
302
303
304
305
306
307
# File 'lib/stasis.rb', line 301

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

.register_instance(inst) ⇒ Object



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

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

Instance Method Details

#add_plugin(plugin) ⇒ Object



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

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

#load_controllersObject



118
119
120
121
122
123
124
125
126
# File 'lib/stasis.rb', line 118

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

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

#load_pathsObject



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

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] == @destination+'/'
  end

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

#render(*only) ⇒ Object



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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/stasis.rb', line 128

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

    # Change current working directory.
    Dir.chdir(File.dirname(@path))
    
    # Trigger all plugin `before_render` events.
    trigger(:before_render)

    # Skip if `@path` set to `nil`.
    next unless @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, render_opts) { 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
    
    # Set @output instance variable for manipulation from within plugins
    @output = view
    
    # 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 @output
      # Write the rendered markup to the destination.
      if render_options[:write] != false
        File.open(@dest, 'w') do |f|
          f.write(@output)
        end
      end
      # Collect render output.
      if render_options[:collect]
        collect[relative[1..-1]] = @output
      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

    # Trigger all plugin `after_write` events. Only fires if view was created.
    trigger(:after_write)
  end

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

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

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

#trigger(type) ⇒ Object

Trigger an event on every plugin in the controller.



310
311
312
313
314
# File 'lib/stasis.rb', line 310

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