class Mhc::PropertyValue::Range

Constants

ITEM_SEPARATOR

Attributes

first[R]
last[R]

Public Class Methods

new(item_class, prefix = nil, first = nil, last = nil) click to toggle source
# File lib/mhc/property_value/range.rb, line 9
def initialize(item_class, prefix = nil, first = nil, last = nil)
  @item_class, @prefix = item_class, prefix
  @first, @last = first, last
end

Public Instance Methods

<=>(o) click to toggle source
# File lib/mhc/property_value/range.rb, line 56
def <=>(o)
  o = o.first if o.respond_to?(:first)
  safe_comp(self.first, o)
end
blank?() click to toggle source
# File lib/mhc/property_value/range.rb, line 65
def blank?
  @first.nil? && @last.nil?
end
each() { |i| ... } click to toggle source
# File lib/mhc/property_value/range.rb, line 42
def each
  i = first
  while i <= last
    yield(i)
    i = i.succ
  end
end
infinit?() click to toggle source
# File lib/mhc/property_value/range.rb, line 61
def infinit?
  return @first.nil? || @last.nil?
end
narrow(from, to) click to toggle source
# File lib/mhc/property_value/range.rb, line 50
def narrow(from, to)
  from = @first if from.nil? or (@first and from < @first)
  to   = @last  if to.nil?   or (@last  and to   > @last)
  self.class.new(@item_class, @prefix, from, to)
end
parse(string) click to toggle source

our Range acceps these 3 forms:

(1) A-B    : first, last = A, B
(2) A      : first, last = A, A
(3) A-     : first, last = A, nil
(4) -B     : first, last = nil, B

nil means range is open (infinite).

# File lib/mhc/property_value/range.rb, line 22
def parse(string)
  @first, @last = nil, nil
  first, last = string.split(ITEM_SEPARATOR, 2)
  last = first if last.nil? # single "A" means "A-A"

  @first = @item_class.parse(first) unless first.to_s == ""
  @last  = @item_class.parse(last, @first)  unless last.to_s  == ""
  return self.class.new(@item_class, @prefix, @first, @last)
end
to_a() click to toggle source
# File lib/mhc/property_value/range.rb, line 32
def to_a
  array = []
  i = first
  while i <= last
    array << i
    i = i.succ
  end
  return array
end
to_mhc_string() click to toggle source
# File lib/mhc/property_value/range.rb, line 69
def to_mhc_string
  first = @first.nil? ? "" : @first.to_mhc_string
  last  = @last.nil?  ? "" : @last.to_mhc_string

  if first == last
    return @prefix.to_s + first
  else
    return @prefix.to_s + [first, last].join(ITEM_SEPARATOR)
  end
end
Also aliased as: to_s
to_s()
Alias for: to_mhc_string

Private Instance Methods

cover?(item) click to toggle source
# File lib/mhc/property_value/range.rb, line 84
def cover?(item)
  return false if @first && item < @first
  return false if @last  && item > @last
  return true
end
safe_comp(a, o) click to toggle source
# File lib/mhc/property_value/range.rb, line 90
def safe_comp(a, o)
  # nil is minimum
  return (a <=> o) if  a and  o
  return -1        if !a and  o
  return  1        if  a and !o
  return  0        if !a and !o
end