Skip to content

Instantly share code, notes, and snippets.

@ashbb
Created March 28, 2011 13:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashbb/890466 to your computer and use it in GitHub Desktop.
Save ashbb/890466 to your computer and use it in GitHub Desktop.
A solution for the question posted by Lark Work. http://www.ruby-forum.com/topic/1369589
# Another solution with Shoes::Widget
class SomeDataProcessing < Shoes::Widget
Person = Struct.new :name, :last_name, :phone_number, :birthday, :city, :adress
def self.openOn(p_file)
new(p_file)
end
def initialize(p_file)
@file = p_file
@persons = {}
loadData
end
def listData
@persons.keys.sort.each {|p| showlistData(p)}
end
def loadData
File.foreach(@file) do |line|
name, last_name, phone_number, birthday, city, adress = line.chomp.split(",")
@persons[name] = Person.new(name, last_name, phone_number, birthday, city, adress)
end
end
def showData(p_name)
person = @persons[p_name]
if person.nil?
then
@show_text2 = "#{p_name} not in database."
end
stack do
@show_text2 = ["Name: #{person.name} #{person.last_name}",
"Phonenumber: #{person.phone_number}",
"Birthday: #{person.birthday}",
"City: #{person.city}",
"Adress: #{person.adress}"].join("\n")
end
edit_box @show_text2, :width => 250, :height => 200, :top => 140, :left => 380
end
def showlistData(p_name)
person = @persons[p_name]
if person.nil?
then
@show_text3 = "#{p_name} not in database."
end
@show_text3 = "#{person.name} #{person.last_name}"
edit_box @show_text3, :width => 250, :height => 200, :top => 140, :left => 380
end
end
Shoes.app :title => "PPID", :height => 400, :width => 700 do
@ds = some_data_processing("PPIDdata.txt")
background black, :height => 60
background gradient(rgb(1.0, 1.0, 1.0, 0.7), rgb(1.0, 1.0, 1.0, 0.0))
title "PPID", :align => "center", :stroke => white
caption "Personal: Person Info Database", :top => 80, :left => 20
stack :top => 130, :left => 50 do
para "Add to database:"
flow do
inscription "Name: "
@name = edit_line :width => 120
end
flow do
inscription "Lastname: "
@lastname = edit_line :width => 120
end
flow do
inscription "Phonenumber: "
@phonenumber = edit_line :width => 80
end
flow do
inscription "Birthday: "
@month = edit_line :width => 30
@day = edit_line :width => 30
@year = edit_line :width => 50
end
flow do
inscription "City: "
@city = edit_line :width => 120
end
flow do
inscription "Streetname: "
@streetname = edit_line :width => 120
@housenumber = edit_line :width => 50
end
@save_button = button "Save" do
open('PPIDdata.txt', 'a+') { |f|
f.puts "#{@name.text},#{@lastname.text},#{@phonenumber.text},#{@month.text}-#{@day.text}-#{@year.text},#{@city.text},#{@streetname.text},#{@housenumber.text}"}
stack do
@show_text = ["Name: #{@name.text} #{@lastname.text}",
"Phonenumber: #{@phonenumber.text}",
"Birthday: #{@month.text}-#{@day.text}-#{@year.text}",
"City: #{@city.text}",
"Adress: #{@streetname.text} #{@housenumber.text}"].join("\n")
end
edit_box @show_text, :width => 250, :height => 200, :top => 140, :left => 380
end
end
edit_box "", :width => 250, :height => 200, :top => 140, :left => 380
stack :top => 85, :left => 380 do
flow do
name = edit_line :width => 150
button "Search" do
#ds = SomeDataProcessing.openOn("PPIDdata.txt")
@ds.showData(name.text)
end
button "List" do
#dl = SomeDataProcessing.openOn("PPIDdata.txt")
@ds.listData
end
end
end
end
# A solution with a global variable $app
class SomeDataProcessing
Person = Struct.new :name, :last_name, :phone_number, :birthday, :city, :adress
def self.openOn(p_file)
new(p_file)
end
def initialize(p_file)
@file = p_file
@persons = {}
loadData
end
def listData
@show_text3 = ''
@persons.keys.sort.each {|p| showlistData(p)}
$app.instance_variable_get(:@eb).text = @show_text3
end
def loadData
File.foreach(@file) do |line|
name, last_name, phone_number, birthday, city, adress = line.chomp.split(",")
@persons[name] = Person.new(name, last_name, phone_number, birthday, city, adress)
end
end
def showData(p_name)
person = @persons[p_name]
if person.nil?
then
@show_text2 = "#{p_name} not in database."
else
@show_text2 = ["Name: #{person.name} #{person.last_name}",
"Phonenumber: #{person.phone_number}",
"Birthday: #{person.birthday}",
"City: #{person.city}",
"Adress: #{person.adress}"].join("\n")
end
$app.instance_variable_get(:@eb).text = @show_text2
end
def showlistData(p_name)
person = @persons[p_name]
if person.nil?
then
@show_text3 << "#{p_name} not in database."
else
@show_text3 << "#{person.name} #{person.last_name}\n"
end
end
end
Shoes.app :title => "PPID", :height => 400, :width => 700 do
$app = self
background black, :height => 60
background gradient(rgb(1.0, 1.0, 1.0, 0.7), rgb(1.0, 1.0, 1.0, 0.0))
title "PPID", :align => "center", :stroke => white
caption "Personal: Person Info Database", :top => 80, :left => 20
stack :top => 130, :left => 50 do
para "Add to database:"
flow do
inscription "Name: "
@name = edit_line :width => 120
end
flow do
inscription "Lastname: "
@lastname = edit_line :width => 120
end
flow do
inscription "Phonenumber: "
@phonenumber = edit_line :width => 80
end
flow do
inscription "Birthday: "
@month = edit_line :width => 30
@day = edit_line :width => 30
@year = edit_line :width => 50
end
flow do
inscription "City: "
@city = edit_line :width => 120
end
flow do
inscription "Streetname: "
@streetname = edit_line :width => 120
@housenumber = edit_line :width => 50
end
@save_button = button "Save" do
open('PPIDdata.txt', 'a+') { |f|
f.puts "#{@name.text},#{@lastname.text},#{@phonenumber.text},#{@month.text}-#{@day.text}-#{@year.text},#{@city.text},#{@streetname.text},#{@housenumber.text}"}
stack do
@show_text = ["Name: #{@name.text} #{@lastname.text}",
"Phonenumber: #{@phonenumber.text}",
"Birthday: #{@month.text}-#{@day.text}-#{@year.text}",
"City: #{@city.text}",
"Adress: #{@streetname.text} #{@housenumber.text}"].join("\n")
end
edit_box @show_text, :width => 250, :height => 200, :top => 140, :left => 380
end
end
@eb = edit_box "", :width => 250, :height => 200, :top => 140, :left => 380
stack :top => 85, :left => 380 do
flow do
name = edit_line :width => 150
button "Search" do
ds = SomeDataProcessing.openOn("PPIDdata.txt")
ds.showData(name.text)
end
button "List" do
dl = SomeDataProcessing.openOn("PPIDdata.txt")
dl.listData
end
end
end
end
test,test,test,test-test-test,test,test
testaa,dd,ee,5-5-5,ff,ss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment