Class: OrderQuery::Column
- Inherits:
-
Object
- Object
- OrderQuery::Column
- Defined in:
- lib/order_query/column.rb
Overview
An order column (sort column)
Instance Attribute Summary collapse
-
#custom_sql ⇒ Object
readonly
Returns the value of attribute custom_sql.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#order_enum ⇒ Object
readonly
Returns the value of attribute order_enum.
Instance Method Summary collapse
- #default_nulls_direction(reverse = false) ⇒ :first, :last
-
#direction(reverse = false) ⇒ Object
rubocop:enable Metrics/ParameterLists,Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity rubocop:enable Metrics/MethodLength.
-
#enum_side(value, side, strict = true) ⇒ Array
Valid order values before / after the given value.
-
#initialize(scope, attr_name, *vals_and_or_dir, unique: nil, nulls: false, sql: nil) ⇒ Column
constructor
A new instance of Column.
- #inspect ⇒ Object
- #nullable? ⇒ Boolean
- #nulls_direction(reverse = false) ⇒ :first, :last
- #unique? ⇒ Boolean
Constructor Details
#initialize(scope, attr_name, *vals_and_or_dir, unique: nil, nulls: false, sql: nil) ⇒ Column
Returns a new instance of Column.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/order_query/column.rb', line 29 def initialize(scope, attr_name, *vals_and_or_dir, unique: nil, nulls: false, sql: nil) @name = attr_name @order_enum = vals_and_or_dir.shift if vals_and_or_dir[0].is_a?(Array) @direction = Direction.parse!( vals_and_or_dir.shift || (@order_enum ? :desc : :asc) ) unless vals_and_or_dir.empty? fail ArgumentError, "extra arguments: #{vals_and_or_dir.map(&:inspect) * ', '}" end @unique = unique.nil? ? (name.to_s == scope.primary_key) : unique if @order_enum&.include?(nil) fail ArgumentError, '`nulls` cannot be set if a value is null' if nulls @nullable = true @nulls = if @order_enum[0].nil? @direction == :desc ? :first : :last else @direction == :desc ? :last : :first end else @nullable = !!nulls # rubocop:disable Style/DoubleNegation @nulls = NullsDirection.parse!( nulls || NullsDirection.default(scope, @direction) ) end @custom_sql = sql @sql_builder = SQL::Column.new(scope, self) end |
Instance Attribute Details
#custom_sql ⇒ Object (readonly)
Returns the value of attribute custom_sql.
9 10 11 |
# File 'lib/order_query/column.rb', line 9 def custom_sql @custom_sql end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/order_query/column.rb', line 9 def name @name end |
#order_enum ⇒ Object (readonly)
Returns the value of attribute order_enum.
9 10 11 |
# File 'lib/order_query/column.rb', line 9 def order_enum @order_enum end |
Instance Method Details
#default_nulls_direction(reverse = false) ⇒ :first, :last
73 74 75 |
# File 'lib/order_query/column.rb', line 73 def default_nulls_direction(reverse = false) NullsDirection.default(scope, direction(reverse)) end |
#direction(reverse = false) ⇒ Object
rubocop:enable Metrics/ParameterLists,Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity rubocop:enable Metrics/MethodLength
63 64 65 |
# File 'lib/order_query/column.rb', line 63 def direction(reverse = false) reverse ? Direction.reverse(@direction) : @direction end |
#enum_side(value, side, strict = true) ⇒ Array
Returns valid order values before / after the given value.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/order_query/column.rb', line 91 def enum_side(value, side, strict = true) # rubocop:disable Metrics/AbcSize ord = order_enum pos = ord.index(value) if pos dir = direction if side == :after && dir == :desc || side == :before && dir == :asc ord.from pos + (strict ? 1 : 0) else ord.first pos + (strict ? 0 : 1) end else # default to all if current is not in sort order values [] end end |
#inspect ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/order_query/column.rb', line 107 def inspect parts = [ @name, (@order_enum.inspect if order_enum), ('unique' if @unique), (column_name if @custom_sql), @direction ].compact "(#{parts.join(' ')})" end |
#nullable? ⇒ Boolean
77 78 79 |
# File 'lib/order_query/column.rb', line 77 def nullable? @nullable end |
#nulls_direction(reverse = false) ⇒ :first, :last
68 69 70 |
# File 'lib/order_query/column.rb', line 68 def nulls_direction(reverse = false) reverse ? NullsDirection.reverse(@nulls) : @nulls end |
#unique? ⇒ Boolean
81 82 83 |
# File 'lib/order_query/column.rb', line 81 def unique? @unique end |