lib/psd.rb in psd-0.3.5 vs lib/psd.rb in psd-0.4.0
- old
+ new
@@ -28,10 +28,28 @@
parse_layer_images: false
}
attr_reader :file
+ # Opens the named file, parses it, and makes it available for reading. Then, closes it after you're finished.
+ # @param filename [String] the name of the file to open
+ # @return [PSD] the {PSD} object if no block was given, otherwise the value of the block
+ def self.open(filename, opts={}, &block)
+ psd = PSD.new(filename, opts)
+ psd.parse!
+
+ return psd unless block_given?
+
+ if 0 == block.arity
+ psd.instance_eval(&block)
+ else
+ yield psd
+ end
+ ensure
+ psd.close if psd
+ end
+
# Create and store a reference to our PSD file
def initialize(file, opts={})
@file = PSD::File.new(file, 'rb')
@file.seek 0 # If the file was previously used and not closed
@@ -40,10 +58,15 @@
@resources = nil
@layer_mask = nil
@parsed = false
end
+ # Close the PSD file
+ def close
+ file.close unless file.closed?
+ end
+
# There is a specific order that must be followed when parsing
# the PSD. Sections can be skipped if needed. This method will
# parse all sections of the PSD.
def parse!
header
@@ -69,19 +92,19 @@
PSD.logger.debug @header.inspect
end
# Get the Resources section, parsing if needed.
def resources
- return @resources.data unless @resources.nil?
+ return @resources unless @resources.nil?
ensure_header
@resources = Resources.new(@file)
@resources.parse
- PSD.logger.debug @resources.inspect
+ PSD.logger.debug @resources.data.inspect
- return @resources.data
+ return @resources
end
# Get the LayerMask section. Ensures the header and resources
# have been parsed first since they are required.
def layer_mask
\ No newline at end of file