Sha256: 06a05e27ebe98b05b15f41710a407850d2c0bf018238a4161b3d4fc0f7f21e70

Contents?: true

Size: 1019 Bytes

Versions: 2

Compression:

Stored size: 1019 Bytes

Contents

module Sortify
  def sort_option(name, body)
    @sort_options ||= []
    name = name.to_sym 
    
    if self.respond_to?(name)
      raise ArgumentError, "Could not create sort option, There is an existing method with this name."
    end
    
    begin
      scope name, body
    rescue ArgumentError => e
      raise e
    else
      @sort_options << name
    end
  end
  
  def default_sort_option(name)
    @default_sort_option = name.to_sym
  end
  
  def sort_options
    return @sort_options
  end

  def sortify(sort_option = "")
    sort_option = sort_option.to_sym unless sort_option == nil
    
    if @sort_options.include? sort_option
      self.send(sort_option)
    elsif sort_option.empty?
      begin
        self.send(@default_sort_option)
      rescue
        raise NoMethodError, "The default sort option you provided, '#{@default_sort_option.to_s}', does not exist."
      end
    else
      raise NoMethodError, "The sort option you provided, '#{sort_option}', does not exist."
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sortify-0.1.6 lib/sortify.rb
sortify-0.1.5 lib/sortify.rb