Sha256: 96123edbe48a048b41d858f6e69475113b92d9de166667ccaa0ac4b4393f3572

Contents?: true

Size: 1.47 KB

Versions: 20

Compression:

Stored size: 1.47 KB

Contents

class Stasis
  class Before < Plugin

    before_all :before_all
    before_render :before_render
    controller_method :before
    priority 1

    def initialize(stasis)
      @stasis = stasis
      @blocks = {}
    end

    # This method is bound to all controllers. Stores a block in the `@blocks` `Hash`,
    # where the key is a path and the value is an `Array` of blocks.
    def before(*paths, &block)
      paths = [ nil ] if paths.empty?
      if block
        paths.each do |path|
          path = @stasis.controller._resolve(path, true)
          @blocks[path] ||= []
          @blocks[path] << [ @stasis.path, block ]
        end
      end
    end

    # This event triggers before all files render. When a `before` call receives a path
    # that does not exist, we want to create that file dynamically. This method adds
    # those dynamic paths to the `paths` `Array`.
    def before_all
      new_paths = (@blocks || {}).keys.select do |path|
        path.is_a?(::String)
      end
      @stasis.paths = (@stasis.paths + new_paths).uniq
    end

    # This event triggers before each file renders through Stasis. It finds matching
    # blocks for the `path` and evaluates those blocks using the `action` as a scope.
    def before_render
      matches = _match_key?(@blocks, @stasis.path)
      matches.each do |group|
        group.each do |(path, block)|
          if _within?(path)
            @stasis.action.instance_eval(&block)
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
stasis-0.1.19 lib/stasis/plugins/before.rb
stasis-0.1.18 lib/stasis/plugins/before.rb
stasis-0.1.17 lib/stasis/plugins/before.rb
stasis-0.1.16 lib/stasis/plugins/before.rb
stasis-0.1.15 lib/stasis/plugins/before.rb
stasis-0.1.14 lib/stasis/plugins/before.rb
stasis-0.1.13 lib/stasis/plugins/before.rb
stasis-0.1.12 lib/stasis/plugins/before.rb
stasis-0.1.11 lib/stasis/plugins/before.rb
stasis-0.1.10 lib/stasis/plugins/before.rb
stasis-0.1.9 lib/stasis/plugins/before.rb
stasis-0.1.8 lib/stasis/plugins/before.rb
stasis-0.1.7 lib/stasis/plugins/before.rb
stasis-0.1.6 lib/stasis/plugins/before.rb
stasis-0.1.5 lib/stasis/plugins/before.rb
stasis-0.1.4 lib/stasis/plugins/before.rb
stasis-0.1.3 lib/stasis/plugins/before.rb
stasis-0.1.2 lib/stasis/plugins/before.rb
stasis-0.1.1 lib/stasis/plugins/before.rb
stasis-0.1.0 lib/stasis/plugins/before.rb