Sha256: 64468dca6f35adaee00683d846305bb1523115d48078db5893cf3eff5762f2f3

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

module EDTF
  
  class Interval

    extend Forwardable

    include Enumerable
    
    def_delegators :to_range, *(Range.instance_methods - Enumerable.instance_methods - Object.instance_methods)

    attr_reader :from, :to
    
    def initialize(from = :open, to = :open)
      @from, @to = from, to
    end
    
    def from=(from)
      @from = from || :open
    end

    def to=(to)
      @to = to || :open
    end

    [:open, :unknown].each do |method_name|
      
      define_method("#{method_name}?") do
        @to == method_name || @from == method_name
      end

      define_method("#{method_name}!") do
        @to = method_name
      end

      alias_method("#{method_name}_end!", "#{method_name}!")
      
      define_method("#{method_name}_end?") do
        @to == method_name
      end
      
    end

    def unknown_start?
      @from == :unknown
    end
    
    def unknown_start!
      @from = :unknown
    end
    
    # TODO how to handle +/- Infinity for Dates?
    
    def to_range
      case
      when open?
        nil
      when unknown_end?
        nil
      else
        Range.new(unknown_start? ? Date.new : from, to)
      end
    end
    
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
edtf-0.0.3 lib/edtf/interval.rb
edtf-0.0.2 lib/edtf/interval.rb
edtf-0.0.1 lib/edtf/interval.rb