Class: ParamsReady::Ordering::OrderingParameter

Inherits:
Parameter::ArrayParameter show all
Defined in:
lib/params_ready/ordering/ordering.rb

Instance Attribute Summary

Attributes inherited from Parameter::AbstractParameter

#definition

Instance Method Summary collapse

Methods inherited from Parameter::ArrayParameter

#<<

Methods included from Parameter::ArrayParameter::ArrayLike

#[], #[]=

Methods included from Parameter::ComplexParameter

#update_child

Methods inherited from Parameter::Parameter

#allows_undefined?, #definite_default?, #eligible_for_output?, #find_in_hash, #format, #format_self_permitted, #freeze, #hash, #hash_key, #initialize, #inspect_content, #is_default?, #is_definite?, #is_nil?, #is_undefined?, #memo, #memo!, #nil_default?, #populate_other, #set_from_input, #set_value, #to_hash_if_eligible, #unwrap, #unwrap_or, #wrap_output

Methods inherited from Parameter::AbstractParameter

#==, #dup, #initialize, #inspect, intent_for_children, #match?, #populate, #to_hash, #update_if_applicable, #update_in

Methods included from Extensions::Freezer

#freeze_variable, #freeze_variables, #variables_to_freeze

Methods included from Parameter::FromHash

#set_from_hash

Methods included from Extensions::Freezer::InstanceMethods

#freeze

Constructor Details

This class inherits a constructor from ParamsReady::Parameter::Parameter

Instance Method Details

#by_columnsObject



29
30
31
32
33
# File 'lib/params_ready/ordering/ordering.rb', line 29

def by_columns
  bare_value.each_with_index.each_with_object(Hash.new([:none, nil])) do |(tuple, index), hash|
    hash[tuple[0].unwrap] = [tuple[1].unwrap, index]
  end
end

#filtered(name) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/params_ready/ordering/ordering.rb', line 46

def filtered(name)
  bare_value.map do |tuple|
    tuple.format(Intent.instance(:backend))
  end.partition do |item|
    next item[0] == name
  end
end

#inverted_orderObject



73
74
75
# File 'lib/params_ready/ordering/ordering.rb', line 73

def inverted_order
  update_in(inverted_order_value, [])
end

#inverted_order_valueObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/params_ready/ordering/ordering.rb', line 59

def inverted_order_value
  bare_value.map do |tuple|
    name = tuple.first.unwrap
    case tuple.second.unwrap
    when :asc
      [name, :desc]
    when :desc
      [name, :asc]
    else
      raise ParamsReadyError, "Unexpected ordering: '#{tuple.second.unwrap}'"
    end
  end
end

#marshal(intent) ⇒ Object



12
13
14
15
16
17
# File 'lib/params_ready/ordering/ordering.rb', line 12

def marshal(intent)
  arr = to_array(intent)
  return arr unless intent.marshal?(name_for_formatter)

  arr.join(definition.class::COLUMN_DELIMITER)
end

#order_for(name) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/params_ready/ordering/ordering.rb', line 121

def order_for(name)
  order = bare_value.find do |tuple|
    tuple[0].unwrap == name
  end
  return :none if order.nil?

  order[1].unwrap
end

#prepend_item(name, direction, array) ⇒ Object



54
55
56
57
# File 'lib/params_ready/ordering/ordering.rb', line 54

def prepend_item(name, direction, array)
  return array if direction == :none
  [[name, direction], *array]
end

#reordered(name, new_dir) ⇒ Object



96
97
98
# File 'lib/params_ready/ordering/ordering.rb', line 96

def reordered(name, new_dir)
  update_in(reordered_value(name, new_dir), [])
end

#reordered_value(name, new_dir) ⇒ Object



91
92
93
94
# File 'lib/params_ready/ordering/ordering.rb', line 91

def reordered_value(name, new_dir)
  _, save = filtered(name)
  prepend_item(name, new_dir, save)
end

#restriction_from_context(context) ⇒ Object



100
101
102
103
104
105
# File 'lib/params_ready/ordering/ordering.rb', line 100

def restriction_from_context(context)
  restriction = context.to_restriction
  return restriction if restriction.name_permitted? :ordering || !required?

  Restriction.permit({ ordering: [] })
end

#to_arel(default_table, context: Restriction.blanket_permission, inverted: false) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/params_ready/ordering/ordering.rb', line 107

def to_arel(default_table, context: Restriction.blanket_permission, inverted: false)
  ordering = inverted ? inverted_order : self
  ordering.to_array_with_context(context).flat_map do |(column_name, direction)|
    column = definition.columns[column_name]
    attribute = column.attribute(column_name, default_table, context)
    column.clauses(attribute, direction, inverted: inverted)
  end
end

#to_array(intent = Intent.instance(:backend)) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/params_ready/ordering/ordering.rb', line 19

def to_array(intent = Intent.instance(:backend))
  arr = bare_value
  arr.map do |tuple|
    name = tuple.first.unwrap
    next unless intent.name_permitted?(name) || definition.required?(name)

    tuple.format(intent)
  end.compact
end

#to_array_with_context(context) ⇒ Object



116
117
118
119
# File 'lib/params_ready/ordering/ordering.rb', line 116

def to_array_with_context(context)
  intent = Intent.instance(:backend).clone(restriction: restriction_from_context(context))
  to_array(intent.for_children(self))
end

#toggle_schema(schema) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/params_ready/ordering/ordering.rb', line 35

def toggle_schema(schema)
  case schema
  when :desc
    [:desc, :asc]
  when :asc
    [:asc, :desc]
  else
    [:none, :none]
  end
end

#toggled_order(name) ⇒ Object



87
88
89
# File 'lib/params_ready/ordering/ordering.rb', line 87

def toggled_order(name)
  update_in(toggled_order_value(name), [])
end

#toggled_order_value(name) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/params_ready/ordering/ordering.rb', line 77

def toggled_order_value(name)
  drop, save = filtered(name)

  old_dir = drop.count > 0 ? drop.first[1] : :none
  primary, secondary = toggle_schema(definition.columns[name.to_sym].ordering)
  new_dir = old_dir == primary ? secondary : primary

  prepend_item(name, new_dir, save)
end