Class: ActiveWindow::Application

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/active_window/application.rb

Direct Known Subclasses

VimMate::App

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Application

Returns a new instance of Application.



21
22
23
24
25
26
27
28
29
# File 'lib/active_window/application.rb', line 21

def initialize(options = {})
  @builder = Gtk::Builder.new
  @builder.add_from_file(find_view)
  @window = widget(options[:main_window] || 'main_window')
  @window.signal_connect("destroy") { Gtk.main_quit }
  @dot_file_prefs = ActiveWindow::DotFile.read
  @database = options[:database]
  run_callbacks :after_initialize
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



6
7
8
# File 'lib/active_window/application.rb', line 6

def builder
  @builder
end

#controllerObject

Returns the value of attribute controller.



5
6
7
# File 'lib/active_window/application.rb', line 5

def controller
  @controller
end

#databaseObject

Returns the value of attribute database.



5
6
7
# File 'lib/active_window/application.rb', line 5

def database
  @database
end

#windowObject

Returns the value of attribute window.



5
6
7
# File 'lib/active_window/application.rb', line 5

def window
  @window
end

Class Method Details

.widget(*widgets) ⇒ Object



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

def self.widget(*widgets)
  widgets.each do |widget|
    name = widget.to_s.camelize
    define_method widget do
      builder[name]
    end
  end
end

Instance Method Details

#default_databaseObject



41
42
43
# File 'lib/active_window/application.rb', line 41

def default_database
  @dot_file_prefs[:db]
end

#dispatch(handler) ⇒ Object

gets a handler like “config.reset_settings” returns the method (reset_settings) of the controller (ConfigController) to use as a callback



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_window/application.rb', line 80

def dispatch(handler)
  controller_name,action = handler.to_s.split('.')
  unless controller_name and action
    return(error "cannot parse handler '%s'" % handler)
  end

  name = controller_name.to_sym 
  ctrl = controller[name]
  unless ctrl
    puts controller.inspect
    return(error "no controller '%s' defined" % controller_name.camelize)
  end
  
  unless ctrl.respond_to? action
    return(error "controller '%s' does not have a method '%s'" % [ctrl.class, action])
  end
  
  method = ctrl.method(action)
end

#post_setupObject

calls post_setup on each controller



71
72
73
74
75
# File 'lib/active_window/application.rb', line 71

def post_setup
  controller.each do |name,ctrl|
    ctrl.post_setup
  end
end

#runObject



37
38
39
# File 'lib/active_window/application.rb', line 37

def run
  Gtk.main
end

#save_default_database(hash) ⇒ Object



45
46
47
48
# File 'lib/active_window/application.rb', line 45

def save_default_database(hash)
  @dot_file_prefs[:db] = hash
  @dot_file_prefs.save
end

#setupObject

creates the controllers, connects the signals calls ‘setup’ on each controller



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_window/application.rb', line 52

def setup
  @controller = {}
  
  Module.constants.grep(/.Controller$/).each do |klass|
     ctrl = Kernel.const_get(klass).instance
     ctrl.application = self
     ctrl.setup
     name = klass.to_s.sub('Controller','').underscore.to_sym # eg MyFirstController -> :my_first
     controller[name] = ctrl
  end

  builder.connect_signals do |handler_name|
    dispatch(handler_name)
  end

  post_setup
end

#startObject



31
32
33
34
35
# File 'lib/active_window/application.rb', line 31

def start
  setup
  window.show
  run
end

#widget(name) ⇒ Object



17
18
19
# File 'lib/active_window/application.rb', line 17

def widget(name)
  builder[name]
end