Skip to content

support for multi range (multi_period?) #82

Description

@tomasc

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}]}")) }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions