Class: ParamsReady::Ordering::Column
- Inherits:
-
Object
- Object
- ParamsReady::Ordering::Column
- Defined in:
- lib/params_ready/ordering/column.rb
Constant Summary collapse
- DIRECTIONS =
Set.new %i(none asc desc)
- NULLS_FIRST =
{ null: 0, not_null: 1 }.freeze
- NULLS_LAST =
{ null: 1, not_null: 0 }.freeze
Instance Attribute Summary collapse
-
#nulls ⇒ Object
readonly
Returns the value of attribute nulls.
-
#ordering ⇒ Object
readonly
Returns the value of attribute ordering.
-
#pk ⇒ Object
readonly
Returns the value of attribute pk.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #attribute(name, default_table, context) ⇒ Object
- #clauses(attribute, direction, inverted: false) ⇒ Object
- #expression(name) ⇒ Object
-
#initialize(ordering, arel_table:, expression:, nulls: :default, required: false, pk: false) ⇒ Column
constructor
A new instance of Column.
- #null_substitution_values(policy, inverted) ⇒ Object
Constructor Details
#initialize(ordering, arel_table:, expression:, nulls: :default, required: false, pk: false) ⇒ Column
Returns a new instance of Column.
14 15 16 17 18 19 20 21 22 |
# File 'lib/params_ready/ordering/column.rb', line 14 def initialize(ordering, arel_table:, expression:, nulls: :default, required: false, pk: false) raise ParamsReadyError, "Invalid ordering value: #{ordering}" unless DIRECTIONS.include? ordering @ordering = ordering @table = arel_table @expression = expression @nulls = nulls @required = required @pk = pk end |
Instance Attribute Details
#nulls ⇒ Object (readonly)
Returns the value of attribute nulls.
9 10 11 |
# File 'lib/params_ready/ordering/column.rb', line 9 def nulls @nulls end |
#ordering ⇒ Object (readonly)
Returns the value of attribute ordering.
9 10 11 |
# File 'lib/params_ready/ordering/column.rb', line 9 def ordering @ordering end |
#pk ⇒ Object (readonly)
Returns the value of attribute pk.
9 10 11 |
# File 'lib/params_ready/ordering/column.rb', line 9 def pk @pk end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
9 10 11 |
# File 'lib/params_ready/ordering/column.rb', line 9 def required @required end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
9 10 11 |
# File 'lib/params_ready/ordering/column.rb', line 9 def table @table end |
Instance Method Details
#attribute(name, default_table, context) ⇒ Object
28 29 30 31 32 |
# File 'lib/params_ready/ordering/column.rb', line 28 def attribute(name, default_table, context) arel_table = table || default_table arel_builder = Helpers::ArelBuilder::Attribute.instance(expression(name), arel_table: arel_table) arel_builder.to_arel(arel_table, context, self) end |
#clauses(attribute, direction, inverted: false) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/params_ready/ordering/column.rb', line 34 def clauses(attribute, direction, inverted: false) clause = attribute.send direction if nulls == :default [clause] else values = null_substitution_values(nulls, inverted) nulls_last = Arel::Nodes::Case.new .when(attribute.eq(nil)).then(values[:null]) .else(values[:not_null]) [nulls_last, clause] end end |
#expression(name) ⇒ Object
24 25 26 |
# File 'lib/params_ready/ordering/column.rb', line 24 def expression(name) @expression || name end |
#null_substitution_values(policy, inverted) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/params_ready/ordering/column.rb', line 48 def null_substitution_values(policy, inverted) case [policy, inverted] when [:first, false], [:last, true] NULLS_FIRST when [:last, false], [:first, true] NULLS_LAST else raise ParamsReadyError, "Unimplemented null handling policy: '#{nulls}' (inverted: #{inverted})" end end |