Class: Saxon::ItemType::LexicalStringConversion::IntegerConversion

Inherits:
Object
  • Object
show all
Defined in:
lib/saxon/item_type/lexical_string_conversion.rb

Overview

Helper class for performing conversion and validation to XDM integer types from Ruby’s Fixnum/Bignum/Integer classes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min, max) ⇒ IntegerConversion

Returns a new instance of IntegerConversion.



31
32
33
# File 'lib/saxon/item_type/lexical_string_conversion.rb', line 31

def initialize(min, max)
  @min, @max = min, max
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



29
30
31
# File 'lib/saxon/item_type/lexical_string_conversion.rb', line 29

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



29
30
31
# File 'lib/saxon/item_type/lexical_string_conversion.rb', line 29

def min
  @min
end

Instance Method Details

#call(value, item_type) ⇒ String

Check a value against our type constraints, and return the lexical string representation if it’s okay.

Parameters:

  • value (Integer)

    the ruby value

  • item_type (Saxon::ItemType)

    the item type

Returns:

  • (String)

    the lexical string representation of the value

Raises:



69
70
71
72
73
74
75
76
77
78
# File 'lib/saxon/item_type/lexical_string_conversion.rb', line 69

def call(value, item_type)
  integer_value = case value
  when ::Numeric
    value.to_i
  else
    Integer(LexicalStringConversion.validate(value, item_type, Patterns::INTEGER), 10)
  end
  raise Errors::RubyValueOutOfBounds.new(value, item_type) unless in_bounds?(integer_value)
  integer_value.to_s
end

#gte_min?(integer_value) ⇒ Boolean

Returns whether the Ruby integer is >= the lower bound of the range allowed for the XDM type

Parameters:

  • integer_value (Integer)

    the ruby integer to check

Returns:

  • (Boolean)

    whether the value is okay



47
48
49
50
# File 'lib/saxon/item_type/lexical_string_conversion.rb', line 47

def gte_min?(integer_value)
  return true if min.nil?
  integer_value >= min
end

#in_bounds?(integer_value) ⇒ Boolean

Returns whether the Ruby integer is within the range allowed for the XDM type

Parameters:

  • integer_value (Integer)

    the ruby integer to check

Returns:

  • (Boolean)

    whether the value is within bounds



39
40
41
# File 'lib/saxon/item_type/lexical_string_conversion.rb', line 39

def in_bounds?(integer_value)
  gte_min?(integer_value) && lte_max?(integer_value)
end

#lte_max?(integer_value) ⇒ Boolean

Returns whether the Ruby integer is <= the upper bound of the range allowed for the XDM type

Parameters:

  • integer_value (Integer)

    the ruby integer to check

Returns:

  • (Boolean)

    whether the value is okay



56
57
58
59
# File 'lib/saxon/item_type/lexical_string_conversion.rb', line 56

def lte_max?(integer_value)
  return true if max.nil?
  integer_value <= max
end