Class: App::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/app-ctx.rb

Constant Summary collapse

Messages =
{
    :no_entrypoint  => "\n    ::app-ctx:: \n\n -> sorry, your main class (\#{clazz}) seems not to have a default\n -> 'application_main' entrypoint and no method argument was given, so i don't\n -> know what to do.\n\n    thank you, good bye and have a nice day\n\n"
}

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Container

Returns a new instance of Container.



149
150
151
# File 'lib/app-ctx.rb', line 149

def initialize context
    @context = context
end

Instance Method Details

#execute(clazz = nil) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/app-ctx.rb', line 153

def execute clazz = nil

    if block_given?
        warn "attached block takes precedence over class!" if clazz
        return yield(@context)
    end

    # create applicaton instance with or without context c'tor (this is
    # IoC: Constructor Dependency Injection)
    app = clazz.new(@context) rescue clazz.new

    setter_injection app, @context

    begin 
        # first try the default entry point
        app.application_main @context

    rescue NoMethodError => e
        if app.respond_to? :application_main
            DL.logger.error "failed invokation", e
        else
            if 0 == @context.argv.length 
                inform :no_entrypoint, binding()
                RDoc::usage
                return
            else
                # one or more args  =>  first arg is method with rest
                # as params
                begin
                    op = @context.argv.shift
                    app.send(op, @context)
                rescue => e
                    DL.logger.error "oops: #{e}", e
                end
            end
        end
    end

    app # the created application instance
end

#inform(message_id, binding) ⇒ Object



144
145
146
147
# File 'lib/app-ctx.rb', line 144

def inform message_id, binding
    m = Messages[message_id]
    puts eval("\"#{m}\"", binding)
end