Sha256: b16b59862345701c78dd16afbed4b6bea2ad6dd91fe54e4f0a6862fd964124f9

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

module Polymer
  # Represents a single source file used in a sprite.
  #
  class Source

    # @return [String] The sprite name; the filename sans-extension.
    attr_reader :name

    # @return [Pathname] The path to the source file on disk.
    attr_reader :path

    # Creates a new Source instance.
    #
    # @param [Pathname] path
    #   The path the the source image.
    #
    def initialize(path)
      @path = Pathname.new(path)
      @name = @path.basename(@path.extname).to_s
    end

    # Returns the RMagick image instance representing the source.
    #
    # @return [Magick::Image]
    #
    def image
      assert_file!
      @image ||= Magick::Image.read(@path).first
    end

    # Returns a digest which represents the sprite name and file contents.
    #
    # @return [String]
    #
    def digest
      assert_file!
      Digest::SHA256.hexdigest(@name + Digest::SHA256.file(@path).to_s)
    end

    #######
    private
    #######

    # Checks that the source file exists and is a file.
    #
    # @raise [Polymer::MissingSource]
    #   Raised if the source file is not present, or is not a file.
    #
    def assert_file!
      raise MissingSource,
        "Couldn't find the source file `#{@path}'" unless @path.file?
    end

  end # Source
end # Polymer

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
polymer-1.0.0.beta.6 lib/polymer/source.rb
polymer-1.0.0.beta.5 lib/polymer/source.rb
polymer-1.0.0.beta.4 lib/polymer/source.rb
polymer-1.0.0.beta.3 lib/polymer/source.rb