Class: Rubyang::Database::SchemaTree::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyang/database/schema_tree.rb

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Range

Returns a new instance of Range.



250
251
252
253
# File 'lib/rubyang/database/schema_tree.rb', line 250

def initialize arg
  @range = [[-Float::INFINITY, Float::INFINITY]]
  self.update arg
end

Instance Method Details

#to_sObject



289
290
291
# File 'lib/rubyang/database/schema_tree.rb', line 289

def to_s
  @range.map{ |l| "#{l[0]}..#{l[1]}" }.join( "|" )
end

#update(arg) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/rubyang/database/schema_tree.rb', line 261

def update arg
  new_range = Array.new
  arg.delete( ' ' ).split( '|' ).each{ |range_part|
    case range_part
    when /[^\.]+\.\.[^\.]+/
      min, max = range_part.split('..')
    else
      min = max = range_part
    end
    case min
    when /^min$/
      min = @range.map{ |r| r[0] }.min
    else
      min = min.to_i
    end
    case max
    when /^max$/
      max = @range.map{ |r| r[1] }.max
    else
      max = max.to_i
    end
    unless @range.find{ |min2, max2| (min2..max2).include?( min ) && (min2..max2).include?( max ) }
      raise ArgumentError, "#{range_part} is not valid"
    end
    new_range.push [min, max]
  }
  @range = new_range
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


254
255
256
257
258
259
260
# File 'lib/rubyang/database/schema_tree.rb', line 254

def valid? value
  if @range.find{ |min2, max2| (min2..max2).include?( Integer(value) ) }
    true
  else
    false
  end
end