class CustomRankOrder
  def initialize(attribute, ordered_values)
    @attribute = attribute
    @ordered_values = ordered_values
  end

  def sql
    Arel.sql case_statement
  end

  private

  attr_accessor :attribute, :ordered_values

  def case_statement
    ["CASE", case_conditions, "END"].join(" ")
  end

  def case_conditions
    ordered_values.map.with_index do |release, index|
      "WHEN #{attribute}='#{release}' THEN '#{index+1}'"
    end.join(" ")
  end
end
