module JsonbAccessor::QueryBuilder

def self.convert_keys_to_store_keys(attributes, store_key_mapping)

def self.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

def self.validate_column_name!(query, column_name)

def self.validate_column_name!(query, column_name)
  if query.model.columns.none? { |column| column.name == column_name.to_s }
    raise InvalidColumnName, "a column named `#{column_name}` does not exist on the `#{query.model.table_name}` table"
  end
end

def self.validate_direction!(option)

def self.validate_direction!(option)
  if ORDER_DIRECTIONS.exclude?(option)
    raise InvalidDirection, "`#{option}` is not a valid direction for ordering, only `asc` and `desc` are accepted"
  end
end

def self.validate_field_name!(query, column_name, field_name)

def self.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