Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tutorial Walk Through #323

Closed
derks opened this issue Jun 23, 2015 · 3 comments
Closed

Add Tutorial Walk Through #323

derks opened this issue Jun 23, 2015 · 3 comments
Labels

Comments

@derks
Copy link
Member

derks commented Jun 23, 2015

The doc is pretty intense... need something to go in-between Quickstart and Read All This Shit To Understand All The Things.

@derks derks self-assigned this Jun 23, 2015
@derks derks added this to the 2.8.0 Stable milestone Jun 23, 2015
@fxstein
Copy link
Contributor

fxstein commented Aug 17, 2015

For what its worth: I am looking for some more real world examples of controllers, interfaces and handlers. When to use what.

For example I have an App class that needs to perform special tasks to init the overall configuration. Like assemble more complex variables or objects based on the app config settings.

I am also looking for how to add capabilities to have the app communicate with external interfaces. e.g. perform REST gets and posts and leverage the config settings for the interface/handler.

I assume controllers are only for tasks that are exposed to the CLI. But I am not quite sure when to use Interfaces and handlers.

Sorry if these are Noob questions. Still trying to figure everything out.

@derks
Copy link
Member Author

derks commented Jan 11, 2016

@fxstein I appreciate your feedback here, and again apologize for the delay in development (my $dayjob has been busy). To touch on your questions a bit:

For example I have an App class that needs to perform special tasks to init the overall configuration. Like assemble more complex variables or objects based on the app config settings.

For this, I generally tie into CementApp.validate_config() which is intended to be used for this purpose more or less. The validate_config method is called after configuration files are parsed... I've used it for doing something like the following (due to ConfigParser limitations):

from cement.core.foundation import CementApp
from cement.utils.misc import is_true

class MyApp(CementApp):
    class Meta:
        label = 'myapp'

    def validate_config(self):
        # convert 1, 0, true, false, etc to boolean
        my_param = self.config.get('myapp', 'my_param')
        self.config.set('myapp', 'my_param', is_true(my_param))

I am also looking for how to add capabilities to have the app communicate with external interfaces. e.g. perform REST gets and posts and leverage the config settings for the interface/handler.

For this, I use a combination of post_argument_parsing hook, and CementApp.extend():

from cement.core import hook

def extend_my_api_client(app):
    # do something to setup an api client object
    my_api_client = SomeAPIClientClass()

    app.extend('my_api_client', my_api_client)

hook.register('post_argument_parsing', extend_my_api_client)

You can then access app.my_api_client throughout your application. Maybe app.my_api_client.get(12345) (whatever the object does).

I assume controllers are only for tasks that are exposed to the CLI. But I am not quite sure when to use Interfaces and handlers.

Yes, controllers are specifically for exposing operations to the end-user via CLI commands/arguments. Interfaces/Handlers are primarily for segmenting bits of code that might have alternative use cases (handlers). Take for example a database interface. You might want to define an IDatabase interface that specifies exactly what functions an implemenation handler must support. That might be simple CRUD operations like create, read, update, delete... where the actually operations are implemented in the handler, but the application (or developer) can have a standardized usage interface. in that case IDatabase defines the interface (i.e. that implementation must provide create(), read(), etc) but you might have unique implementations like MySQLDatabaseHandler, PostgresDatabaseHandler, etc.... the usage of the handler would be the same (app.db.read(...), app.db.update(...), etc.

Does that make sense?

@derks derks removed this from the 2.8.0 Stable milestone Feb 25, 2016
@derks derks removed their assignment Dec 6, 2018
@derks
Copy link
Member Author

derks commented Sep 2, 2020

This can be closed, complete doc/api rewrites took place when Cement 3 was released. https://builtoncement.com

@derks derks closed this as completed Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants