Module: Cms::Settings
Defined Under Namespace
Classes: CmsModuleProxy, InvalidModuleName, ModuleConfigurationExists, ModuleNotRegistered
Instance Method Summary collapse
-
#delete(module_name) ⇒ Object
Destroys the CmsModule object.
-
#method_missing(method_id, *args) ⇒ Object
This method_missing implementation enables client code to call arbitrary methods on the Settings module.
-
#modules ⇒ Object
Retruns an array of module names the Cms::Settings module knows about.
-
#register(module_name) ⇒ Object
Manually registered modules are ignored by the synchronize method.
-
#synchronize ⇒ Object
cms managed modules are those that are declared as dependencies on environment.rb.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args) ⇒ Object
This method_missing implementation enables client code to call arbitrary methods on the Settings module. Undefined methods whose name does not conform to BCMS’s module naming convention are handled elsewhere (presumably rasing a NoMethodError exception)
If a module with name equal to the called method has been registered previously, a module method with the same name is defined (so it does not go through method missing again) and a proxy object is returned.
If the module has not been registered previously, a ModuleNotRegistered exception is raised.
Given: Cms::Settings.modules => [“bcms_s3”, “bcms_seo_sitemap”]
Cms::Settings.bcms_s3 => #<Cms::Settings: bcms_s3 => “account_id”=>“NEW_ID” Cms::Settings.bcms_news => ModuleNotRegistered Cms::Settings.foo => NoMethodError
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/bcms_settings/cms/settings.rb', line 149 def method_missing(method_id, *args) method_name = method_id.to_s unless method_name =~ CmsModule::NAME_REGEX super(method_id, *args) end define_method(method_name) do CmsModuleProxy.new(find_module(method_name)) end send(method_name) end |
Instance Method Details
#delete(module_name) ⇒ Object
Destroys the CmsModule object. Trying to delete a module that has not been registered raises an exception:
Cms::Settings.modules => [“bcms_s3”, “bcms_seo_sitemap”] Cms::Settings.delete(“bcms_news”) => ModuleNotRegistered
At the moment it is possible to delete cms managed modules although they will be automatically registered again if Cms::Settings.synchronize is called.
126 127 128 |
# File 'lib/bcms_settings/cms/settings.rb', line 126 def delete(module_name) remove_modules [module_name.to_s] end |
#modules ⇒ Object
Retruns an array of module names the Cms::Settings module knows about.
- in environment.rb
-
gem.bcms_s3 gem.bcms_news
Cms::Settings.modules => [“bcms_s3”, “bcms_news”] Cms::Settings.register(“bcms_blog”) Cms::Settings.modules => [“bcms_s3”, “bcms_news”, “bcms_blog”]
61 62 63 |
# File 'lib/bcms_settings/cms/settings.rb', line 61 def modules registered_modules end |
#register(module_name) ⇒ Object
Manually registered modules are ignored by the synchronize method.
Cms::Settings.register(“bcms_foo”) Cms::Settings.bcms_foo will be prsisted in the the database until manually deleted.
Manually registered module names must conform to BCMS’s module naming conventions, so this call will raise an InvalidModuleName exception: Cms::Settings.register(“foo”) => InvalidModuleName
Modules can only be registered once: Cms::Settings.modules => [“bcms_s3”, “bcms_seo_sitemap”] Cms::Settings.register(“bcms_s3”) => ModuleConfigurationExists
****************** **IMPORTANT NOTE** ****************** When developing modules that use the Settings API, it is necessary to register them manually since they can’t declare themselves as dependencies. This can be done in an initializer, however, since modules can only be registered once, every time the modules’ project boots, an exception will be raised.
One possible workaround is to check if the module is registered already:
unless Cms::Settings.modules.include?(‘bcms_seo_sitemap’)
Cms::Settings.register('bcms_seo_sitemap')
end
A marginally better approach would be something like:
unless Cms::Settings.registered?(‘bcms_seo_sitemap’)
Cms::Settings.register('bcms_seo_sitemap')
end
Another possibility is to have the register method fail silently if the module already exists or just log a warning.
I do like the exceptions approach because it makes it evident that somehow, somewhere, for some reason, someone registered that module previously and may be using the configuration object for something.
This is particularly important because in reality, ‘registered modules’ need not to even be modules at all. Someone may just register ‘bcms_my_configuration’ for whatever purpose even from the console.
111 112 113 |
# File 'lib/bcms_settings/cms/settings.rb', line 111 def register(module_name) register_modules [module_name], false end |
#synchronize ⇒ Object
cms managed modules are those that are declared as dependencies on environment.rb. The synchronize method keeps these installed modules in sync with the database automatically, creating a record for declared dependencies and deleting records for bcms_modules that are no longer installed.
Cms::Settings.synchronize can be called as part of BrowserCMS’ initialization process.
If this method is never called, there won’t be any cms managed ‘automatic’ modules, in which case all modules must register themselves calling Cms::Settings.register(“bcms_xyz”) ‘ Conversely, if this method is called, all installed bcms modules will get a configuration object whether they need it or not.
43 44 45 46 47 48 |
# File 'lib/bcms_settings/cms/settings.rb', line 43 def synchronize if CmsModule.table_exists? register_modules(installed_modules - registered_modules) remove_modules(managed_modules - installed_modules) end end |