Class: SleipnirAPI::Profile
- Inherits:
-
Object
- Object
- SleipnirAPI::Profile
- Includes:
- DataUtil, OptionArgument, Util
- Defined in:
- lib/sleipnir_api/profile.rb,
lib/sleipnir_api/profile/ini.rb,
lib/sleipnir_api/profile/key.rb,
lib/sleipnir_api/profile/util.rb,
lib/sleipnir_api/profile/section.rb
Overview
このクラスは Sleipnir の COM オブジェクト (Sleipnir.API) の wrapper クラスです。Sleipnir の ini ファイルを操作する低レベルな API を定義しています。
ini ファイル・セクション・キーに対応するオブジェクトを用意してあるので通常はそちらを利用してください。
-
SleipnirAPI::Profile::Ini
-
SleipnirAPI::Profile::Section
-
SleipnirAPI::Profile::Key
このオブジェクトは SleipnirAPI::Sleipnir#profile で取得します。
高レベル API の例
pnir = SleipnirAPI.new
prof = pnir.profile
# デフォルト値
= prof.script.foo_section.
# 以下と同じ
# bar = prof.ini("Script.ini").section("foo_section").key("bar_key")
.write_string("foobar")
.get_string #=> "foobar"
.delete
.get_string(:default => "default value") #=> "default value"
# 暗号化
= prof.script.foo_section.(:cipher => true)
.write_string("foobar")
.get_string #=> "foobar"
.get_string #=> "tibv&DmD[vq)"
.get_string(:cipher => false) #=> "tibv&DmD[vq)"
# 連番データの取得
history = prof.History.History2.key("URL-data0").list("URLCount")
低レベル API の例
pnir = SleipnirAPI.new
prof = pnir.profile
# デフォルト値
prof.write_string("section_foo", , "foobar")
prof.get_string("section_foo", ) #=> "foobar"
prof.delete("section_foo", )
prof.get_string("section_foo", , :default => "default value") #=> "default value"
# 暗号化
prof.write_string("section_foo", , "foobar", :cipher => true)
prof.get_string("section_foo", , :cipher => true) #=> "foobar"
prof.get_string("section_foo", ) #=> "tF4DxDH)[uUu"
# 連番データの取得
history = prof.list("History2", "URL-data0", "URLCount", :ini => "History.ini")
Defined Under Namespace
Modules: DataUtil, OptionArgument Classes: Ini, Key, ProfileElement, ReadOnlyError, Section
Instance Attribute Summary collapse
-
#default_opts ⇒ Object
readonly
default options.
-
#sleipnir ⇒ Object
readonly
SleipnirAPI::Sleipnir object.
Instance Method Summary collapse
-
#delete(section, key) ⇒ Object
<ini_dir>\script.ini から指定されたキーを削除します。.
-
#expand_user_path(ini) ⇒ Object
SleipnirAPI::Sleipnir#user_path から expand_path します。.
-
#get_int(section, key, opts = nil) ⇒ Object
call-seq: get_int(section, key) get_int(section, key, :default => -1) get_int(section, key, :cipher => true) get_int(section, key, :cipher => true, :default => -1) get_int(section, key, :ini => ini_filename) get_int(section, key, :ini => ini_filename, :default => -1).
-
#get_string(section, key, opts = nil) ⇒ Object
call-seq: get_string(section, key) get_string(section, key, :default => “default value”) get_string(section, key, :cipher => true) get_string(section, key, :cipher => true, :default => “default value”) get_string(section, key, :ini => ini_filename) get_string(section, key, :ini => ini_filename, :default => “default value”).
-
#ini(name, opts = nil) ⇒ Object
指定された ini ファイルを操作する SleipnirAPI::Profile::Ini オブジェクトを返します。.
-
#initialize(sleipnir, default_opts = nil) ⇒ Profile
constructor
A new instance of Profile.
-
#list(section, keyfmt, countkey = "Count", opts = nil, &block) ⇒ Object
call-seq: list(section, keyfmt, countkey=“Count”) list(section, keyfmt, countkey=“Count”, :default => “default value”) list(section, keyfmt, countkey=“Count”, :cipher => true) list(section, keyfmt, countkey=“Count”, :cipher => true, :default => “default value”) list(section, keyfmt, countkey=“Count”, :ini => ini_filename) list(section, keyfmt, countkey=“Count”, :ini => ini_filename, :default => “default value”).
-
#list_key_format(keyfmt) ⇒ Object
連番キーを生成するためのフォーマット文字列を返します。.
-
#method_missing(mid, *args, &block) ⇒ Object
メソッド名を ini ファイル名とみなして SleipnirAPI::Profile::Ini オブジェクトを返します。.
-
#script_ini_path ⇒ Object
<ini_dir>\script.ini のパスを返します。.
-
#write_int(section, key, data, opts = nil) ⇒ Object
call-seq: write_int(section, key, data) write_int(section, key, data, :cipher => true).
-
#write_string(section, key, data, opts = nil) ⇒ Object
call-seq: write_string(section, key, data) write_string(section, key, data, :cipher => true).
Methods included from DataUtil
Methods included from OptionArgument
Methods included from OptionUtil
Methods included from Util
#api, #ensure_version, #join_keyword
Constructor Details
#initialize(sleipnir, default_opts = nil) ⇒ Profile
Returns a new instance of Profile.
76 77 78 79 80 |
# File 'lib/sleipnir_api/profile.rb', line 76 def initialize(sleipnir, default_opts = nil) @sleipnir = sleipnir @user_path = sleipnir.user_path @default_opts = default_opts end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mid, *args, &block) ⇒ Object
メソッド名を ini ファイル名とみなして SleipnirAPI::Profile::Ini オブジェクトを返します。
pnir = SleipnirAPI.connect
proxy = pnir.profile.Proxy(:default => 123)
proxy.get_int("Proxy", "Count")
See Also: #ini
286 287 288 |
# File 'lib/sleipnir_api/profile.rb', line 286 def method_missing(mid, *args, &block) ini(mid.to_s, *args, &block) end |
Instance Attribute Details
#default_opts ⇒ Object (readonly)
default options
74 75 76 |
# File 'lib/sleipnir_api/profile.rb', line 74 def default_opts @default_opts end |
#sleipnir ⇒ Object (readonly)
SleipnirAPI::Sleipnir object
71 72 73 |
# File 'lib/sleipnir_api/profile.rb', line 71 def sleipnir @sleipnir end |
Instance Method Details
#delete(section, key) ⇒ Object
<ini_dir>\script.ini から指定されたキーを削除します。
244 245 246 247 |
# File 'lib/sleipnir_api/profile.rb', line 244 def delete(section, key) section, key = str(section, key) api.DeleteProfileKey(section, key) end |
#expand_user_path(ini) ⇒ Object
SleipnirAPI::Sleipnir#user_path から expand_path します。
-
引数は to_s で文字列に変換されます。
-
拡張子がなければ .ini を付与します。
254 255 256 257 258 259 260 261 |
# File 'lib/sleipnir_api/profile.rb', line 254 def (ini) raise ArgumentError, "Invalid ini file name: #{ini.inspect}" if ini.nil? or ini == "" ini = ini.to_s if File.extname(ini) == "" ini = File.basename(ini, ".*") + ".ini" end File.(ini, @user_path) end |
#get_int(section, key, opts = nil) ⇒ Object
call-seq:
get_int(section, key)
get_int(section, key, :default => -1)
get_int(section, key, :cipher => true)
get_int(section, key, :cipher => true, :default => -1)
get_int(section, key, :ini => ini_filename)
get_int(section, key, :ini => ini_filename, :default => -1)
int 型データを読み込みます。詳細は get_string を参照してください。
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/sleipnir_api/profile.rb', line 193 def get_int(section, key, opts = nil) section, key = str(section, key) opts = (opts, :cipher, :ini, :default) if opts[:ini] api.GetProfileIntEx(section, key, opts[:default], (opts[:ini])) elsif opts[:cipher] api.GetProfileIntCipher(section, key, opts[:default]) else api.GetProfileInt(section, key, opts[:default]) end end |
#get_string(section, key, opts = nil) ⇒ Object
call-seq:
get_string(section, key)
get_string(section, key, :default => "default value")
get_string(section, key, :cipher => true)
get_string(section, key, :cipher => true, :default => "default value")
get_string(section, key, :ini => ini_filename)
get_string(section, key, :ini => ini_filename, :default => "default value")
string 型データを読み込みます。
-
:iniキーワード引数で、データを読み込む ini ファイルを指定できます。-
デフォルトは <ini_dir>\script.ini です。
-
:iniに相対パスを指定した場合、SleipnirAPI::Sleipnir#user_path からの相対パスと見なします。
-
-
:cipherに true を指定すると暗号化されたデータを複合化して読み込みます。 -
:defaultでキーが存在しないときに返す値を指定できます。 -
:iniと:cipherは同時には指定できません。同時に指定した場合:cipherは無視されます。
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/sleipnir_api/profile.rb', line 170 def get_string(section, key, opts = nil) section, key = str(section, key) opts = (opts, :cipher, :ini, :default) if opts[:ini] api.GetProfileStringEx(section, key, opts[:default], (opts[:ini])) elsif opts[:cipher] api.GetProfileStringCipher(section, key, opts[:default]) else api.GetProfileString(section, key, opts[:default]) end end |
#ini(name, opts = nil) ⇒ Object
指定された ini ファイルを操作する SleipnirAPI::Profile::Ini オブジェクトを返します。
pnir = SleipnirAPI.connect
proxy = pnir.profile.ini("Proxy.ini", :default => 123)
proxy.get_int("Proxy", "Count")
See Also: #method_missing
275 276 277 |
# File 'lib/sleipnir_api/profile.rb', line 275 def ini(name, opts = nil) Ini.new(self, str(name), (opts)) end |
#list(section, keyfmt, countkey = "Count", opts = nil, &block) ⇒ Object
call-seq:
list(section, keyfmt, countkey="Count")
list(section, keyfmt, countkey="Count", :default => "default value")
list(section, keyfmt, countkey="Count", :cipher => true)
list(section, keyfmt, countkey="Count", :cipher => true, :default => "default value")
list(section, keyfmt, countkey="Count", :ini => ini_filename)
list(section, keyfmt, countkey="Count", :ini => ini_filename, :default => "default value")
以下のような連番キーのデータをすべて読み込み配列で返します。
[History2]
SearchCount=2
Search-data0="Ruby"
Search-data1="Sleipnir"
-
keyfmtには連番キーを生成するためのテンプレート文字列を指定します。テンプレート文字列からフォーマット文字列への変換ルールは #list_key_format を参照してください。 -
countkeyには連番キーの個数を記録しているキー名を指定します。デフォルトは “Count” です。 -
optsの説明は #get_string を参照してください。 -
blockを指定するとフォーマットされた連番キー(文字列)を引数に block を評価してその結果を返します。
例:
pnir = SleipnirAPI.connect
prof = pnir.profile
prof.list("History2", "Search-data0", "SearchCount", :ini => "History.ini") #=> ["Ruby", "Sleipnir"]
See Also: #list_key_format
113 114 115 116 117 |
# File 'lib/sleipnir_api/profile.rb', line 113 def list(section, keyfmt, countkey="Count", opts = nil, &block) keyfmt = list_key_format(keyfmt) block ||= lambda {|key| get_string(section, key, opts) } (0...get_int(section, countkey, opts)).map{|i| keyfmt % i }.map(&block) end |
#list_key_format(keyfmt) ⇒ Object
連番キーを生成するためのフォーマット文字列を返します。
フォーマット文字列は以下のルールで生成します。
-
引数に「%(数字)d」が含まれていればそのまま返す。
-
引数に連続する 0 があれば、そこを連番箇所と判断して %d に変換して返す。
-
0 が複数箇所に存在する場合は最後の 0 が利用される
-
連続する 0 の場合、幅指定のフォーマットになる
-
-
0 が存在しない場合は、以下の場所を連番箇所と判断して %d に変換して返す。
-
最初のアンダースコアの前
-
文字列末尾
-
例:
list_key_format("Search-data%d") #=> Search-data%d
list_key_format("Foo_Bar0_Name") #=> Foo_Bar%d_Name
list_key_format("Foo0_Bar0_Name") #=> Foo0_Bar%d_Name
list_key_format("Foo0_Bar000_Name") #=> Foo0_Bar%03d_Name
list_key_format("Foo_Bar_Name") #=> Foo%d_Bar_Name
list_key_format("Name") #=> Name%d
142 143 144 145 146 147 148 149 |
# File 'lib/sleipnir_api/profile.rb', line 142 def list_key_format(keyfmt) return keyfmt if keyfmt =~ /%\d*d/ return keyfmt if keyfmt.sub!(/(0+)(\D*)\z/) { n = $1.length (n == 1) ? "%d#{$2}" : "%0#{n}d#{$2}" } return keyfmt.sub(/(_|\z)/) { "%d#{$1}" } end |
#script_ini_path ⇒ Object
<ini_dir>\script.ini のパスを返します。
264 265 266 |
# File 'lib/sleipnir_api/profile.rb', line 264 def script_ini_path ("script.ini") end |
#write_int(section, key, data, opts = nil) ⇒ Object
call-seq:
write_int(section, key, data)
write_int(section, key, data, :cipher => true)
<ini_dir>\script.ini に int 型データを書き込みます。
-
:cipherに true を指定すると暗号化してデータを書き込みます。
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/sleipnir_api/profile.rb', line 232 def write_int(section, key, data, opts = nil) section, key = str(section, key) check_data(section, key, data, Integer) opts = (opts, :cipher) if opts[:cipher] api.WriteProfileIntCipher(section, key, data) else api.WriteProfileInt(section, key, data) end end |
#write_string(section, key, data, opts = nil) ⇒ Object
call-seq:
write_string(section, key, data)
write_string(section, key, data, :cipher => true)
<ini_dir>\script.ini に string 型データを書き込みます。
-
:cipherに true を指定すると暗号化してデータを書き込みます。
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/sleipnir_api/profile.rb', line 213 def write_string(section, key, data, opts = nil) section, key = str(section, key) check_data(section, key, data, String) opts = (opts, :cipher) if opts[:cipher] api.WriteProfileStringCipher(section, key, data) else api.WriteProfileString(section, key, data) end end |