Class: Saxon::XDM::Map
- Inherits:
-
Object
- Object
- Saxon::XDM::Map
- Includes:
- Enumerable, ItemSequenceLike, SequenceLike
- Defined in:
- lib/saxon/xdm/map.rb
Overview
Represents an XDM Map
Class Method Summary collapse
-
.create(hash) ⇒ XDM::Map
Create an Map from a Ruby Hash, by ensuring each key has been converted to an AtomicValue, and each value has been converted to an XDM Value of some sort.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compare this Map against another.
-
#[](key) ⇒ Object
Fetch the value for the key given.
-
#each {|key, value| ... } ⇒ Object
Iterate over the Map as Hash#each would.
-
#fetch(key, *args, &block) ⇒ Object
Fetch the value for the key given, as Hash#fetch would.
-
#initialize(s9_xdm_map) ⇒ Map
constructor
private
A new instance of Map.
-
#merge(other) {|key, value| ... } ⇒ XDM::Map
Create a new Map from the result of merging another Map into this one.
-
#reject {|key, value| ... } ⇒ Object
Return a new Map containing only key, value pairs for which the block DOES NOT return true.
-
#select {|key, value| ... } ⇒ Object
Return a new Map containing only key, value pairs for which the block returns true.
-
#to_h ⇒ Hash
a (frozen) Ruby hash containing the keys and values from the Map.
-
#to_java ⇒ S9API::XdmMap
The underlying Saxon XdmMap.
Methods included from ItemSequenceLike
#sequence_enum, #sequence_size
Methods included from SequenceLike
#append, #sequence_enum, #sequence_size
Constructor Details
#initialize(s9_xdm_map) ⇒ Map
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 Map.
35 36 37 |
# File 'lib/saxon/xdm/map.rb', line 35 def initialize(s9_xdm_map) @s9_xdm_map = s9_xdm_map end |
Class Method Details
.create(hash) ⇒ XDM::Map
Create an Saxon::XDM::Map from a Ruby Hash, by ensuring each key has been converted to an AtomicValue, and each value has been converted to an XDM Value of some sort.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/saxon/xdm/map.rb', line 14 def self.create(hash) case hash when Saxon::S9API::XdmMap new(hash) else new(S9API::XdmMap.new(Hash[ hash.map { |key, value| [XDM.AtomicValue(key).to_java, XDM.Value(value).to_java] } ])) end end |
Instance Method Details
#==(other) ⇒ Object
Compare this Map against another. They’re equal if they contain the same key, value pairs.
41 42 43 44 |
# File 'lib/saxon/xdm/map.rb', line 41 def ==(other) return false unless other.is_a?(self.class) to_h == other.to_h end |
#[](key) ⇒ Object
Fetch the value for the key given. key
is converted to an AtomicValue if it isn’t already one.
49 50 51 |
# File 'lib/saxon/xdm/map.rb', line 49 def [](key) cached_hash[XDM.AtomicValue(key)] end |
#each {|key, value| ... } ⇒ Object
Iterate over the Map as Hash#each would
64 65 66 |
# File 'lib/saxon/xdm/map.rb', line 64 def each(&block) cached_hash.each(&block) end |
#fetch(key, *args, &block) ⇒ Object
Fetch the value for the key given, as Hash#fetch would. key
is converted to an AtomicValue if it isn’t already one.
57 58 59 |
# File 'lib/saxon/xdm/map.rb', line 57 def fetch(key, *args, &block) cached_hash.fetch(XDM.AtomicValue(key), *args, &block) end |
#merge(other) {|key, value| ... } ⇒ XDM::Map
Create a new Map from the result of merging another Map into this one. In the case of duplicate keys, the value in the provided hash will be used.
93 94 95 |
# File 'lib/saxon/xdm/map.rb', line 93 def merge(other) self.class.create(to_h.merge(other.to_h)) end |
#reject {|key, value| ... } ⇒ Object
Return a new Map containing only key, value pairs for which the block DOES NOT return true.
82 83 84 |
# File 'lib/saxon/xdm/map.rb', line 82 def reject(&block) self.class.create(each.reject(&block).to_h) end |
#select {|key, value| ... } ⇒ Object
Return a new Map containing only key, value pairs for which the block returns true.
73 74 75 |
# File 'lib/saxon/xdm/map.rb', line 73 def select(&block) self.class.create(each.select(&block).to_h) end |
#to_h ⇒ Hash
a (frozen) Ruby hash containing the keys and values from the Map.
104 105 106 |
# File 'lib/saxon/xdm/map.rb', line 104 def to_h cached_hash end |
#to_java ⇒ S9API::XdmMap
Returns the underlying Saxon XdmMap.
98 99 100 |
# File 'lib/saxon/xdm/map.rb', line 98 def to_java @s9_xdm_map end |