Class: Sanity::Groq::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/sanity/groq/order.rb

Constant Summary collapse

RESERVED =
%i[order]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Order

Returns a new instance of Order.



16
17
18
19
20
21
22
# File 'lib/sanity/groq/order.rb', line 16

def initialize(**args)
  args.slice(*RESERVED).then do |opts|
    @order = opts[:order]
  end

  @val = +""
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



14
15
16
# File 'lib/sanity/groq/order.rb', line 14

def order
  @order
end

#valObject (readonly)

Returns the value of attribute val.



14
15
16
# File 'lib/sanity/groq/order.rb', line 14

def val
  @val
end

Class Method Details

.call(**args) ⇒ Object



7
8
9
# File 'lib/sanity/groq/order.rb', line 7

def call(**args)
  new(**args).call
end

Instance Method Details

#callObject

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sanity/groq/order.rb', line 24

def call
  return unless order

  raise ArgumentError, "order must be hash" unless order.is_a?(Hash)

  order.to_a.each_with_index do |(key, sort), idx|
    val << " | order(#{key} #{sort})".then do |str|
      idx.positive? ? str : str.strip
    end
  end

  val
end