Class: Saxon::XDM::AtomicValue

Inherits:
Object
  • Object
show all
Includes:
ItemSequenceLike, SequenceLike
Defined in:
lib/saxon/xdm/atomic_value.rb

Overview

An XPath Data Model Node object, representing an XML document, or an element or one of the other node chunks in the XDM.

Defined Under Namespace

Classes: CannotCreateQNameFromString, NotationCannotBeDirectlyCreated

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ItemSequenceLike

#sequence_enum, #sequence_size

Methods included from SequenceLike

#append, #sequence_enum, #sequence_size

Constructor Details

#initialize(s9_xdm_atomic_value) ⇒ AtomicValue

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of AtomicValue.



126
127
128
# File 'lib/saxon/xdm/atomic_value.rb', line 126

def initialize(s9_xdm_atomic_value)
  @s9_xdm_atomic_value = s9_xdm_atomic_value
end

Class Method Details

.create(value, item_type = nil) ⇒ Saxon::XDM::AtomicValue

Convert a single Ruby value into an XDM::AtomicValue

If no explicit ItemType is passed, the correct type is guessed based on the class of the value. (e.g. xs:date for Date.)

Values are converted based on Ruby idioms and operations, so an explicit ItemType of xs:boolean will use truthyness to evaluate the value. This means that the Ruby string 'false' will produce an xs:boolean whose value is true, because all strings are truthy. If you need to pass lexical strings unchanged, use from_lexical_string.

Parameters:

  • value (Object)

    the value to convert

  • item_type (Saxon::ItemType, String) (defaults to: nil)

    the value’s type, as either an ItemType or a name (xs:date)

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/saxon/xdm/atomic_value.rb', line 68

def create(value, item_type = nil)
  case value
  when Saxon::S9API::XdmAtomicValue
    new(value)
  when XDM::AtomicValue
    value
  else
    return create_implying_item_type(value) if item_type.nil?

    item_type = ItemType.get_type(item_type)

    return new(Saxon::S9API::XdmAtomicValue.new(value.to_java)) if item_type == xs_qname && value_is_qname?(value)
    raise NotationCannotBeDirectlyCreated if item_type == xs_notation

    value_lexical_string = item_type.lexical_string(value)
    new(new_s9_xdm_atomic_value(value_lexical_string, item_type))
  end
end

.from_lexical_string(value, item_type) ⇒ Saxon::XDM::AtomicValue

convert a lexical string representation of an XDM Atomic Value into an XDM::AtomicValue.

Note that this skips all conversion and checking of the string before handing it off to Saxon.

Parameters:

  • value (String)

    the lexical string representation of the value

  • item_type (Saxon::ItemType, String)

    the value’s type, as either an ItemType or a name (xs:date)

Returns:

Raises:



97
98
99
100
101
# File 'lib/saxon/xdm/atomic_value.rb', line 97

def from_lexical_string(value, item_type)
  item_type = ItemType.get_type(item_type)
  raise CannotCreateQNameFromString if item_type == xs_qname
  new(new_s9_xdm_atomic_value(value.to_s, item_type))
end

.xs_notationObject

ItemType representing NOTATION



46
47
48
# File 'lib/saxon/xdm/atomic_value.rb', line 46

def xs_notation
  @xs_notation ||= ItemType.get_type('xs:NOTATION')
end

.xs_qnameObject

ItemType representing QNames



41
42
43
# File 'lib/saxon/xdm/atomic_value.rb', line 41

def xs_qname
  @xs_qname ||= ItemType.get_type('xs:QName')
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

compares two Saxon::XDM::AtomicValues using the underlying Saxon and XDM comparision rules

Parameters:

Returns:

  • (Boolean)


170
171
172
173
# File 'lib/saxon/xdm/atomic_value.rb', line 170

def ==(other)
  return false unless other.is_a?(XDM::AtomicValue)
  s9_xdm_atomic_value.equals(other.to_java)
end

#hashObject

Compute a hash-code for this Saxon::XDM::AtomicValue.

Two Saxon::XDM::AtomicValues with the same content will have the same hash code (and will compare using eql?).

See Also:

  • Object#hash


181
182
183
# File 'lib/saxon/xdm/atomic_value.rb', line 181

def hash
  @hash ||= s9_xdm_atomic_value.hashCode
end

#item_typeSaxon::ItemType

Returns The ItemType of the value.

Returns:



152
153
154
# File 'lib/saxon/xdm/atomic_value.rb', line 152

def item_type
  @item_type ||= Saxon::ItemType.get_type(Saxon::QName.resolve(s9_xdm_atomic_value.getTypeName))
end

#to_javaSaxon::S9API::XdmAtomicValue

atomic value object.

Returns:

  • (Saxon::S9API::XdmAtomicValue)

    The underlying Saxon Java XDM



139
140
141
# File 'lib/saxon/xdm/atomic_value.rb', line 139

def to_java
  s9_xdm_atomic_value
end

#to_rubyObject

Return the value as an instance of the Ruby class that best represents the type of the value. Types with no sensible equivalent are returned as their lexical string form

Returns:

  • (Object)

    A Ruby object representation of the value



161
162
163
# File 'lib/saxon/xdm/atomic_value.rb', line 161

def to_ruby
  @ruby_value ||= item_type.ruby_value(self).freeze
end

#to_sString

Return the value as a String. Like calling XPath’s string() function.

Returns:

  • (String)

    The string representation of the value



147
148
149
# File 'lib/saxon/xdm/atomic_value.rb', line 147

def to_s
  s9_xdm_atomic_value.toString
end

#type_nameSaxon::QName

Return a QName representing the type of the value

Returns:



133
134
135
# File 'lib/saxon/xdm/atomic_value.rb', line 133

def type_name
  @type_name ||= Saxon::QName.new(s9_xdm_atomic_value.getTypeName)
end