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    EOM\n}\n"

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Container

Returns a new instance of Container.


160
161
162
# File 'lib/app-ctx.rb', line 160

def initialize context
    @context = context
end

Instance Method Details

#execute(opts = {}) ⇒ Object


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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/app-ctx.rb', line 164

def execute opts = {}

    tmp = opts[:entrypoint] || :application_main
    entrypoint, clazz = tmp.to_s.split("#").reverse
    clazz = eval "#{clazz ||= opts[:class]}"

    pimp_my_hash(@context.values)

    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 
        pimp_my_hash(@context.values) # XXX refresh lost pimping
        # first try the default entry point
        app.send(entrypoint, @context)

    rescue NoMethodError => e
        if app.respond_to?(entrypoint)
            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 ArgumentError => e
                    app.send(op)
                rescue => e
                    DL.logger.error "oops: #{e}", e
                end
            end
        end
    end

    app # the created application instance
end

#inform(message_id, binding) ⇒ Object


155
156
157
158
# File 'lib/app-ctx.rb', line 155

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