Class: PSD::Resources

Inherits:
Object
  • Object
show all
Defined in:
lib/psd/resources.rb

Overview

Parses and reads all of the Resource records in the document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Resources

Returns a new instance of Resources.



11
12
13
14
15
16
# File 'lib/psd/resources.rb', line 11

def initialize(file)
  @file = file
  @resources = {}
  @type_index = {}
  @length = nil
end

Instance Attribute Details

#resourcesObject (readonly) Also known as: data

Returns the value of attribute resources.



8
9
10
# File 'lib/psd/resources.rb', line 8

def resources
  @resources
end

Instance Method Details

#[](id) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/psd/resources.rb', line 42

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

#by_type(id) ⇒ Object



57
58
59
# File 'lib/psd/resources.rb', line 57

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

#lengthObject



61
62
63
# File 'lib/psd/resources.rb', line 61

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

#parseObject

Parses each Resource and stores them.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/psd/resources.rb', line 19

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

#saved_pathsObject

Paths exist between resource ID 2000 and 2997



51
52
53
54
55
# File 'lib/psd/resources.rb', line 51

def saved_paths
  @resources.
    select { |id, r| id >= 2000 && id <= 2997 }.
    map { |id, r| r }
end

#skipObject



38
39
40
# File 'lib/psd/resources.rb', line 38

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