Class: HappyGirl

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

Overview

$:.unshift(File::join(File::dirname(File::dirname(__FILE__)), “/lib”))

Constant Summary collapse

VERSION =
'1.1.2'

Instance Method Summary collapse

Constructor Details

#initializeHappyGirl

Returns a new instance of HappyGirl.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/happy_girl.rb', line 33

def initialize()
  readConfig()
  
  font24 = TkFont.new('arial').configure('size'=>24, 'family' => 'bold')
  font18 = TkFont.new('times').configure('size' => 18, 'family' => 'bold')
  
  @root = TkRoot.new()
  @entry = TkLabel.new(@root) {
    font font24
    justify 'left'
  }
  @entry.pack("side" => 'top', "fill" => 'y')

  @why = TkLabel.new(@root) {
    font font18
  }
  setMessage()
  @why.pack("fill" => "x", "side" => "left" )
  
  image = TkLabel.new(@root) {
    image TkPhotoImage.new { file File::dirname(__FILE__) + '/heart.gif' }
  }.pack('side' => "left", "fill" => "both")
      
  button = TkButton.new(@root) { 
    text "Accept the love..." 
    justify "right"
  }.pack("side" => "bottom", "fill" => "x")

  button.bind("Button-1") {
    @root.withdraw()
  }
end

Instance Method Details

#initMessageIndex(smi) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/happy_girl.rb', line 8

def initMessageIndex(smi) 
  return unless @msgIdx.nil?
  if smi.nil? or smi.value.to_i.to_s != smi.value 
     @msgIdx = rand(@messages.length)
  else
    @msgIdx = smi.value.to_i
  end
  @msgIdx = rand(@messages.length) if @msgIdx.nil? or @msgIdx < 0 or @msgIdx >= @messages.length
end

#readConfigObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/happy_girl.rb', line 18

def readConfig()
  begin
    @cfg = YAML::parse(File.open("#{ENV['HOME']}/.happygirl.yaml")) 
  rescue Exception
    @cfg = YAML::parse(File.open(File::dirname(__FILE__) + '/messages.yaml')) 
  end
  @messages = @cfg["Messages"].value
  initMessageIndex(@cfg["StartMessageIndex"])
  unless @cfg["NextMessageIsRandom"].nil? or @cfg["NextMessageIsRandom"].value != "true"
    @randomNext = true
  else 
    @randomNext = false
  end
end

#setMessageObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/happy_girl.rb', line 66

def setMessage()
  readConfig()
  @why.text(@messages[@msgIdx].value)
  @entry.text(@cfg["Question"].value)
  @root.title(@cfg["WindowTitle"].value)
  @root.deiconify()
  if @randomNext
    @msgIdx = rand(@messages.length)
  else
    @msgIdx += 1
    @msgIdx %= @messages.length
  end
end

#startObject



80
81
82
83
84
85
86
87
88
# File 'lib/happy_girl.rb', line 80

def start()
  th = Thread.new do
    loop do
      setMessage()
      sleep(@cfg["SecondsToWait"].value.to_i)
    end
  end
  Tk.mainloop()
end