Sha256: fc4c53a1e8564f1a1ce21d793f2dc9902a5c5ea70be8d8631354a8c93bfefab4

Contents?: true

Size: 1.83 KB

Versions: 25

Compression:

Stored size: 1.83 KB

Contents

module ActiveAssets
  module ActiveExpansions
    class Asset < Struct.new(:path, :type, :expansion_name, :group, :cache)

      REQUIRED_PROPS = [:path, :type, :expansion_name]
      VALID_TYPES = [:js, :css]

      class InvalidContext < StandardError
        def initialize(msg = nil)
          msg ||= "You do not have a valid context to create this asset.  Some properties are missing.  They are: #{REQUIRED_PROPS.join(', ')}."
          super(msg)
        end
      end

      class AmbiguousContext < InvalidContext
        def initilaize(missing_field)
          super("You do not have a valid context to create this asset.  Some properties are missing.  Our guess is your didn't specify a #{missing_field}")
        end
      end

      class ValidationError < InvalidContext
        attr_reader :asset, :missing_fields
        def initialize(asset, missing_fields)
          @asset, @missing_fields = asset, missing_fields
          super("#{asset} is not valid.  The following fields are missing, #{missing_fields.join(', ')}.")
        end
      end

      class InvalidAssetType < StandardError
        attr_reader :invalid_type
        def initilaize(invalid_type)
          @invalid_type = invalid_type
          super("#{invalid_type} is not a recognized asset type.")
        end
      end

      def initialize(*)
        super
        self.group ||= :all
        self.cache = self.cache == false ? false : true
      end

      def validation_error
        missing_fields = REQUIRED_PROPS.reject { |meth| send(meth) }
        if missing_fields.any?
          ValidationError.new(self, missing_fields)
        elsif !VALID_TYPES.include?(type)
          InvalidAssetType.new(type)
        end
      end

      def valid?
        !validation_error
      end

      def valid!
        e = validation_error
        raise e if e
      end

    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
active_assets-0.2.0.rc4 lib/active_assets/active_expansions/asset.rb
active_assets-0.2.0.rc3 lib/active_assets/active_expansions/asset.rb
active_assets-0.2.0.rc2 lib/active_assets/active_expansions/asset.rb
active_assets-0.2.0.rc lib/active_assets/active_expansions/asset.rb
active_assets-0.1.0 lib/active_assets/active_expansions/asset.rb