Skip to content

Instantly share code, notes, and snippets.

@dangalipo
Last active August 29, 2015 14:07
Show Gist options
  • Save dangalipo/460ed281f69b10625dc6 to your computer and use it in GitHub Desktop.
Save dangalipo/460ed281f69b10625dc6 to your computer and use it in GitHub Desktop.
class ProductImageCollection
def initialize(images)
self.images = load_images(images)
end
def images_for(group: nil, color: nil, size: nil)
processor = ProductImageCollectionProcessor.new(images.clone)
processor.filter(group, color)
processor.resize(size) if size.present?
processor.images
end
private
def load_images(images)
images.map{|img| ProductImage.new(img) }
end
attr_accessor :images
class ProductImageCollectionProcessor
attr_reader :images
def initialize(images)
self.images = images
end
def filter(group, color)
filter_by_group(group) if group.present?
filter_by_color(color) if color.present?
end
def resize(size)
images.map!{|img| img.resize_to(size) }
end
private
attr_writer :images
def filter_by_group(group)
images.select! do |image|
image.path =~ /\/#{group}\//
end
end
def filter_by_color(color)
images.select! do |image|
image.name =~ /#{color}/
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment