Class: AlPersistSh
Overview
Note:
sh設定ファイルへのAlPersistインターフェース
擬似的にAlPersistのインターフェースを真似ているが、 データの性質は違うので注意する。 このクラスでは、イメージ的にはRDBでいうところの 横持ち構造になっていて、1行のみデータが存在する。
Constant Summary collapse
- REX_DATA =
/\A\s*(\w+)=["'](.*)["']/
Instance Attribute Summary collapse
-
#filename ⇒ String
readonly
保存ファイル名.
Attributes inherited from AlPersist
#persist_base, #pkeys, #result, #search_condition, #values
Class Method Summary collapse
Instance Method Summary collapse
-
#create(values = nil) ⇒ Boolean
(also: #entry)
新規登録.
-
#delete(values = nil) ⇒ Boolean
削除.
-
#initialize(fname) ⇒ AlPersistSh
constructor
constructor.
-
#read(values = nil) ⇒ Boolean
データを読み込み、内部(@values) に保持する。.
-
#search(param = {}) ⇒ Array<AlPersist>
(also: #all)
データを読み込む。.
-
#update(values = nil) ⇒ Boolean
更新.
Methods inherited from AlPersist
#[], #[]=, #get_next_offset, #get_previous_offset, #pkey
Constructor Details
#initialize(fname) ⇒ AlPersistSh
constructor
47 48 49 50 |
# File 'lib/al_persist_sh.rb', line 47 def initialize( fname ) super( nil, nil ) @filename = fname end |
Instance Attribute Details
#filename ⇒ String (readonly)
Returns 保存ファイル名.
40 41 42 |
# File 'lib/al_persist_sh.rb', line 40 def filename @filename end |
Class Method Details
.connect(dsn = nil) ⇒ AlPersistSh
ファクトリ
34 35 36 |
# File 'lib/al_persist_sh.rb', line 34 def self.connect( dsn = nil ) return self.new( dsn ) end |
Instance Method Details
#create(values = nil) ⇒ Boolean Also known as: entry
Note:
新規登録
引数の指定があれば、その値を一旦内部値(@values)にしたうえで登録する。 キーの合致するデータがあれば更新し、なければ新規登録する。
131 132 133 |
# File 'lib/al_persist_sh.rb', line 131 def create( values = nil ) entry_main( :create, values ) end |
#delete(values = nil) ⇒ Boolean
Note:
削除
ファイル中の、キーの合致するデータを削除する。 他のPersistクラスと、動作が違うので注意する。 引数の指定があれば、その値を一旦内部値(@values)にしたうえで削除する。 消すための処理は、keyが合致することだけをみていて、値は必要ではない。 ファイルが存在しなかったり、項目が存在せずに値を消すことができなくても成功とする。
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/al_persist_sh.rb', line 95 def delete( values = nil ) @values = values if values begin file = open( @filename, 'r' ) rescue Errno::ENOENT return true end fout = Tempfile.new( "al-temp", File.dirname(@filename) ) while text = file.gets if REX_DATA =~ text k = $1 next if @values.has_key?( k ) next if @values.has_key?( k.to_sym ) end fout.write( text ) end file.close() File.rename( fout.path, @filename ) fout.close() return true end |
#read(values = nil) ⇒ Boolean
Note:
データを読み込み、内部(@values) に保持する。
ファイルが存在しなくても、成功とする。
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/al_persist_sh.rb', line 61 def read( values = nil ) @values = {} begin file = open( @filename, 'r' ) rescue Errno::ENOENT return true end while text = file.gets next if REX_DATA !~ text k = $1 v = $2.gsub(/\\(u\{[\da-fA-F]+\}|x[\da-fA-F]{2}|\d{3}|[a-zA-Z\\"'])/) { eval( %Q("\\#{$1}") ) } @values[k.to_sym] = v end file.close() return true end |
#search(param = {}) ⇒ Array<AlPersist> Also known as: all
Note:
データを読み込む。
他のPersistクラスとの互換性の為に定義する。
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/al_persist_sh.rb', line 158 def search( param = {} ) obj = self.dup() obj.read() if obj.values.empty? @search_condition[:total_rows] = 0 @search_condition[:num_rows] = 0 return [] else @search_condition[:total_rows] = 1 @search_condition[:num_rows] = 1 return [obj] end end |
#update(values = nil) ⇒ Boolean
Note:
更新
引数の指定があれば、その値を一旦内部値(@values)にしたうえで登録する。
145 146 147 |
# File 'lib/al_persist_sh.rb', line 145 def update( values = nil ) entry_main( :update, values ) end |