Sha256: 256e003081448b7b38de73343406d895a9e257d971b9fefb369bd69a09704bf0

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

module Kashiwamochi
  class Sort
    DIRS = {:asc => 'asc', :desc => 'desc'}.freeze

    def initialize(key, dir = nil)
      self.key = key
      self.dir = dir
    end

    def key
      @key
    end

    def key=(value)
      @key = Sort.sanitize(value)
    end

    def dir
      @dir
    end

    def dir=(value)
      @dir = Sort.sanitize_dir(value)
    end

    def toggle!
      @dir = asc? ? DIRS[:desc] : DIRS[:asc]
      self
    end

    def toggle
      self.dup.toggle!
    end

    def asc?
      @dir == DIRS[:asc]
    end

    def desc?
      @dir == DIRS[:desc]
    end

    def valid?
      !@key.empty?
    end

    def to_query
      "#{key} #{dir}"
    end

    def inspect
      "#<Sort #{key}: #{dir}>"
    end

    def self.sanitize(value)
      value = value.first if value.is_a?(Array)
      value.to_s.strip
    end

    def self.sanitize_dir(dir)
      sanitize(dir).downcase != DIRS[:desc] ? DIRS[:asc] : DIRS[:desc]
    end

    def self.parse(value)
      key, dir = sanitize(value).split(/\s+/, 2)
      new(key, dir)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
kashiwamochi-0.4.3 lib/kashiwamochi/sort.rb
kashiwamochi-0.4.2 lib/kashiwamochi/sort.rb
kashiwamochi-0.4.1 lib/kashiwamochi/sort.rb
kashiwamochi-0.4.0 lib/kashiwamochi/sort.rb
kashiwamochi-0.3.0 lib/kashiwamochi/sort.rb