Hey all,
I have just written a small widget that
shows a Ruby structure with mixed Hashes and
Arrays. In a future version I hope I can build
something much better that allows editing,
inserting and deleting.
This is still too simple to add to the samples
pages.
Any corrections and additions are very welcome.
Best regards,
Joao
----- bos
#!/usr/bin/env ruby
# structureview.rb
#
# Structure View
#
# Widget that can be used to view structures with
mixed Hashes and
# Arrays.
#
# Author: Joao Pedrosa - joaopedrosa@...
#
# You can redistribute it and/or modify it under the
terms of
# the Ruby's license.
#
# Big thanks to Masao Mutoh and the Ruby/GNOME2 team
for such a
# great binding to GTK+2.
# Big thanks to Matz the Great, creator and developer
of Ruby.
require 'gtk2'
include Gtk
class StructureView < ScrolledWindow
def initialize(structure = nil)
super()
@model = TreeStore.new(String)
@treeview = TreeView.new(@model)
@column = TreeViewColumn.new("Demo Structure",
CellRendererText.new, :text => 0)
#@column.title = "Demo Structure"
#@column.pack_start(CellRendererText.new, false)
@treeview.append_column(@column)
add(@treeview)
end
def structure= v
z = ""
@struct = v
view(z, v, "")
p 'aki'
print z
end
def create_iter(parent, text)
iter = @model.append(parent)
iter.set_value(0, text)
end
def view(z, estrut, caminho, parent = nil)
if estrut.class == Array
estrut.each_with_index { |v,i|
t = "#{caminho}/#{i}"
iter = create_iter(parent, t)
view(z, v, t, iter)
}
elsif estrut.class == Hash
estrut.each { |c,v|
#p "chave: #{c} valor: #{v}"
if [Array, Hash].include?(v.class)
t = "#{caminho}/#{c}"
iter = create_iter(parent, t)
view(z, v, t, iter)
else
t = "#{caminho}/#{c}=#{v}\n"
z << t
iter = create_iter(parent, t)
end
}
else
p 'pq aqui ?'
t = "#{caminho}=#{estrut}\n"
z << t
iter = create_iter(parent, t)
end
end
end
class DemoStruct
def DemoStruct.generate
[
"Books",
{:List => [
{
:Name => "Fiction Programming Ruby",
:Author => "Fiction Author",
:Value => 55.90 }, {
:Name => "Fiction Effective Ruby",
:Author => "Fiction Author 2",
:Value => 31.44 } ] },
"Music",
{:List => [
{
:Name => "Eminem",
:Author => "Cleaning out my closet",
:Value => 15.90 }, {
:Name => "Madonna",
:Author => "Like a virgin",
:Value => 16.10 } ] } ]
end
end
if $0 == __FILE__
Gtk.init
w = Window.new
w.signal_connect('destroy') { Gtk.main_quit }
w.add(structview = StructureView.new)
structview.structure = DemoStruct.generate
w.set_default_size(500,400)
w.show_all
Gtk.main
end
----- eos
__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com
|