Sha256: 303a772d3140eae60d83876c9c7df8c80cba3f8457e61703305e743894218608

Contents?: true

Size: 1.62 KB

Versions: 10

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

10 entries across 10 versions & 1 rubygems

Version Path
active_assets-0.2.7 lib/active_assets/active_expansions/expansion.rb
active_assets-0.2.6 lib/active_assets/active_expansions/expansion.rb
active_assets-0.2.5 lib/active_assets/active_expansions/expansion.rb
active_assets-0.2.4 lib/active_assets/active_expansions/expansion.rb
active_assets-0.2.3 lib/active_assets/active_expansions/expansion.rb
active_assets-0.2.2 lib/active_assets/active_expansions/expansion.rb
active_assets-0.2.1 lib/active_assets/active_expansions/expansion.rb
active_assets-0.2.0 lib/active_assets/active_expansions/expansion.rb
active_assets-0.2.0.rc4 lib/active_assets/active_expansions/expansion.rb
active_assets-0.2.0.rc3 lib/active_assets/active_expansions/expansion.rb