Sha256: d34076b71113412b53f9567855f0e9b637a842940793285c5a670edcc1203f08

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

module Rley # This module is used as a namespace
  module PTree # This module is used as a namespace
    class TokenRange
      # The index of the lower bound of token range
      attr_reader(:low)

      # The index of the upper bound of token range
      attr_reader(:high)

      # @param aRangeRep [Hash]
      def initialize(aRangeRep)
        assign_low(aRangeRep)
        assign_high(aRangeRep)
      end


      def ==(other)
        return true if object_id == other.object_id

        case other
          when Hash
            result = low == other[:low] && high == other[:high]
          when TokenRange
            result = low == other.low && high == other.high
          when Array
            result = low == other[0] && high == other[1]
        end

        return result
      end

      # true when both bounds aren't nil.
      def bounded?()
        return !(low.nil? || high.nil?)
      end

      # Conditional assign
      def assign(aRange)
        return if bounded?

        assign_low(aRange) if low.nil?
        assign_high(aRange) if high.nil?
      end

      private

      def assign_low(aRange)
        case aRange
          when Hash then @low = aRange.fetch(:low, nil)
          when TokenRange then @low = aRange.low
        end
      end

      def assign_high(aRange)
        case aRange
          when Hash then @high = aRange.fetch(:high, nil)
          when TokenRange then @high = aRange.high
        end
      end
    end # class
  end # module
end # module
# End of file

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rley-0.2.01 lib/rley/ptree/token_range.rb
rley-0.2.00 lib/rley/ptree/token_range.rb