Class: Tube::StationGroup

Inherits:
Array
  • Object
show all
Defined in:
lib/station_group.rb

Overview

Really just an array that can have a name, plus appropriate #to_json and #to_xml methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args) ⇒ StationGroup

:call-seq: StationGroup.new(name, size=0, obj=nil) StationGroup.new(name, array) StationGroup.new(name, size) {|index| block }

See Array.new.



15
16
17
18
# File 'lib/station_group.rb', line 15

def initialize( name, *args )
  @name = name
  super( *args )
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/station_group.rb', line 7

def name
  @name
end

Instance Method Details

#to_hashObject

:call-seq: station_group.to_hash -> hash

Returns a hash representation of the object. Also calls #to_hash on its contents.

{"Closed stations" => [array contents...]}


26
27
28
# File 'lib/station_group.rb', line 26

def to_hash
  {@name => to_a.map {|e| e.to_hash}}
end

#to_json(*args) ⇒ Object

:call-seq: station_group.to_json -> string

Returns a string of JSON representing the object.

'{"Closed stations":[array contents...]}'


35
36
37
# File 'lib/station_group.rb', line 35

def to_json( *args )
  to_hash.to_json( *args )
end

#to_xml(as_string = true) ⇒ Object

:call-seq: station_group.to_xml -> string station_group.to_xml(false) -> rexml_document.

Returns a string of XML representing the object.

<station_group>
  <name>Closed stations</name>
  <stations>
    contents of the array as xml...
  </stations>
</station_group>

Alternately pass false as the only argument to get an instance of REXML::Document.



53
54
55
56
57
58
59
60
61
62
# File 'lib/station_group.rb', line 53

def to_xml( as_string=true )
  doc = REXML::Document.new
  root = doc.add_element( "station_group" )
  root.add_element( "name" ).add_text( name )
  stations = root.add_element( "stations" )
  each do |e|
    stations.add_element( e.to_xml( false ) )
  end
  if as_string then doc.to_s else doc end
end