Sha256: 9d0d73dbb264e2fbbeacaf133d6e5e0e9433d55826d4f90d8b1e44ba86819aac

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

require 'active_support'
require 'active_support/hash_with_indifferent_access'

module ActiveAssets
  module ActiveExpansions
    class Expansion
      include AssetScope
      include TypeInferrable

      attr_reader :type, :name, :assets, :namespace
      alias_method :all, :assets
      delegate :empty?, :to => :assets

      def initialize(name)
        @name = name
        @assets = []
      end

      def configure(options = {}, &blk)
        @type, @group, @namespace = options.values_at(:type, :group, :namespace)
        instance_eval(&blk) if block_given?
        self
      end

      def asset(path, options = {})
        options = HashWithIndifferentAccess.new(options)
        options.assert_valid_keys(*Asset.members)
      
        inferred_type, extension = inferred_type(path)

        options.reverse_merge!(
          :type => inferred_type || @current_type || extension || type,
          :expansion_name => name,
          :group => @current_groups
        )
        options.update(:path => path)

        members = options.values_at(*Asset.members)
        a = Asset.new(*members)

        a.valid!
        @assets << a
      end
      alias_method :a, :asset
      alias_method :`, :asset

      def group(*groups, &blk)
        @current_groups = groups
        instance_eval(&blk)
      ensure
        @current_groups = nil
      end

      def namespace(&blk)
        raise NoMethodError, "Cannot call namespace from within expansion." if block_given?
        @namespace
      end

      private
        def cleanse_path(path)
          File.join(File.dirname(path) + File.basename(path, ".#{type}"))
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_assets-0.2.0.rc2 lib/active_assets/active_expansions/expansion.rb
active_assets-0.2.0.rc lib/active_assets/active_expansions/expansion.rb
active_assets-0.1.0 lib/active_assets/active_expansions/expansion.rb