Module: Moxml::XPath::Conversion

Defined in:
lib/moxml/xpath/conversion.rb

Overview

Module for converting XPath objects such as NodeSets to different types.

Class Method Summary collapse

Class Method Details

.boolean?(value) ⇒ Boolean

Checks if a value is a boolean.

Parameters:

  • value (Object)

Returns:

  • (Boolean)


111
112
113
# File 'lib/moxml/xpath/conversion.rb', line 111

def self.boolean?(value)
  [true, false].include?(value)
end

.first_node_text(set) ⇒ String

Gets the text of the first node in a NodeSet.

Parameters:

Returns:

  • (String)


119
120
121
# File 'lib/moxml/xpath/conversion.rb', line 119

def self.first_node_text(set)
  set[0].respond_to?(:text) ? set[0].text : ""
end

.to_boolean(value) ⇒ Boolean

Converts a value to an XPath boolean.

Parameters:

  • value (Object)

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/moxml/xpath/conversion.rb', line 91

def self.to_boolean(value)
  bool = false

  if value.is_a?(Float)
    bool = !value.nan? && !value.zero?
  elsif value.is_a?(Integer)
    bool = !value.zero?
  elsif value.respond_to?(:empty?)
    bool = !value.empty?
  elsif value
    bool = true
  end

  bool
end

.to_compatible_types(left, right) ⇒ Array<Object, Object>

Converts both arguments to a type that can be compared using ==.

Parameters:

  • left (Object)
  • right (Object)

Returns:

  • (Array<Object, Object>)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/moxml/xpath/conversion.rb', line 14

def self.to_compatible_types(left, right)
  if left.is_a?(Moxml::NodeSet) || left.respond_to?(:text)
    left = to_string(left)
  end

  if right.is_a?(Moxml::NodeSet) || right.respond_to?(:text)
    right = to_string(right)
  end

  if left.is_a?(Numeric) && !right.is_a?(Numeric)
    right = to_float(right)
  end

  if left.is_a?(String) && !right.is_a?(String)
    right = to_string(right)
  end

  if boolean?(left) && !boolean?(right)
    right = to_boolean(right)
  end

  [left, right]
end

.to_float(value) ⇒ Float

Converts a value to an XPath number (float).

Parameters:

  • value (Object)

Returns:

  • (Float)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/moxml/xpath/conversion.rb', line 65

def self.to_float(value)
  if value.is_a?(Moxml::NodeSet)
    value = first_node_text(value)
  end

  if value.respond_to?(:text)
    value = value.text
  end

  if value == true
    1.0
  elsif value == false
    0.0
  else
    begin
      Float(value)
    rescue ArgumentError, TypeError
      Float::NAN
    end
  end
end

.to_string(value) ⇒ String

Converts a value to an XPath string.

Parameters:

  • value (Object)

Returns:

  • (String)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/moxml/xpath/conversion.rb', line 42

def self.to_string(value)
  # If we have a number that has a zero decimal (e.g. 10.0) we want to
  # get rid of that decimal. For this we'll first convert the number to
  # an integer.
  if value.is_a?(Float) && value.modulo(1).zero?
    value = value.to_i
  end

  if value.is_a?(Moxml::NodeSet)
    value = first_node_text(value)
  end

  if value.respond_to?(:text)
    value = value.text
  end

  value.to_s
end