Class: SleipnirAPI::Profile

Inherits:
Object
  • Object
show all
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

# デフォルト値
bar = prof.script.foo_section.bar_key
# 以下と同じ
# bar = prof.ini("Script.ini").section("foo_section").key("bar_key")

bar.write_string("foobar")
bar.get_string                              #=> "foobar"
bar.delete
bar.get_string(:default => "default value") #=> "default value"

# 暗号化
bar_cipher = prof.script.foo_section.bar_key(:cipher => true)
bar_cipher.write_string("foobar")
bar_cipher.get_string                       #=> "foobar"
bar.get_string                              #=> "tibv&DmD[vq)"
bar_cipher.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", key_bar, "foobar")
prof.get_string("section_foo", key_bar)                              #=> "foobar"
prof.delete("section_foo", key_bar)
prof.get_string("section_foo", key_bar, :default => "default value") #=> "default value"

# 暗号化
prof.write_string("section_foo", key_bar, "foobar", :cipher => true)
prof.get_string("section_foo", key_bar, :cipher => true)             #=> "foobar"
prof.get_string("section_foo", key_bar)                              #=> "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

Instance Method Summary collapse

Methods included from DataUtil

#check_data, #str

Methods included from OptionArgument

#options

Methods included from OptionUtil

parse_option_arguments

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_optsObject (readonly)

default options



74
75
76
# File 'lib/sleipnir_api/profile.rb', line 74

def default_opts
  @default_opts
end

#sleipnirObject (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 を付与します。

Raises:

  • (ArgumentError)


254
255
256
257
258
259
260
261
# File 'lib/sleipnir_api/profile.rb', line 254

def expand_user_path(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.expand_path(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 = options(opts, :cipher, :ini, :default)
  if opts[:ini]
    api.GetProfileIntEx(section, key, opts[:default], expand_user_path(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 = options(opts, :cipher, :ini, :default)
  if opts[:ini]
    api.GetProfileStringEx(section, key, opts[:default], expand_user_path(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), options(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_pathObject

<ini_dir>\script.ini のパスを返します。



264
265
266
# File 'lib/sleipnir_api/profile.rb', line 264

def script_ini_path
  expand_user_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 = options(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 = options(opts, :cipher)
  if opts[:cipher]
    api.WriteProfileStringCipher(section, key, data)
  else
    api.WriteProfileString(section, key, data)
  end
end