On Tuesday, May 3, 2016 at 8:06:23 PM UTC-7, Chris Riddoch wrote:
>
> Hi, folks.
>
> I've been working with postgresql's tstzrange types with sequel, and have 
> found the convert_infinite_timestamps option very helpful in making it 
> possible to handle the ranges where 'infinity' or '-infinity' is a value at 
> one of the ends of a tstzrange.  The PGRange seems meant to work with 
> ranges where the native ruby Range class just doesn't have the features 
> necessary.
>

Correct.  PostgreSQL supports more range types than ruby, so you can't use 
ruby's range types to represent all PostgreSQL range types, hence the 
reason for PGRange.
 

>
> But the current implementation of cover? for the PGRange class, line 407 
> to 411 [0] delegates to the native ruby Range class. When I call cover? (or 
> any of the other methods delegated to Range) on a PGRange that uses one of 
> the infinity values, I get an error like the following:
>
> ArgumentError: bad value for range
> from .../gems/sequel-4.34.0/lib/sequel/extensions/pg_range.rb:500:in 
> `initialize'
>
> I started writing tests for implementations of these methods for the 
> PGRange class to at least not delegate in the cases where 
> convert_infinite_timestamps is set to :float (which is what I'm using) or 
> would otherwise result in an ArgumentError...
>
> Working through this, I now think the logic of valid_ruby_range? is 
> incomplete.  But not all cases of infinity will problems, so I can't just 
> test for that:
>
>   ((1.0)..Float::INFINITY).cover?(10) => true
>
> Here's a curiosity I just found by way of a stack overflow post[1]: 
> DateTime::Infinity.new
>
> (DateTime.now()..DateTime::Infinity.new).cover?(DateTime.new(2017,1,1,0,0,0)) 
> => true
>
> It took a little trying to get a range that goes the other direction, but 
> this seems to work:
>
> (Date.new(2000,1,1,Date::GREGORIAN)..DateTime.now()).cover?(DateTime.new(2012,1,1,0,0,0))
>  
> => true
>
> So maybe one option is to make use of these when converting DateTime 
> ranges, instead of of the current convert_implement_timestamps code?  It 
> sort of seems like every option is a bit of a hack.  I've already started 
> on some tests, which I've put into my fork on github, in a branch 
> pg-tstzrange-cover. [2]  Suggestions?  
>

I suppose I would be OK with having PGRange#cover? not delegate to 
to_range. Maybe something like the following:

class Sequel::Postgres::PGRange
  def cover?(value)
    return false if empty?
    b = self.begin
    return false if b && b.send(exclude_begin? ? :>= : :>, value)
    e = self.end
    return false if e && e.send(exclude_end? ? :<= : :<, value)
    true
  end
end

Can you try that out and see if it works for you?  If so, add some specs 
and submit a pull request for it?

Thanks,
Jeremy

>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to