Skip to content

Commit

Permalink
Use special Assoc class to keep lists in order.
Browse files Browse the repository at this point in the history
  • Loading branch information
trans committed Oct 26, 2011
1 parent 3db0170 commit 2d503f0
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions lib/xoxo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#
module XOXO
# xoxo.rb version number
VERSION = "1.1.0"
VERSION = "1.2.0" # :erb: VERSION = "<%= version %>"

# Load and return a XOXO structure from the String, IO or StringIO or _xoxo_.
def self.load(xoxo)
Expand Down Expand Up @@ -82,6 +82,15 @@ def self.dump(struct, options={})
def self.make_xoxo(struct, class_name=nil)
s = ''
case struct
when Assoc
unless struct.empty?
s << "<dl>"
struct.each do |(key, value)|
s << "<dt>" << key.to_s << "</dt><dd>" << make_xoxo(value) << "</dd>"
end
s << "</dl>"
end

when Array
if class_name
s << %Q[<ol class="#{class_name}">]
Expand All @@ -106,7 +115,7 @@ def self.make_xoxo(struct, class_name=nil)
else
unless struct.empty?
s << "<dl>"
struct.each do |(key, value)|
struct.each do |key, value|
s << "<dt>" << key.to_s << "</dt><dd>" << make_xoxo(value) << "</dd>"
end
s << "</dl>"
Expand All @@ -120,24 +129,28 @@ def self.make_xoxo(struct, class_name=nil)
s << struct.to_s

when Struct
h = {}
a = Assoc.new
struct.each_pair do |k,v|
h[k] = v
a << [k,v]
end
s = make_xoxo(h, class_name)
s = make_xoxo(a, class_name)

else
h = {}
a = Assoc.new
struct.instance_variables.each do |iv|
key = iv.to_s.sub(/^@/, '')
h[key] = struct.instance_variable_get(iv)
a << [key, struct.instance_variable_get(iv)]
end
s = make_xoxo(h, class_name)
s = make_xoxo(a, class_name)
end

s
end

# Used to distinguish arrays from assoc arrays.
class Assoc < Array
end

end

class XOXO::Parser
Expand Down

0 comments on commit 2d503f0

Please sign in to comment.