Class: JSONAPIonify::Api::SortField

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapionify/api/sort_field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ SortField

Returns a new instance of SortField.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/jsonapionify/api/sort_field.rb', line 6

def initialize(name)
  @str = name.to_s
  if name.to_s.start_with? '-'
    @name  = name.to_s[1..-1].to_sym
    @order = :desc
  else
    @name  = name.to_sym
    @order = :asc
  end
  freeze
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/jsonapionify/api/sort_field.rb', line 4

def name
  @name
end

#orderObject (readonly)

Returns the value of attribute order.



4
5
6
# File 'lib/jsonapionify/api/sort_field.rb', line 4

def order
  @order
end

Instance Method Details

#==(other) ⇒ Object



18
19
20
21
# File 'lib/jsonapionify/api/sort_field.rb', line 18

def ==(other)
  other.class == self.class &&
    other.name == self.name
end

#===(other) ⇒ Object



23
24
25
26
# File 'lib/jsonapionify/api/sort_field.rb', line 23

def ===(other)
  other.class == self.class &&
    other.name == self.name
end

#contains_arelObject



41
42
43
44
45
46
47
48
# File 'lib/jsonapionify/api/sort_field.rb', line 41

def contains_arel
  case @order
  when :asc
    :gteq
  when :desc
    :lteq
  end
end

#contains_operatorObject



32
33
34
35
36
37
38
39
# File 'lib/jsonapionify/api/sort_field.rb', line 32

def contains_operator
  case @order
  when :asc
    :>=
  when :desc
    :<=
  end
end

#id?Boolean

Returns:



28
29
30
# File 'lib/jsonapionify/api/sort_field.rb', line 28

def id?
  name == :id
end

#outside_arelObject



59
60
61
62
63
64
65
66
# File 'lib/jsonapionify/api/sort_field.rb', line 59

def outside_arel
  case @order
  when :asc
    :gt
  when :desc
    :lt
  end
end

#outside_operatorObject



50
51
52
53
54
55
56
57
# File 'lib/jsonapionify/api/sort_field.rb', line 50

def outside_operator
  case @order
  when :asc
    :>
  when :desc
    :<
  end
end

#to_hObject



72
73
74
# File 'lib/jsonapionify/api/sort_field.rb', line 72

def to_h
  { name => order }
end

#to_sObject



68
69
70
# File 'lib/jsonapionify/api/sort_field.rb', line 68

def to_s
  @str
end