class PrismQA::DesignImageSet

Design image sets need to be able to report on the images they contain

Public Instance Methods

allow(image) click to toggle source
# File gem/lib/prism_qa/imageset.rb, line 39
def allow(image)
  # Ensure that image objects have an "attribute" field, among other things
  raise IncompatibilityError, 'Tried to add a non- DesignImage object to a DesignImageSet' unless image.is_a? DesignImage

  # no duplicates allowed
  if @images.map { |i| [i.id, i.attribute] }.include? [image.id, image.attribute]
    raise OperationalError, "Tried to add an image with duplicate ID '#{image.id}' and attribute '#{image.attribute}'"
  end
end
cache_image_attributes() click to toggle source

cache the image attributes

# File gem/lib/prism_qa/imageset.rb, line 55
def cache_image_attributes
  return if @cache_valid

  # make a hash -- hash[image id] = list of attributes defined for this image id
  # we use this for convenience later
  @attributes_by_id = {}
  @images.each do |img|
    proper_key = img.id.to_s
    @attributes_by_id[proper_key] = [] unless @attributes_by_id.key? proper_key
    @attributes_by_id[proper_key] << img.attribute
  end

  @cache_valid = true
end
contained_attributes() click to toggle source

Get the list of unique attributes contained by the images within

# File gem/lib/prism_qa/imageset.rb, line 50
def contained_attributes
  @images.map(&:attribute).uniq
end
images_for_attribute(attribute) click to toggle source

get the list of images that are valid for a particular attribute

# File gem/lib/prism_qa/imageset.rb, line 71
def images_for_attribute(attribute)
  cache_image_attributes

  # return the pared-down list
  @images.select do |img|
    # this covers nil == nil and attribute == attribute
    next true if img.attribute == attribute

    # if there is no attribute for this image, it should be pulled in
    #        ... unless there's an exact match elsewhere in the set.
    next true if img.attribute.nil? && !(@attributes_by_id[img.id.to_s].include? attribute)

    false
  end
end