Class: DbGui::Presenter::DbPresenter
- Inherits:
-
Object
- Object
- DbGui::Presenter::DbPresenter
- Includes:
- Glimmer::DataBinding::ObservableModel
- Defined in:
- app/db_gui/presenter/db_presenter.rb
Instance Attribute Summary collapse
-
#dbs ⇒ Object
Returns the value of attribute dbs.
-
#new_db ⇒ Object
Returns the value of attribute new_db.
-
#selected_db ⇒ Object
Returns the value of attribute selected_db.
Instance Method Summary collapse
- #delete ⇒ Object
-
#initialize ⇒ DbPresenter
constructor
A new instance of DbPresenter.
- #load_db_config ⇒ Object
- #new ⇒ Object
- #newable ⇒ Object
- #save ⇒ Object
- #save_config ⇒ Object
- #selected_db_name ⇒ Object
- #selected_db_name=(db_name) ⇒ Object
Constructor Details
#initialize ⇒ DbPresenter
Returns a new instance of DbPresenter.
11 12 13 14 15 16 17 |
# File 'app/db_gui/presenter/db_presenter.rb', line 11 def initialize @new_db = Model::Db.new @dbs = [@new_db] @selected_db = @new_db load_db_config selected_db.connect end |
Instance Attribute Details
#dbs ⇒ Object
Returns the value of attribute dbs.
9 10 11 |
# File 'app/db_gui/presenter/db_presenter.rb', line 9 def dbs @dbs end |
#new_db ⇒ Object
Returns the value of attribute new_db.
9 10 11 |
# File 'app/db_gui/presenter/db_presenter.rb', line 9 def new_db @new_db end |
#selected_db ⇒ Object
Returns the value of attribute selected_db.
9 10 11 |
# File 'app/db_gui/presenter/db_presenter.rb', line 9 def selected_db @selected_db end |
Instance Method Details
#delete ⇒ Object
50 51 52 53 |
# File 'app/db_gui/presenter/db_presenter.rb', line 50 def delete dbs.delete(selected_db) self.selected_db_name = new_db.name end |
#load_db_config ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/db_gui/presenter/db_presenter.rb', line 66 def load_db_config db_config_yaml = File.read(FILE_DB_CONFIGS) db_config = YAML.load(db_config_yaml) db_config[:dbs].each do |db_config| db = Model::Db.new db_config.each do |attribute, value| db.send("#{attribute}=", value) end self.dbs << db end self.selected_db_name = db_config[:selected_db_name] rescue => e puts "No database configurations stored yet. #{e.}" end |
#new ⇒ Object
46 47 48 |
# File 'app/db_gui/presenter/db_presenter.rb', line 46 def new self.selected_db_name = new_db.name end |
#newable ⇒ Object
19 20 21 |
# File 'app/db_gui/presenter/db_presenter.rb', line 19 def newable selected_db != new_db end |
#save ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/db_gui/presenter/db_presenter.rb', line 33 def save if selected_db == new_db && selected_db.name != Model::Db::NAME_NEW saved_db = new_db.clone new_db.reset dbs << saved_db # this will trigger an update to the combobox items self.selected_db_name = saved_db.name else notify_observers(:dbs) self.selected_db_name = selected_db.name end save_config end |
#save_config ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'app/db_gui/presenter/db_presenter.rb', line 55 def save_config dbs_attributes = dbs.reject {|db| db.name == Model::Db::NAME_NEW }.map(&:to_h) selected_db_name db_config = { selected_db_name:, dbs: dbs_attributes, } db_config_yaml = YAML.dump(db_config) File.write(FILE_DB_CONFIGS, db_config_yaml) end |
#selected_db_name ⇒ Object
23 24 25 |
# File 'app/db_gui/presenter/db_presenter.rb', line 23 def selected_db_name selected_db.name end |
#selected_db_name=(db_name) ⇒ Object
27 28 29 30 31 |
# File 'app/db_gui/presenter/db_presenter.rb', line 27 def selected_db_name=(db_name) self.selected_db = dbs.find { |db| db.name == db_name } notify_observers(:newable) save_config end |