Sha256: 5daf7f08f18bb02bbdbf5e50bcf8cc3951f1203765564bec56e47807a7dd0235

Contents?: true

Size: 1.18 KB

Versions: 14

Compression:

Stored size: 1.18 KB

Contents

class Stasis
  class Priority < Plugin

    after_all :after_all
    before_all :before_all
    controller_method :priority
    priority 2

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

    # This event triggers before all files render through Stasis. Collect matching
    # `paths` and sort those `paths` by priority.
    def before_all
      @stasis.paths.collect! do |path|
        priority = 0
        matches = _match_key?(@priorities, path)
        matches.each do |(within, value, force)|
          priority = value if _within?(within, path) || force
        end
        [ path, priority ]
      end
      @stasis.paths.sort! { |a, b| b[1] <=> a[1] }
      @stasis.paths.collect! { |(path, priority)| path }
    end

    # This method is bound to all controllers. Stores a priority integer in the
    # `@@priorities` `Hash`, where the key is a path and the value is the priority.
    def priority(hash)
      hash = hash.inject({}) do |hash, (key, value)|
        force = key[0..0] == '/'
        key = @stasis.controller._resolve(key)
        hash[key] = [ @stasis.path, value, force ] if key
        hash
      end
      @priorities.merge!(hash)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
stasis-0.1.13 lib/stasis/plugins/priority.rb
stasis-0.1.12 lib/stasis/plugins/priority.rb
stasis-0.1.11 lib/stasis/plugins/priority.rb
stasis-0.1.10 lib/stasis/plugins/priority.rb
stasis-0.1.9 lib/stasis/plugins/priority.rb
stasis-0.1.8 lib/stasis/plugins/priority.rb
stasis-0.1.7 lib/stasis/plugins/priority.rb
stasis-0.1.6 lib/stasis/plugins/priority.rb
stasis-0.1.5 lib/stasis/plugins/priority.rb
stasis-0.1.4 lib/stasis/plugins/priority.rb
stasis-0.1.3 lib/stasis/plugins/priority.rb
stasis-0.1.2 lib/stasis/plugins/priority.rb
stasis-0.1.1 lib/stasis/plugins/priority.rb
stasis-0.1.0 lib/stasis/plugins/priority.rb