module JsonbAccessor::QueryHelper

Constants

GREATER_THAN

Constants

GREATER_THAN_OR_EQUAL_TO
InvalidColumnName

Errors

InvalidDirection
InvalidFieldName
LESS_THAN
LESS_THAN_OR_EQUAL_TO
NUMBER_OPERATORS
NUMBER_OPERATORS_MAP
NotSupported
ORDER_DIRECTIONS
TIME_OPERATORS
TIME_OPERATORS_MAP

Public Class Methods

convert_keys_to_store_keys(attributes, store_key_mapping) click to toggle source
# File lib/jsonb_accessor/query_helper.rb, line 60
def convert_keys_to_store_keys(attributes, store_key_mapping)
  attributes.each_with_object({}) do |(name, value), new_attributes|
    store_key = store_key_mapping[name.to_s]
    new_attributes[store_key] = value
  end
end
convert_number_ranges(attributes) click to toggle source
# File lib/jsonb_accessor/query_helper.rb, line 75
def convert_number_ranges(attributes)
  attributes.each_with_object({}) do |(name, value), new_attributes|
    is_range = value.is_a?(Range)

    new_attributes[name] = if is_range && value.first.is_a?(Numeric) && value.exclude_end?
                             { greater_than_or_equal_to: value.first, less_than: value.end }
                           elsif is_range && value.first.is_a?(Numeric)
                             { greater_than_or_equal_to: value.first, less_than_or_equal_to: value.end }
                           else
                             value
                           end
  end
end
convert_ranges(attributes) click to toggle source
# File lib/jsonb_accessor/query_helper.rb, line 103
def convert_ranges(attributes)
  %i[convert_number_ranges convert_time_ranges].reduce(attributes) do |new_attributes, converter_method|
    public_send(converter_method, new_attributes)
  end
end
convert_time_ranges(attributes) click to toggle source
# File lib/jsonb_accessor/query_helper.rb, line 89
def convert_time_ranges(attributes)
  attributes.each_with_object({}) do |(name, value), new_attributes|
    is_range = value.is_a?(Range)

    if is_range && (value.first.is_a?(Time) || value.first.is_a?(Date))
      start_time = value.first
      end_time = value.end
      new_attributes[name] = { before: end_time, after: start_time }
    else
      new_attributes[name] = value
    end
  end
end
number_query_arguments?(arg) click to toggle source
# File lib/jsonb_accessor/query_helper.rb, line 67
def number_query_arguments?(arg)
  arg.is_a?(Hash) && arg.keys.map(&:to_s).all? { |key| NUMBER_OPERATORS.include?(key) }
end
time_query_arguments?(arg) click to toggle source
# File lib/jsonb_accessor/query_helper.rb, line 71
def time_query_arguments?(arg)
  arg.is_a?(Hash) && arg.keys.map(&:to_s).all? { |key| TIME_OPERATORS.include?(key) }
end
validate_column_name!(query, column_name) click to toggle source
# File lib/jsonb_accessor/query_helper.rb, line 44
def validate_column_name!(query, column_name)
  raise InvalidColumnName, "a column named `#{column_name}` does not exist on the `#{query.model.table_name}` table" if query.model.columns.none? { |column| column.name == column_name.to_s }
end
validate_direction!(option) click to toggle source
# File lib/jsonb_accessor/query_helper.rb, line 56
def validate_direction!(option)
  raise InvalidDirection, "`#{option}` is not a valid direction for ordering, only `asc` and `desc` are accepted" if ORDER_DIRECTIONS.exclude?(option)
end
validate_field_name!(query, column_name, field_name) click to toggle source
# File lib/jsonb_accessor/query_helper.rb, line 48
def validate_field_name!(query, column_name, field_name)
  store_keys = query.model.public_send("jsonb_store_key_mapping_for_#{column_name}").values
  if store_keys.exclude?(field_name.to_s)
    valid_field_names = store_keys.map { |key| "`#{key}`" }.join(", ")
    raise InvalidFieldName, "`#{field_name}` is not a valid field name, valid field names include: #{valid_field_names}"
  end
end