Class: Idcf::Cli::Service::Base

Inherits:
Object
  • Object
show all
Includes:
Lib::Include::RecurringCalling
Defined in:
lib/idcf/cli/service/base.rb

Overview

model base

Constant Summary collapse

ARG_TYPE_REQ =
:req
ARG_TYPE_OPT =
:opt
ARG_TYPE_REST =
:rest
HELP_FORMAT =
{
  ARG_TYPE_REQ  => '<%<name>s>',
  ARG_TYPE_OPT  => '[%<name>s]',
  ARG_TYPE_REST => '[%<name>s ...]'
}.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lib::Include::RecurringCalling

#recurring_calling

Constructor Details

#initializeBase

Returns a new instance of Base.



82
83
84
85
# File 'lib/idcf/cli/service/base.rb', line 82

def initialize
  @last_command      = ''
  @last_command_args = []
end

Class Attribute Details

.optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/idcf/cli/service/base.rb', line 19

def options
  @options
end

Instance Attribute Details

#last_commandObject (readonly)

Returns the value of attribute last_command.



15
16
17
# File 'lib/idcf/cli/service/base.rb', line 15

def last_command
  @last_command
end

#last_command_argsObject (readonly)

Returns the value of attribute last_command_args.



15
16
17
# File 'lib/idcf/cli/service/base.rb', line 15

def last_command_args
  @last_command_args
end

Class Method Details

.descriptionObject

descritpion

Returns:

  • String



42
43
44
# File 'lib/idcf/cli/service/base.rb', line 42

def description
  ''
end

.make_param_sObject

make param string

Returns:

  • String



49
50
51
52
53
54
55
56
57
# File 'lib/idcf/cli/service/base.rb', line 49

def make_param_s
  cp = []
  valid_params.each do |param|
    f = HELP_FORMAT[param[0]]
    next if f.nil?
    cp << format(f, name: param[1])
  end
  cp.join(' ')
end

.option(name, attr) ⇒ Object

option

Parameters:

  • name (String)
  • attr (Hash)

Options Hash (attr):

  • :type (String)
  • :required (Boolean)
  • :desc (String)


34
35
36
37
# File 'lib/idcf/cli/service/base.rb', line 34

def option(name, attr)
  @options ||= {}
  @options[name] = attr
end

.resetObject

reset setting reset



23
24
25
# File 'lib/idcf/cli/service/base.rb', line 23

def reset
  @options = {}
end

.valid_paramsObject



59
60
61
62
63
64
65
# File 'lib/idcf/cli/service/base.rb', line 59

def valid_params
  [].tap do |result|
    new.method(:do).parameters.each do |param|
      result << param if target_param?(param[1])
    end
  end
end

Instance Method Details

#between_param?(arg_size) ⇒ Boolean

between params

Parameters:

  • params (Array)
  • arg_size (int)

Returns:

  • (Boolean)

Raises:



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/idcf/cli/service/base.rb', line 103

def between_param?(arg_size)
  param = self.class.valid_params
  p_cnt = param.size
  return true if method_rest?(param)
  opt_cnt = method_option_cnt(param)

  min = p_cnt - opt_cnt
  msg = format('Argument: %<arg>s', arg: self.class.make_param_s)
  cli_error msg unless arg_size.between?(min, p_cnt)
  true
end

#cli_error(msg) ⇒ Object

cli error

Parameters:

  • msg (String)

Raises:



150
151
152
# File 'lib/idcf/cli/service/base.rb', line 150

def cli_error(msg)
  raise Idcf::Cli::Error::CliError, msg
end

#do(_client, _o, *_args) ⇒ Object

do

Parameters:

  • _client (Mixed)
  • _o (Hash)

    options

  • *_args (Array)

Returns:

  • Hash

Raises:



94
95
96
# File 'lib/idcf/cli/service/base.rb', line 94

def do(_client, _o, *_args)
  cli_error 'Require override'
end

#method_option_cnt(params) ⇒ Object

option count

Parameters:

  • params (Array)

Returns:

  • int



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/idcf/cli/service/base.rb', line 134

def method_option_cnt(params)
  result = 0
  return result if params.nil?
  params.each do |param|
    case param[0]
    when ARG_TYPE_OPT
      result += 1
    end
  end
  result
end

#method_rest?(params) ⇒ Boolean

is rest

Parameters:

  • params (Array)

Returns:

  • (Boolean)

    Boolean



119
120
121
122
123
124
125
126
127
128
# File 'lib/idcf/cli/service/base.rb', line 119

def method_rest?(params)
  return false if params.nil?
  params.each do |param|
    case param[0]
    when ARG_TYPE_REST
      return true
    end
  end
  false
end