Websites/app/logical/parse_value.rb

50 lines
1.0 KiB
Ruby
Raw Permalink Normal View History

2024-05-03 03:04:43 +00:00
# frozen_string_literal: true
module ParseValue
extend self
def range(range, type = :integer)
if range.start_with?("<=")
[:lte, cast(range.delete_prefix("<="), type)]
elsif range.start_with?("..")
[:lte, cast(range.delete_prefix(".."), type)]
elsif range.start_with?("<")
[:lt, cast(range.delete_prefix("<"), type)]
elsif range.start_with?(">=")
[:gte, cast(range.delete_prefix(">="), type)]
elsif range.end_with?("..")
[:gte, cast(range.delete_suffix(".."), type)]
elsif range.start_with?(">")
[:gt, cast(range.delete_prefix(">"), type)]
elsif range.include?("..")
left, right = range.split("..", 2)
[:between, cast(left, type), cast(right, type)]
elsif range.include?(",")
[:in, range.split(",")[0..99].map { |x| cast(x, type) }]
else
[:eq, cast(range, type)]
end
end
private
def cast(object, type)
case type
when :integer
object.to_i
when :float
object.to_f
end
end
end