Class: Command::Setting
Defined Under Namespace
Classes: InvalidVariableName, InvalidVariableType, UnknownVariableType
Constant Summary
collapse
- TYPE_OF_VALUE =
{
TrueClass => :boolean, FalseClass => :boolean, Fixnum => :integer,
Float => :float, String => :string
}
- INVISIBLE =
true
- SETTING_VARIABLES =
{
local: {
"convert.no-epub" => [:boolean, "EPUB変換を無効にするかどうか"],
"convert.no-mobi" => [:boolean, "MOBI変換を無効にするかどうか"],
"convert.no-strip" => [:boolean, "MOBIのstripを無効にするかどうか\n" +
" " * 6 + "※注意:KDP用のMOBIはstripしないでください"],
"convert.no-open" => [:boolean, "変換終了時に保存フォルダを開かない"],
"convert.copy_to" => [:directory, "変換したらこのフォルダにコピーする\n" +
" " * 6 + "※注意:存在しないフォルダだとエラーになる"],
"convert.inspect" => [:boolean, "常に変換時に調査結果を表示するか"],
"download.interval" => [:float, "各話DL時に指定した秒数待機する。デフォルト0"],
"device" => [:string, "変換、送信対象の端末(sendの--help参照)"],
},
global: {
"aozoraepub3dir" => [:directory, "AozoraEpub3のあるフォルダを指定", INVISIBLE],
"difftool" => [:string, "Diffで使うツールのパスを指定する"],
"difftool.arg" => [:string, "difftoolで使う引数を設定(オプション)"],
"no-color" => [:boolean, "表示内容に色を付けない"],
"over18" => [:boolean, "", INVISIBLE],
}
}
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from CommandBase
execute_and_rescue_exit
Constructor Details
Returns a new instance of Setting.
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/command/setting.rb', line 29
def initialize
super("[<name>=<value> ...] [options]")
@opt.separator <<-EOS
・各コマンドの設定の変更が出来ます。
・Global な設定はユーザープロファイルに保存され、すべての narou コマンドで使われます。
Local Variable List:
<name> <value> 説明
EOS
@opt.separator(get_variable_list_strings(:local))
@opt.separator("\n Global Variable List:")
@opt.separator(get_variable_list_strings(:global))
@opt.separator <<-EOS
Example:
narou setting --list
narou setting convert.no-open=true
narou setting convert.no-epub= # 右辺に何も書かないとその設定を削除できる
narou setting convert.copy_to=C:/dropbox/mobi
narou s convert.copy_to="C:\\Documents and Settings\\user\\epub"
Optioins:
EOS
@opt.on("-l", "--list", "現在の設定値を確認する") {
output_setting_list
exit 0
}
@opt.on("-a", "--all", "設定できる全ての変数名を表示する") {
@options["all"] = true
display_variable_list
exit 0
}
end
|
Class Method Details
.variable_type_to_description(type) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/command/setting.rb', line 66
def self.variable_type_to_description(type)
case type
when :boolean
"true/false "
when :integer
"整数 "
when :float
"小数点数 "
when :string
"文字列 "
when :directory
"フォルダパス"
when :file
"ファイルパス"
else
raise UnknownVariableType, type
end
end
|
Instance Method Details
#casting_variable(name, value) ⇒ Object
値の文字列を設定に基づいた型にキャストして、[scope, value] 形式で返す 不正な設定名もしくは値の場合は例外を吐く
98
99
100
101
102
103
104
105
|
# File 'lib/command/setting.rb', line 98
def casting_variable(name, value)
scope = get_scope_of_variable_name(name)
unless scope
raise InvalidVariableName, "#{name} は不明な名前です"
end
casted_value = string_cast_to_type(value, SETTING_VARIABLES[scope][name][0])
[scope, casted_value]
end
|
#display_variable_list ⇒ Object
215
216
217
218
219
220
221
|
# File 'lib/command/setting.rb', line 215
def display_variable_list
puts "Local Variable List:"
puts get_variable_list_strings(:local).gsub(/^ {4}/, "")
puts
puts "Global Variable List:"
puts get_variable_list_strings(:global).gsub(/^ {4}/, "")
end
|
#execute(argv) ⇒ Object
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
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/command/setting.rb', line 161
def execute(argv)
super
if argv.empty?
puts @opt.help
return
end
settings = {
local: LocalSetting.get["local_setting"],
global: GlobalSetting.get["global_setting"]
}
argv.each do |arg|
name, value = arg.split("=", 2).map(&:strip)
if name == ""
error "書式が間違っています。変数名=値 のように書いて下さい"
next
end
scope = get_scope_of_variable_name(name)
unless scope
error "#{name} という変数は存在しません"
next
end
if value.nil?
error "書式が間違っています。#{name}=値 のように書いて下さい"
next
end
if value == ""
settings[scope].delete(name)
puts "#{name} の設定を削除しました"
next
end
begin
scope, casted_value = casting_variable(name, value)
rescue InvalidVariableName, InvalidVariableType => e
error e.message
next
end
settings[scope][name] = casted_value
puts "#{name} を #{casted_value} に設定しました"
end
LocalSetting.get.save_settings("local_setting")
GlobalSetting.get.save_settings("global_setting")
end
|
#get_scope_of_variable_name(name) ⇒ Object
85
86
87
88
89
90
91
92
|
# File 'lib/command/setting.rb', line 85
def get_scope_of_variable_name(name)
[:local, :global].each do |scope|
if SETTING_VARIABLES[scope].include?(name)
return scope
end
end
nil
end
|
#get_variable_list_strings(scope) ⇒ Object
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/command/setting.rb', line 204
def get_variable_list_strings(scope)
result = ""
SETTING_VARIABLES[scope].each do |name, info|
if @options["all"] || info[2] != INVISIBLE
type_description = self.class.variable_type_to_description(info[0])
result << " <bold><green>#{name.ljust(18)}</green></bold> #{type_description} #{info[1]}\n".termcolor
end
end
result
end
|
#oneline_help ⇒ Object
11
12
13
|
# File 'lib/command/setting.rb', line 11
def oneline_help
"各コマンドの設定を変更します"
end
|
#output_setting_list ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/command/setting.rb', line 145
def output_setting_list
settings = {
local: LocalSetting.get["local_setting"],
global: GlobalSetting.get["global_setting"]
}
settings.each do |scope, scoped_settings|
puts "[#{scope.capitalize} Variables]"
scoped_settings.each do |name, value|
if value =~ / /
value = "'#{value}'"
end
puts "<bold><green>#{name}</green></bold>=#{value}".termcolor
end
end
end
|
#string_cast_to_type(value, type) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/command/setting.rb', line 107
def string_cast_to_type(value, type)
result = nil
case type
when :boolean
case value
when /true/i
result = true
when /false/i
result = false
else
raise InvalidVariableType, type
end
when :integer
if value =~ /^[+-]?\d+$/
result = value.to_i
else
raise InvalidVariableType, type
end
when :float
if value =~ /^[+-]?\d+\.?\d*$/
result = value.to_f
else
raise InvalidVariableType, type
end
when :directory, :file
if File.method("#{type}?").call(value)
result = File.expand_path(value)
else
raise InvalidVariableType, type
end
when :string
result = value
else
raise UnknownVariableType, type
end
result
end
|