Class: Mexico::FileSystem::FiestaMap
- Inherits:
-
Object
- Object
- Mexico::FileSystem::FiestaMap
- Includes:
- ROXML
- Defined in:
- lib/mexico/file_system/fiesta_map.rb
Overview
This class models a data container for map (or attribute value) structures.
Instance Attribute Summary collapse
-
#map_items ⇒ Object
readonly
Returns the value of attribute map_items.
Class Method Summary collapse
-
.from_xml(node) ⇒ FiestaMap
Auxiliary method that reads a
FiestaMap
object from the standard XML representation of FiESTA.
Instance Method Summary collapse
-
#[](k) ⇒ Object?
Retrieves the value for the given key
k
from the map. -
#[]=(k, v) ⇒ void
Adds or modifies an entry with the key
k
in the map. -
#empty? ⇒ Boolean
Returns
true
iff this map is empty. -
#has_key?(k) ⇒ Boolean
Returns
true
iff this map contains an entry with the given key. -
#initialize(initial_vals = {}) ⇒ FiestaMap
constructor
Creates a new map instance.
-
#size ⇒ Integer
Returns the number of entries in this map.
-
#to_s ⇒ String
Creates a human-readable string representation of this map.
-
#to_xml(x) ⇒ XML::Node
Converts this map object to its standard XML representation.
Constructor Details
#initialize(initial_vals = {}) ⇒ FiestaMap
Creates a new map instance. If initial_vals is defined and contains values, those will be used to populate the map.
30 31 32 33 |
# File 'lib/mexico/file_system/fiesta_map.rb', line 30 def initialize(initial_vals={}) @map = Hash.new @map.merge!(initial_vals) end |
Instance Attribute Details
#map_items ⇒ Object (readonly)
Returns the value of attribute map_items.
25 26 27 |
# File 'lib/mexico/file_system/fiesta_map.rb', line 25 def map_items @map_items end |
Class Method Details
.from_xml(node) ⇒ FiestaMap
Auxiliary method that reads a FiestaMap
object from the standard XML representation of FiESTA.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mexico/file_system/fiesta_map.rb', line 74 def self.from_xml(node) #puts "SOMEONE CALLED FiestaMap.from_xml" #puts node.name # @map = Hash.new map = self.new node.xpath('./MapItem').each do |mi| #puts mi.name #puts mi['key'] #puts mi.text map[mi['key']]=mi.text end #puts map.size map end |
Instance Method Details
#[](k) ⇒ Object?
Retrieves the value for the given key k
from the map.
47 48 49 |
# File 'lib/mexico/file_system/fiesta_map.rb', line 47 def [](k) @map[k] end |
#[]=(k, v) ⇒ void
This method returns an undefined value.
Adds or modifies an entry with the key k
in the map.
39 40 41 |
# File 'lib/mexico/file_system/fiesta_map.rb', line 39 def []=(k,v) @map[k]=v end |
#empty? ⇒ Boolean
Returns true
iff this map is empty.
59 60 61 |
# File 'lib/mexico/file_system/fiesta_map.rb', line 59 def empty? @map.empty? end |
#has_key?(k) ⇒ Boolean
Returns true
iff this map contains an entry with the given key.
66 67 68 |
# File 'lib/mexico/file_system/fiesta_map.rb', line 66 def has_key?(k) @map.has_key?(k) end |
#size ⇒ Integer
Returns the number of entries in this map.
53 54 55 |
# File 'lib/mexico/file_system/fiesta_map.rb', line 53 def size @map.size end |
#to_s ⇒ String
Creates a human-readable string representation of this map.
93 94 95 |
# File 'lib/mexico/file_system/fiesta_map.rb', line 93 def to_s @map.to_a.collect{|k,v| "#{k} => #{v}"}.join(", ") end |
#to_xml(x) ⇒ XML::Node
what does the variable x
do?
Converts this map object to its standard XML representation
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mexico/file_system/fiesta_map.rb', line 100 def to_xml(x) n = XML::new_node("Map") @map.each_pair do |k,v| i_node = XML::new_node("MapItem") i_node['key'] = k i_node.content = v n.add_child(i_node) end n end |