Class: Mexico::FileSystem::FiestaMap

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • initial_vals (Hash) (defaults to: {})

    Initial values for the new map object.


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_itemsObject (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.

Parameters:

  • node (XML::Node)

    The XML node to read from

Returns:

  • (FiestaMap)

    The map object represented in that XML node.


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.

Parameters:

  • k (Object)

    The key to be used.

Returns:

  • (Object, nil)

    The corresponding value object, or nil if there was no entry for the given key.


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.

Parameters:

  • k (Object)

    The key to be used.

  • v (Object)

    The value to be used.


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.

Returns:

  • (Boolean)

    Returns true iff this map is empty, false otherwise.


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.

Parameters:

  • k (Object)

    The key to be found.

Returns:

  • (Boolean)

    Returns true iff the given key is used in this map, false otherwise.


66
67
68
# File 'lib/mexico/file_system/fiesta_map.rb', line 66

def has_key?(k)
  @map.has_key?(k)
end

#sizeInteger

Returns the number of entries in this map.

Returns:

  • (Integer)

    The number of entries.


53
54
55
# File 'lib/mexico/file_system/fiesta_map.rb', line 53

def size
  @map.size
end

#to_sString

Creates a human-readable string representation of this map.

Returns:

  • (String)

    A string describing this map object.


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

TODO:

what does the variable x do?

Converts this map object to its standard XML representation

Returns:

  • (XML::Node)

    the node containing the 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