Class: Groonga::Record
- Inherits:
-
Object
- Object
- Groonga::Record
- Defined in:
- lib/groonga/record.rb
Defined Under Namespace
Classes: AttributeHashBuilder
Instance Attribute Summary collapse
-
#table ⇒ Object
readonly
レコードが所属するテーブル.
Instance Method Summary collapse
-
#==(other) ⇒ Object
record と other が同じテーブルに属していて、さらに、 同じレコードIDを持つなら +true+ を返し、そうでなければ +false+ を返す。.
-
#[](column_name) ⇒ Object
このレコードの column_name で指定されたカラムの値を返す。.
-
#[]=(column_name, value) ⇒ Object
Sets column value of the record.
- #added=(added) ⇒ Object
- #added? ⇒ Boolean
-
#append(column_name, value) ⇒ Object
このレコードの column_name で指定されたカラムの値の最後に value を追加する。.
- #as_json ⇒ Object
-
#attributes ⇒ Object
レコードが所属しているテーブルで定義されているインデックス 型のカラムでない全カラムを対象とし、カラムの名前をキーとし たこのレコードのカラムの値のハッシュを返す。.
-
#clear_lock(options = {}) ⇒ Object
レコードが所属するテーブルのロックを強制的に解除する。.
-
#columns ⇒ Object
レコードが所属するテーブルの全てのカラムを返す。.
-
#decrement!(name, delta = nil) ⇒ Object
このレコードの name で指定されたカラムの値を delta だけ減 少する。 delta が +nil+ の場合は1減少する。.
-
#delete ⇒ Object
Delete the record.
-
#eql?(other) ⇒ Boolean
#== と同じ。.
-
#hash ⇒ Object
同じテーブルの同じIDのレコードに対しては常に同じハッシュ 値を返す。.
-
#have_column?(name) ⇒ Boolean
名前が name のカラムがレコードの所属するテーブルで定義され ているなら +true+ を返す。.
-
#increment!(name, delta = nil) ⇒ Object
このレコードの name で指定されたカラムの値を delta だけ増 加する。 delta が +nil+ の場合は1増加する。.
-
#index_column?(name) ⇒ Boolean
名前が name のカラムが索引カラム ( IndexColumn )であるなら +true+ を返す。.
-
#initialize(table, id, values = nil) ⇒ Record
constructor
table の id に対応するレコードを作成する。 values には各 カラムに設定する値を以下のような形式で指定する。.
- #inspect ⇒ Object
-
#key ⇒ Object
レコードの主キーを返す。.
-
#lock(options = {}, &block) ⇒ Object
レコードが所属するテーブルをロックする。ロックに失敗した場 合は ResourceDeadlockAvoided 例外が発生する。.
-
#locked?(options = {}) ⇒ Boolean
レコードが所属するテーブルがロックされていれば +true+ を返す。.
- #methods(include_inherited = true) ⇒ Object
-
#n_sub_records ⇒ Object
主キーの値が同一であったレコードの件数を返す。検索結果とし て生成されたテーブルのみに定義される。.
-
#prepend(column_name, value) ⇒ Object
このレコードの column_name で指定されたカラムの値の最初に value を追加する。.
-
#record_id ⇒ Object
レコードを一意に識別するための情報を返す。.
-
#record_raw_id ⇒ Object
(also: #id)
レコードのIDを返す。.
-
#reference_column?(name) ⇒ Boolean
名前が name のカラムが参照カラムであるなら +true+ を返す。.
-
#rename(new_key) ⇒ Object
Renames key of the record.
- #respond_to?(name) ⇒ Boolean
-
#scalar_column?(name) ⇒ Boolean
名前が name のカラムの値がスカラーであるなら +true+ を返す。.
-
#score ⇒ Object
レコードのスコア値を返す。検索結果として生成されたテーブル のみに定義される。.
-
#score=(new_score) ⇒ Object
Sets score.
-
#search(name, query, options = {}) ⇒ Object
名前が name の IndexColumn の search メソッドを呼ぶ。 query と options はそのメソッドにそのまま渡される。詳しく は IndexColumn#search を参照。.
-
#sub_records ⇒ SubRecords
Sub records of the record.
-
#support_key? ⇒ Boolean
record が所属するテーブルで主キーを使える場合は +true+ を返し、使えない場合は +false+ を返す。.
-
#support_score? ⇒ Boolean
#score が利用できる場合は +true+ を 返す。.
-
#support_sub_records? ⇒ Boolean
#n_sub_records が利用できる場合は +true+ を 返す。.
-
#support_value? ⇒ Boolean
created with value type, @false@ otherwise.
-
#to_json(*args) ⇒ String
The record formatted as JSON.
-
#unlock(options = {}) ⇒ Object
レコードが所属するテーブルのロックを解除する。.
-
#valid_id? ⇒ Boolean
レコードが持つIDが有効なIDであれば +true+ を返す。.
-
#value ⇒ Object
レコードの値を返す。.
-
#value=(value) ⇒ Object
レコードの値を設定する。既存の値は上書きされる。.
-
#vector_column?(name) ⇒ Boolean
名前が name のカラムの値がベクターであるなら +true+ を返す。.
Constructor Details
#initialize(table, id, values = nil) ⇒ Record
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/groonga/record.rb', line 42 def initialize(table, id, values=nil) @table = table @id = id @added = false if values values.each do |name, value| self[name] = value end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/groonga/record.rb', line 419 def method_missing(name, *args, &block) if /=\z/ =~ name.to_s base_name = $PREMATCH is_setter = true else base_name = name.to_s is_setter = false end _column = @table.column(base_name) if _column if is_setter _column.send("[]=", @id, *args, &block) else _column.send("[]", @id, *args, &block) end else super end end |
Instance Attribute Details
#table ⇒ Object (readonly)
レコードが所属するテーブル
27 28 29 |
# File 'lib/groonga/record.rb', line 27 def table @table end |
Instance Method Details
#==(other) ⇒ Object
record と other が同じテーブルに属していて、さらに、 同じレコードIDを持つなら +true+ を返し、そうでなければ +false+ を返す。
56 57 58 59 |
# File 'lib/groonga/record.rb', line 56 def ==(other) self.class == other.class and [table, id] == [other.table, other.id] end |
#[](column_name) ⇒ Object
このレコードの column_name で指定されたカラムの値を返す。
73 74 75 |
# File 'lib/groonga/record.rb', line 73 def [](column_name) @table.column_value(@id, column_name, :id => true) end |
#[]=(column_name, value) ⇒ Object #[]=(column_name, value_with_weight) ⇒ Object
Sets column value of the record.
99 100 101 |
# File 'lib/groonga/record.rb', line 99 def []=(column_name, value) @table.set_column_value(@id, column_name, value, :id => true) end |
#added=(added) ⇒ Object
366 367 368 |
# File 'lib/groonga/record.rb', line 366 def added=(added) @added = added end |
#added? ⇒ Boolean
361 362 363 |
# File 'lib/groonga/record.rb', line 361 def added? @added end |
#append(column_name, value) ⇒ Object
このレコードの column_name で指定されたカラムの値の最後に value を追加する。
105 106 107 |
# File 'lib/groonga/record.rb', line 105 def append(column_name, value) column(column_name).append(@id, value) end |
#as_json ⇒ Object
266 267 268 269 270 271 272 273 274 275 |
# File 'lib/groonga/record.rb', line 266 def as_json accessor = AttributeHashBuilder.new(self) do |value| if value.is_a?(Time) value.iso8601 else value end end accessor.build end |
#attributes ⇒ Object
レコードが所属しているテーブルで定義されているインデックス 型のカラムでない全カラムを対象とし、カラムの名前をキーとし たこのレコードのカラムの値のハッシュを返す。
return same attributes object if duplicate records exist.
261 262 263 264 |
# File 'lib/groonga/record.rb', line 261 def attributes accessor = AttributeHashBuilder.new(self) accessor.build end |
#clear_lock(options = {}) ⇒ Object
レコードが所属するテーブルのロックを強制的に解除する。
利用可能なオプションは現在は無い。
328 329 330 |
# File 'lib/groonga/record.rb', line 328 def clear_lock(={}) @table.clear_lock(.merge(:id => @id)) end |
#columns ⇒ Object
レコードが所属するテーブルの全てのカラムを返す。
252 253 254 |
# File 'lib/groonga/record.rb', line 252 def columns @table.columns end |
#decrement!(name, delta = nil) ⇒ Object
このレコードの name で指定されたカラムの値を delta だけ減 少する。 delta が +nil+ の場合は1減少する。
247 248 249 |
# File 'lib/groonga/record.rb', line 247 def decrement!(name, delta=nil) column(name).decrement!(@id, delta) end |
#delete ⇒ Object
Delete the record.
283 284 285 286 287 288 289 |
# File 'lib/groonga/record.rb', line 283 def delete if support_key? @table.delete(@id, :id => true) else @table.delete(@id) end end |
#eql?(other) ⇒ Boolean
#== と同じ。
62 63 64 |
# File 'lib/groonga/record.rb', line 62 def eql?(other) self == other end |
#hash ⇒ Object
同じテーブルの同じIDのレコードに対しては常に同じハッシュ 値を返す。
68 69 70 |
# File 'lib/groonga/record.rb', line 68 def hash @table.hash ^ @id.hash end |
#have_column?(name) ⇒ Boolean
名前が name のカラムがレコードの所属するテーブルで定義され ているなら +true+ を返す。
129 130 131 |
# File 'lib/groonga/record.rb', line 129 def have_column?(name) not @table.column(normalize_column_name(name)).nil? end |
#increment!(name, delta = nil) ⇒ Object
このレコードの name で指定されたカラムの値を delta だけ増 加する。 delta が +nil+ の場合は1増加する。
241 242 243 |
# File 'lib/groonga/record.rb', line 241 def increment!(name, delta=nil) column(name).increment!(@id, delta) end |
#index_column?(name) ⇒ Boolean
名前が name のカラムが索引カラム ( IndexColumn )であるなら +true+ を返す。
140 141 142 |
# File 'lib/groonga/record.rb', line 140 def index_column?(name) column(name).index? end |
#inspect ⇒ Object
371 372 373 374 375 376 377 |
# File 'lib/groonga/record.rb', line 371 def inspect if @table.closed? super.gsub(/>\z/, " (closed)>") else super.gsub(/>\z/, ", attributes: #{attributes.inspect}>") end end |
#key ⇒ Object
レコードの主キーを返す。
record が所属するテーブルが Array の場合は常 に +nil+ を返す。
169 170 171 172 173 174 175 |
# File 'lib/groonga/record.rb', line 169 def key if support_key? @key ||= @table.key(@id) else nil end end |
#lock(options = {}, &block) ⇒ Object
レコードが所属するテーブルをロックする。ロックに失敗した場 合は Groonga::ResourceDeadlockAvoided 例外が発生する。
ブロックを指定した場合はブロックを抜けたときにunlockする。
利用可能な option は以下の通り。
314 315 316 |
# File 'lib/groonga/record.rb', line 314 def lock(={}, &block) @table.lock(.merge(:id => @id), &block) end |
#locked?(options = {}) ⇒ Boolean
レコードが所属するテーブルがロックされていれば +true+ を返す。
利用可能なオプションは現在は無い。
335 336 337 |
# File 'lib/groonga/record.rb', line 335 def locked?(={}) @table.locked?(.merge(:id => @id)) end |
#methods(include_inherited = true) ⇒ Object
350 351 352 353 354 |
# File 'lib/groonga/record.rb', line 350 def methods(include_inherited=true) original_methods = super return original_methods unless include_inherited (original_methods + dynamic_methods).uniq end |
#n_sub_records ⇒ Object
主キーの値が同一であったレコードの件数を返す。検索結果とし て生成されたテーブルのみに定義される。
#support_sub_records? でこの値を利用でき るかがわかる。
219 220 221 |
# File 'lib/groonga/record.rb', line 219 def n_sub_records self["_nsubrecs"] end |
#prepend(column_name, value) ⇒ Object
このレコードの column_name で指定されたカラムの値の最初に value を追加する。
111 112 113 |
# File 'lib/groonga/record.rb', line 111 def prepend(column_name, value) column(column_name).prepend(@id, value) end |
#record_id ⇒ Object
レコードを一意に識別するための情報を返す。
record が所属するテーブルが Array の場合はID を返し、それ以外の場合は主キーを返す。
181 182 183 184 185 186 187 |
# File 'lib/groonga/record.rb', line 181 def record_id if support_key? key else id end end |
#record_raw_id ⇒ Object Also known as: id
レコードのIDを返す。
190 191 192 |
# File 'lib/groonga/record.rb', line 190 def record_raw_id @id end |
#reference_column?(name) ⇒ Boolean
名前が name のカラムが参照カラムであるなら +true+ を返す。
134 135 136 |
# File 'lib/groonga/record.rb', line 134 def reference_column?(name) column(name).reference? end |
#rename(new_key) ⇒ Object
Renames key of the record. Only available for DoubleArrayTrie table.
295 296 297 298 299 300 301 |
# File 'lib/groonga/record.rb', line 295 def rename(new_key) if @table.is_a?(DoubleArrayTrie) @table.update(@id, new_key, :id => true) else raise OperationNotSupported, "Only Groonga::DoubleArrayTrie table supports Groonga::Record#rename: <#{@table.class}>" end end |
#respond_to?(name) ⇒ Boolean
357 358 359 |
# File 'lib/groonga/record.rb', line 357 def respond_to?(name) super or !@table.column(name.to_s.sub(/=\z/, '')).nil? end |
#scalar_column?(name) ⇒ Boolean
名前が name のカラムの値がスカラーであるなら +true+ を返す。
154 155 156 |
# File 'lib/groonga/record.rb', line 154 def scalar_column?(name) column(name).scalar? end |
#score ⇒ Object
レコードのスコア値を返す。検索結果として生成されたテーブル のみに定義される。
197 198 199 |
# File 'lib/groonga/record.rb', line 197 def score self["_score"] end |
#score=(new_score) ⇒ Object
Sets score. Score column exists only search result table.
204 205 206 |
# File 'lib/groonga/record.rb', line 204 def score=(new_score) self["_score"] = new_score end |
#search(name, query, options = {}) ⇒ Object
名前が name の IndexColumn の search メソッドを呼ぶ。 query と options はそのメソッドにそのまま渡される。詳しく は IndexColumn#search を参照。
161 162 163 |
# File 'lib/groonga/record.rb', line 161 def search(name, query, ={}) column(name).search(query, ) end |
#sub_records ⇒ SubRecords
Returns Sub records of the record.
345 346 347 |
# File 'lib/groonga/record.rb', line 345 def sub_records SubRecords.new(self) end |
#support_key? ⇒ Boolean
record が所属するテーブルで主キーを使える場合は +true+ を返し、使えない場合は +false+ を返す。
117 118 119 |
# File 'lib/groonga/record.rb', line 117 def support_key? @table.support_key? end |
#support_score? ⇒ Boolean
#score が利用できる場合は +true+ を 返す。
210 211 212 |
# File 'lib/groonga/record.rb', line 210 def support_score? @table.have_column?("_score") # TODO delegate to Table end |
#support_sub_records? ⇒ Boolean
#n_sub_records が利用できる場合は +true+ を 返す。
225 226 227 |
# File 'lib/groonga/record.rb', line 225 def support_sub_records? @table.support_sub_records? end |
#support_value? ⇒ Boolean
created with value type, @false@ otherwise.
123 124 125 |
# File 'lib/groonga/record.rb', line 123 def support_value? @table.support_value? end |
#to_json(*args) ⇒ String
Returns the record formatted as JSON.
278 279 280 |
# File 'lib/groonga/record.rb', line 278 def to_json(*args) as_json.to_json(*args) end |
#unlock(options = {}) ⇒ Object
レコードが所属するテーブルのロックを解除する。
利用可能なオプションは現在は無い。
321 322 323 |
# File 'lib/groonga/record.rb', line 321 def unlock(={}) @table.unlock(.merge(:id => @id)) end |
#valid_id? ⇒ Boolean
レコードが持つIDが有効なIDであれば +true+ を返す。
340 341 342 |
# File 'lib/groonga/record.rb', line 340 def valid_id? @table.exist?(@id) end |
#value ⇒ Object
レコードの値を返す。
230 231 232 |
# File 'lib/groonga/record.rb', line 230 def value @table.value(@id, :id => true) end |
#value=(value) ⇒ Object
レコードの値を設定する。既存の値は上書きされる。
235 236 237 |
# File 'lib/groonga/record.rb', line 235 def value=(value) @table.set_value(@id, value, :id => true) end |
#vector_column?(name) ⇒ Boolean
名前が name のカラムの値がベクターであるなら +true+ を返す。
147 148 149 |
# File 'lib/groonga/record.rb', line 147 def vector_column?(name) column(name).vector? end |