Last active
August 29, 2015 14:07
-
-
Save dangalipo/460ed281f69b10625dc6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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