support for multi range (multi_period?)
Opened this issue · 2 comments
I have implemented very rudimentary suport for multi range type in my app. Some example snippets below. I start to use these types of ranges more and more, as they proved very useful.
It would be great if torque-postgresql would have support for this. I can prepare a PR, but would need some guidance with how to implement this in the current gem's architecture.
# SQL
range_agg(tstzrange(starttime, endtime, '[]')) AS multi_period
# MultiPeriod.value(self[:multi_period]) => converts column to Array of Range objects
class MultiPeriod
extend Dry::Initializer
param :multi_period, type: Types::Array.of(Types.Instance(Range)) | Types::Coercible::String
def self.value(...)
new(...).value
end
def value
return multi_period if multi_period.is_a?(Array)
json_value.map do |from, to|
(Time.zone.parse(from).to_datetime..Time.zone.parse(to).to_datetime)
end.sort_by(&:first)
end
private
def json_value
JSON.parse("[#{multi_period[1..-2]}]")
end
end
scope :order_by_lower, -> (dir = :asc) { order(Arel.sql("lower(multi_period) #{dir}")) }
scope :order_by_upper, -> (dir = :asc) { order(Arel.sql("upper(multi_period) #{dir}")) }
scope :multi_period_overlapping, -> (from, to) { where("multi_period && ?", Arel.sql("{[#{from},#{to}]}")) }
Hey @tomasc, sorry for taking so long to reply. I've been quite engaged in my current project!
I'll be checking this tomorrow. My first instinct is that AR already handles arrays of types as a modifier. That said, what might need to be provided is a way to alias or give the proper name for them (which I think it's possible with a few lines).
Thank you @crashtech , that would be lovely!