Class: Filterameter::SortDeclaration

Inherits:
Object
  • Object
show all
Defined in:
lib/filterameter/sort_declaration.rb

Overview

# Sort Declaration

Class SortDeclaration captures the sort declaration within the controller. A sort declaration is also generated from a FilterDeclaration when it is ‘sortable?`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameter_name, options) ⇒ SortDeclaration

Returns a new instance of SortDeclaration.



11
12
13
14
15
16
17
# File 'lib/filterameter/sort_declaration.rb', line 11

def initialize(parameter_name, options)
  @parameter_name = parameter_name.to_s

  validate_options(options)
  @name = options.fetch(:name, parameter_name).to_s
  @association = Array.wrap(options[:association]).presence
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



9
10
11
# File 'lib/filterameter/sort_declaration.rb', line 9

def association
  @association
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/filterameter/sort_declaration.rb', line 9

def name
  @name
end

#parameter_nameObject (readonly)

Returns the value of attribute parameter_name.



9
10
11
# File 'lib/filterameter/sort_declaration.rb', line 9

def parameter_name
  @parameter_name
end

Instance Method Details

#nested?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/filterameter/sort_declaration.rb', line 19

def nested?
  !@association.nil?
end

#to_sObject



23
24
25
26
27
28
29
30
# File 'lib/filterameter/sort_declaration.rb', line 23

def to_s
  options = {}
  options[:name] = ":#{@name}" if @parameter_name != @name
  options[:association] = @association if nested?

  (["sort :#{@parameter_name}"] + options.map { |k, v| "#{k}: #{v}" })
    .join(', ')
end