Sha256: 0da6a9ee3d9d49ae2895b71d8bbde158b8de3c5cce5c6be5fd9e29d4728e063b

Contents?: true

Size: 1.38 KB

Versions: 13

Compression:

Stored size: 1.38 KB

Contents

module Hem
  class << self
    attr_accessor :asset_applicators

    # Utility method to access (with initialization) the asset applicator registry.
    # This allows you to register new asset applicator methods on a per-project basis.
    # For example:
    #
    #     Hem.asset_applicators.register /.*\.zip/ do |file|
    #       Dir.chdir File.dirname(file) do
    #         shell "unzip", file
    #       end
    #     end
    #
    # @see Hem::AssetApplicatorRegistry
    # @return [Hem::AssetApplicatorRegistry] Applicator registry cotnainer
    def asset_applicators
      @asset_applicators ||= AssetApplicatorRegistry.new
    end
  end

  private

  # Thin wrapper over a hash to provide a means to "register" asset applicators
  class AssetApplicatorRegistry < Array
    # Register a new asset applicator
    # @param [String] The name of the applicator
    # @param [Regexp] Pattern to match against asset filename.
    # @yield The block to be executed when an asset matches the pattern.
    def register name, pattern, &block
      self << AssetApplicator.new(name, pattern, block)
    end
  end

  class AssetApplicator
    attr_reader :name

    def initialize(name, pattern, block)
      @name = name
      @pattern = pattern
      @block = block
    end

    def matches?(file)
      @pattern.match(file)
    end

    def call(file, opts = {})
      @block.call file, opts
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
hem-1.2.4 lib/hem/asset_applicator.rb
hem-1.2.3 lib/hem/asset_applicator.rb
hem-1.2.2 lib/hem/asset_applicator.rb
hem-1.2.1 lib/hem/asset_applicator.rb
hem-1.2.0 lib/hem/asset_applicator.rb
hem-1.1.2 lib/hem/asset_applicator.rb
hem-1.1.1 lib/hem/asset_applicator.rb
hem-1.1.0 lib/hem/asset_applicator.rb
hem-1.0.1.beta6 lib/hem/asset_applicator.rb
hem-1.0.1.beta5 lib/hem/asset_applicator.rb
hem-1.0.1.beta4 lib/hem/asset_applicator.rb
hem-1.0.1.beta3 lib/hem/asset_applicator.rb
hem-1.0.1.beta2 lib/hem/asset_applicator.rb