Class: ActiveGroonga::Base
- Inherits:
-
Object
- Object
- ActiveGroonga::Base
show all
- Extended by:
- ActiveModel::Naming
- Includes:
- Callbacks, Persistence, Validations, ActiveModel::AttributeMethods, ActiveModel::Conversion
- Defined in:
- lib/active_groonga/base.rb
Constant Summary
- @@configurations =
{}
- @@context =
nil
- @@encoding =
"utf8"
Class Attribute Summary (collapse)
Class Method Summary
(collapse)
Instance Method Summary
(collapse)
Methods included from Callbacks
#destroy
#save, #save!, #valid?
#becomes, #delete, #destroy, #destroyed?, #new_record?, #persisted?, #reload, #save, #save!, #update_attribute, #update_attributes, #update_attributes!
Constructor Details
- (Base) initialize(record_or_attributes = nil)
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# File 'lib/active_groonga/base.rb', line 262
def initialize(record_or_attributes=nil)
self.class.define_column_accessors
@id = nil
@key = nil
@score = nil
@new_record = true
@destroyed = false
@attributes = initial_attributes
@attributes_cache = {}
if record_or_attributes.is_a?(Groonga::Record)
reload_attributes(record_or_attributes)
else
reload_attributes
self.attributes = (record_or_attributes || {})
end
end
|
Class Attribute Details
+ (Integer) limit
77
|
# File 'lib/active_groonga/base.rb', line 77
class_attribute :limit
|
+ (Array<Array<String, Symbol>>) sort_keys
61
|
# File 'lib/active_groonga/base.rb', line 61
class_attribute :sort_keys
|
Class Method Details
+ (Object) all(options = {})
144
145
146
|
# File 'lib/active_groonga/base.rb', line 144
def all(options={})
create_result_set(table)
end
|
80
81
82
83
84
85
86
87
88
|
# File 'lib/active_groonga/base.rb', line 80
def configure(configuration)
case configuration
when String, Symbol
configure(configurations[configuration.to_s])
when Hash
self.database_path = configuration["database"]
self.encoding = configuration["encoding"]
end
end
|
+ (Object) context
152
153
154
|
# File 'lib/active_groonga/base.rb', line 152
def context
@@context ||= Groonga::Context.default
end
|
+ (Object) count
148
149
150
|
# File 'lib/active_groonga/base.rb', line 148
def count
table.size
end
|
+ (Object) create(attributes = nil, &block)
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/active_groonga/base.rb', line 94
def create(attributes=nil, &block)
if attributes.is_a?(Array)
attributes.collect do |nested_attributes|
create(nested_attributes, &block)
end
else
object = new(attributes)
yield(object) if block_given?
object.save
object
end
end
|
+ (Object) custom_reference_class(column_name)
237
238
239
240
241
|
# File 'lib/active_groonga/base.rb', line 237
def custom_reference_class(column_name)
@reference_mapping ||= {}
column_name = column_name.to_s
@reference_mapping[column_name]
end
|
+ (Object) database
90
91
92
|
# File 'lib/active_groonga/base.rb', line 90
def database
@@database ||= Database.new(database_path)
end
|
+ (Object) database_path=(path)
225
226
227
228
229
|
# File 'lib/active_groonga/base.rb', line 225
def database_path=(path)
path = Pathname(path) if path.is_a?(String)
@@database_path = path
@@database = nil
end
|
+ (Object) define_column_accessors
181
182
183
184
185
186
|
# File 'lib/active_groonga/base.rb', line 181
def define_column_accessors
attribute_names = table.columns.collect do |column|
column.local_name
end
define_attribute_methods(attribute_names)
end
|
+ (Object) define_method_attribute(name)
209
210
211
212
213
214
215
|
# File 'lib/active_groonga/base.rb', line 209
def define_method_attribute(name)
generated_attribute_methods.module_eval do
define_method(name) do
read_attribute(name)
end
end
end
|
+ (Object) define_method_attribute=(name)
217
218
219
220
221
222
223
|
# File 'lib/active_groonga/base.rb', line 217
def define_method_attribute=(name)
generated_attribute_methods.module_eval do
define_method("#{name}=") do |new_value|
write_attribute(name, new_value)
end
end
end
|
+ (Object) encoding=(new_encoding)
156
157
158
159
160
161
162
163
|
# File 'lib/active_groonga/base.rb', line 156
def encoding=(new_encoding)
return if @@encoding == new_encoding
@@encoding = new_encoding
database_opened = !context.database.nil?
Groonga::Context.default = nil
Groonga::Context.default_options = {:encoding => @@encoding}
database.reopen if database_opened
end
|
+ (Boolean) exists?(record_id)
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/active_groonga/base.rb', line 122
def exists?(record_id)
record_id = record_id.record_id if record_id.respond_to?(:record_id)
if table.support_key?
not table[record_id].nil?
else
begin
record_id = Integer(record_id)
rescue ArgumentError
return false
end
table.exist?(record_id)
end
end
|
+ (Object) find(record_id, options = {})
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/active_groonga/base.rb', line 107
def find(record_id, options={})
record_id = record_id.record_id if record_id.respond_to?(:record_id)
unless table.support_key?
begin
record_id = Integer(record_id)
rescue ArgumentError
return nil
end
return nil unless table.exist?(record_id)
end
record = table[record_id]
return nil if record.nil?
instantiate(record)
end
|
+ (Object) i18n_scope
243
244
245
|
# File 'lib/active_groonga/base.rb', line 243
def i18n_scope
:activegroonga
end
|
+ (Object) inspect
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/active_groonga/base.rb', line 188
def inspect
return super if table.nil?
sorted_columns = table.columns.sort_by do |column|
column.local_name
end
columns_info = sorted_columns.collect do |column|
"#{column.local_name}: #{column.range.name}"
end
"#{name}(#{columns_info.join(', ')})"
end
|
+ (Object) instantiate(record)
199
200
201
202
203
204
205
206
207
|
# File 'lib/active_groonga/base.rb', line 199
def instantiate(record)
object = new(record)
object.instance_variable_set("@id", record.id)
if record.support_key?
object.instance_variable_set("@key", record.key)
end
object.instance_variable_set("@new_record", false)
object
end
|
+ (Object) reference_class(column_name, klass)
231
232
233
234
235
|
# File 'lib/active_groonga/base.rb', line 231
def reference_class(column_name, klass)
@reference_mapping ||= {}
column_name = column_name.to_s
@reference_mapping[column_name] = klass
end
|
+ (Object) select(options = {})
136
137
138
139
140
141
142
|
# File 'lib/active_groonga/base.rb', line 136
def select(options={})
return all(options) unless block_given?
records = table.select do |record|
yield(record)
end
create_result_set(records, :expression => records.expression)
end
|
+ (Object) table
177
178
179
|
# File 'lib/active_groonga/base.rb', line 177
def table
@table ||= context[table_name]
end
|
+ (Object) table_name(name = nil)
165
166
167
168
169
170
171
|
# File 'lib/active_groonga/base.rb', line 165
def table_name(name=nil)
if name.nil?
@table_name ||= model_name.plural
else
self.table_name = name
end
end
|
+ (Object) table_name=(name)
173
174
175
|
# File 'lib/active_groonga/base.rb', line 173
def table_name=(name)
@table_name = name
end
|
Instance Method Details
- (Object) ==(other)
331
332
333
|
# File 'lib/active_groonga/base.rb', line 331
def ==(other)
other.is_a?(self.class) and other.id == id
end
|
- (Object) attributes
321
322
323
|
# File 'lib/active_groonga/base.rb', line 321
def attributes
@attributes
end
|
- (Object) attributes=(attributes)
325
326
327
328
329
|
# File 'lib/active_groonga/base.rb', line 325
def attributes=(attributes)
attributes.each do |key, value|
send("#{key}=", value)
end
end
|
- (Object) hash
335
336
337
|
# File 'lib/active_groonga/base.rb', line 335
def hash
id.hash
end
|
- (Boolean) have_column?(name)
279
280
281
|
# File 'lib/active_groonga/base.rb', line 279
def have_column?(name)
table.have_column?(name)
end
|
- (Object) id
283
284
285
|
# File 'lib/active_groonga/base.rb', line 283
def id
@id
end
|
- (Object) inspect
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
# File 'lib/active_groonga/base.rb', line 347
def inspect
inspected_attributes = []
if table.support_key?
inspected_attributes << "key: #{key}"
else
inspected_attributes << "id: #{id}"
end
sorted_attributes = @attributes.sort_by do |key, _|
key
end
sorted_attributes.each do |key, value|
inspected_attributes << "#{key}: #{value.inspect}"
end
"\#<#{self.class.name} #{inspected_attributes.join(', ')}>"
end
|
- (Object) key
287
288
289
|
# File 'lib/active_groonga/base.rb', line 287
def key
@key
end
|
- (Object) key=(key)
291
292
293
294
295
|
# File 'lib/active_groonga/base.rb', line 291
def key=(key)
raise NoKeyTableError.new(table) unless table.support_key?
raise KeyOverrideError.new(table, key) unless new_record?
@key = key
end
|
- (Object) read_attribute(name)
339
340
341
|
# File 'lib/active_groonga/base.rb', line 339
def read_attribute(name)
@attributes[name]
end
|
- (Object) record_id
305
306
307
308
309
310
311
|
# File 'lib/active_groonga/base.rb', line 305
def record_id
if table.support_key?
key
else
id
end
end
|
- (Object) record_raw_id
313
314
315
|
# File 'lib/active_groonga/base.rb', line 313
def record_raw_id
id
end
|
- (Object) score
297
298
299
|
# File 'lib/active_groonga/base.rb', line 297
def score
@score
end
|
- (Object) score=(score)
301
302
303
|
# File 'lib/active_groonga/base.rb', line 301
def score=(score)
@score = score
end
|
- (Object) table
363
364
365
|
# File 'lib/active_groonga/base.rb', line 363
def table
@table ||= self.class.table
end
|
- (Object) to_key
317
318
319
|
# File 'lib/active_groonga/base.rb', line 317
def to_key
persisted? ? [record_id] : nil
end
|
- (Object) write_attribute(name, value)
343
344
345
|
# File 'lib/active_groonga/base.rb', line 343
def write_attribute(name, value)
@attributes[name] = value
end
|