Class: Utopia::Tags::Gallery::Container

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/utopia/tags/gallery/container.rb

Instance Method Summary collapse

Constructor Details

#initialize(node, path, processes = Processes::DEFAULT) ⇒ Container

Returns a new instance of Container.



82
83
84
85
86
87
# File 'lib/utopia/tags/gallery/container.rb', line 82

def initialize(node, path, processes = Processes::DEFAULT)
	@node = node
	@path = path
	
	@processes = processes
end

Instance Method Details

#each(options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/utopia/tags/gallery/container.rb', line 99

def each(options = {})
	return to_enum(:each, options) unless block_given?
	
	options[:filter] ||= /(jpg|png)$/i

	paths = []
	local_path = @node.local_path(@path)

	Dir.entries(local_path).each do |filename|
		next unless filename.match(options[:filter])

		path = Path.new(@path + filename)

		process!(path, options[:process]) if options[:process]

		yield path
	end
end

#metadataObject



89
90
91
92
93
94
95
96
97
# File 'lib/utopia/tags/gallery/container.rb', line 89

def 
	 = @node.local_path(@path + "gallery.yaml")
		
	if File.exist? 
		return YAML::load(File.read())
	else
		return {}
	end
end

#process!(image_path, processes) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/utopia/tags/gallery/container.rb', line 118

def process!(image_path, processes)
	# Create the local cache directory if it doesn't exist already
	local_cache_path = @node.local_path(image_path.cache_root)
		
	unless File.exist? local_cache_path
		FileUtils.mkdir local_cache_path
	end
		
	# Calculate the new name for the processed image
	local_original_path = @node.local_path(image_path.original)
		
	if processes.kind_of? String
		processes = processes.split(",").collect{|p| p.split(":")}
	end
		
	processes.each do |process_name, extension|
		process_name = process_name.to_sym
			
		process = @processes[process_name]
		extension ||= process.default_extension(image_path)
			
		image_path.extensions[process_name] = extension if extension
			
		local_processed_path = @node.local_path(image_path.processed(process_name))
			
		unless File.exists? local_processed_path
			image = Magick::ImageList.new(local_original_path)
			image.scene = 0

			processed_image = process.call(image)
			processed_image.write(local_processed_path)

			# Run GC to free up any memory.
			image = processed_image = nil
			GC.start if defined? GC
		end
	end
end