Sha256: 6694d93ec1f2192d6cd3217c40ece2eaa167f2b0a12b53acd3781407e7f2d7d0

Contents?: true

Size: 1.13 KB

Versions: 8

Compression:

Stored size: 1.13 KB

Contents

require 'psd/resource_section'
require 'psd/resource'
require 'psd/resources/base'

class PSD
  # Parses and reads all of the Resource records in the document.
  class Resources
    attr_reader :resources
    alias :data :resources
    
    def initialize(file)
      @file = file
      @resources = {}
      @type_index = {}
      @length = nil
    end

    # Parses each Resource and stores them.
    def parse
      finish = length + @file.tell

      while @file.tell < finish
        resource = Resource.new(@file)
        resource.parse

        resource_end = @file.tell + resource.size

        name = Resource::Section.factory(@file, resource)
        @resources[resource.id] = resource
        @type_index[name] = resource.id unless name.nil?

        @file.seek resource_end
      end

      @file.seek finish if @file.tell != finish
    end

    def skip
      @file.seek length, IO::SEEK_CUR
    end

    def [](id)
      if id.is_a?(Symbol)
        by_type(id)
      else
        @resources[id]
      end
    end

    def by_type(id)
      @resources[@type_index[id]]
    end

    def length
      @length ||= @file.read_int
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
psd-3.8.0 lib/psd/resources.rb
psd-3.7.0 lib/psd/resources.rb
psd-3.6.0 lib/psd/resources.rb
psd-3.5.0 lib/psd/resources.rb
psd-3.4.0 lib/psd/resources.rb
psd-3.3.3 lib/psd/resources.rb
psd-3.3.2 lib/psd/resources.rb
psd-3.3.1 lib/psd/resources.rb