Class: Benry::CmdApp::ApplicationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/benry/cmdapp.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, _registry: REGISTRY) ⇒ ApplicationContext

Returns a new instance of ApplicationContext.



1021
1022
1023
1024
1025
1026
1027
1028
# File 'lib/benry/cmdapp.rb', line 1021

def initialize(config, _registry: REGISTRY)
  @config        = config
  @_registry     = _registry
  #@scope_objects = {}     # {action_name => ActionScope}
  @status_dict   = {}      # {action_name => (:done|:doing)}
  @curr_action   = nil     # ActionMetadata
  @end_blocks    = []      # [Proc]
end

Instance Method Details

#_add_end_block(block) ⇒ Object

:nodoc:



1030
1031
1032
1033
# File 'lib/benry/cmdapp.rb', line 1030

def _add_end_block(block)  # :nodoc:
  @end_blocks << block
  nil
end

#invoke_action(action_name, args, kwargs, once: false) ⇒ Object

called from ActionScope#run_action_xxxx()



1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
# File 'lib/benry/cmdapp.rb', line 1070

def invoke_action(action_name, args, kwargs, once: false)  ## called from ActionScope#run_action_xxxx()
  action = action_name
  #; [!uw6rq] raises ActionError if action name is not a string.
  Util.name_should_be_a_string(action, 'Action', ActionError)
  #; [!dri6e] if called from other action containing prefix, looks up action with the prefix firstly.
   = nil
  if action !~ /:/ && @curr_action && @curr_action.name =~ /\A(.*:)/
    prefix = $1
     = @_registry.(prefix + action)
    action = prefix + action if 
  end
  #; [!ygpsw] raises ActionError if action not found.
   ||= @_registry.(action)
   != nil  or
    raise ActionError.new("#{action}: Action not found.")
  #; [!de6a9] raises ActionError if alias name specified.
  ! .alias?  or
    raise ActionError.new("#{action}: Action expected, but it is an alias.")
  return _invoke_action(, args, kwargs, once: once)
end

#start_action(action_name, cmdline_args) ⇒ Object

called from Application#run()



1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
# File 'lib/benry/cmdapp.rb', line 1051

def start_action(action_name, cmdline_args)  ## called from Application#run()
  #; [!2mnh7] looks up action metadata with action or alias name.
  , alias_args = @_registry.(action_name)
  #; [!0ukvb] raises CommandError if action nor alias not found.
   != nil  or
    raise CommandError.new("#{action_name}: Action nor alias not found.")
  #; [!9n46s] if alias has its own args, combines them with command-line args.
  args = alias_args + cmdline_args
  #; [!5ru31] options in alias args are also parsed as well as command-line options.
  #; [!r3gfv] raises OptionError if invalid action options specified.
  options = .parse_options(args)
  #; [!lg6br] runs action with command-line arguments.
  _invoke_action(, args, options, once: false)
  return nil
ensure
  #; [!jcguj] clears instance variables.
  teardown()
end