Module: JsonbAccessor::QueryHelper
- Defined in:
- lib/jsonb_accessor/query_helper.rb
Defined Under Namespace
Classes: InvalidColumnName, InvalidDirection, InvalidFieldName, NotSupported
Constant Summary
collapse
- GREATER_THAN =
">"
- GREATER_THAN_OR_EQUAL_TO =
">="
- LESS_THAN =
"<"
- LESS_THAN_OR_EQUAL_TO =
"<="
- NUMBER_OPERATORS_MAP =
{
GREATER_THAN => GREATER_THAN,
"greater_than" => GREATER_THAN,
"gt" => GREATER_THAN,
GREATER_THAN_OR_EQUAL_TO => GREATER_THAN_OR_EQUAL_TO,
"greater_than_or_equal_to" => GREATER_THAN_OR_EQUAL_TO,
"gte" => GREATER_THAN_OR_EQUAL_TO,
LESS_THAN => LESS_THAN,
"less_than" => LESS_THAN,
"lt" => LESS_THAN,
LESS_THAN_OR_EQUAL_TO => LESS_THAN_OR_EQUAL_TO,
"less_than_or_equal_to" => LESS_THAN_OR_EQUAL_TO,
"lte" => LESS_THAN_OR_EQUAL_TO
}.freeze
- NUMBER_OPERATORS =
NUMBER_OPERATORS_MAP.keys.freeze
- TIME_OPERATORS_MAP =
{
"after" => GREATER_THAN,
"before" => LESS_THAN
}.freeze
- TIME_OPERATORS =
TIME_OPERATORS_MAP.keys.freeze
- ORDER_DIRECTIONS =
[:asc, :desc, "asc", "desc"].freeze
Class Method Summary
collapse
Class Method Details
.convert_number_ranges(attributes) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/jsonb_accessor/query_helper.rb', line 68
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) ⇒ Object
96
97
98
99
100
|
# File 'lib/jsonb_accessor/query_helper.rb', line 96
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) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/jsonb_accessor/query_helper.rb', line 82
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) ⇒ Boolean
60
61
62
|
# File 'lib/jsonb_accessor/query_helper.rb', line 60
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) ⇒ Boolean
64
65
66
|
# File 'lib/jsonb_accessor/query_helper.rb', line 64
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) ⇒ Object
44
45
46
|
# 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) ⇒ Object
56
57
58
|
# 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) ⇒ Object
48
49
50
51
52
53
54
|
# 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
|