Class: Kashiwamochi::Sort
- Inherits:
-
Object
- Object
- Kashiwamochi::Sort
- Defined in:
- lib/kashiwamochi/sort.rb
Constant Summary collapse
- DIRS =
{:asc => 'asc', :desc => 'desc'}.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #asc? ⇒ Boolean
- #desc? ⇒ Boolean
- #dir ⇒ Object
- #dir=(value) ⇒ Object
-
#initialize(key, dir = nil) ⇒ Sort
constructor
A new instance of Sort.
- #inspect ⇒ Object
- #key ⇒ Object
- #key=(value) ⇒ Object
- #to_query ⇒ Object
- #toggle ⇒ Object
- #toggle! ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(key, dir = nil) ⇒ Sort
Returns a new instance of Sort.
5 6 7 8 |
# File 'lib/kashiwamochi/sort.rb', line 5 def initialize(key, dir = nil) self.key = key self.dir = dir end |
Class Method Details
.parse(value) ⇒ Object
64 65 66 67 |
# File 'lib/kashiwamochi/sort.rb', line 64 def self.parse(value) key, dir = sanitize(value).split(/\s+/, 2) new(key, dir) end |
.sanitize(value) ⇒ Object
55 56 57 58 |
# File 'lib/kashiwamochi/sort.rb', line 55 def self.sanitize(value) value = value.first if value.is_a?(Array) value.to_s.strip end |
Instance Method Details
#desc? ⇒ Boolean
39 40 41 |
# File 'lib/kashiwamochi/sort.rb', line 39 def desc? @dir == DIRS[:desc] end |
#dir ⇒ Object
18 19 20 |
# File 'lib/kashiwamochi/sort.rb', line 18 def dir @dir end |
#dir=(value) ⇒ Object
22 23 24 |
# File 'lib/kashiwamochi/sort.rb', line 22 def dir=(value) @dir = Sort.sanitize_dir(value) end |
#inspect ⇒ Object
51 52 53 |
# File 'lib/kashiwamochi/sort.rb', line 51 def inspect "#<Sort #{key}: #{dir}>" end |
#key ⇒ Object
10 11 12 |
# File 'lib/kashiwamochi/sort.rb', line 10 def key @key end |
#key=(value) ⇒ Object
14 15 16 |
# File 'lib/kashiwamochi/sort.rb', line 14 def key=(value) @key = Sort.sanitize(value) end |
#to_query ⇒ Object
47 48 49 |
# File 'lib/kashiwamochi/sort.rb', line 47 def to_query "#{key} #{dir}" end |
#toggle ⇒ Object
31 32 33 |
# File 'lib/kashiwamochi/sort.rb', line 31 def toggle self.dup.toggle! end |
#toggle! ⇒ Object
26 27 28 29 |
# File 'lib/kashiwamochi/sort.rb', line 26 def toggle! @dir = asc? ? DIRS[:desc] : DIRS[:asc] self end |
#valid? ⇒ Boolean
43 44 45 |
# File 'lib/kashiwamochi/sort.rb', line 43 def valid? !@key.empty? end |