Class: ActiveGroonga::Base
- Inherits:
-
Object
- Object
- ActiveGroonga::Base
show all
- Extended by:
- Callbacks, Persistence, Validations, ActiveModel::Naming
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Conversion
- Defined in:
- lib/active_groonga/base.rb
Constant Summary
collapse
- @@configurations =
{}
- @@context =
nil
- @@encoding =
"utf8"
Class Attribute Summary collapse
Class 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!
Class Attribute Details
.limit ⇒ Integer
77
|
# File 'lib/active_groonga/base.rb', line 77
class_attribute :limit
|
.sort_keys ⇒ Array<Array<String, Symbol>>
61
|
# File 'lib/active_groonga/base.rb', line 61
class_attribute :sort_keys
|
Class Method Details
.==(other) ⇒ Object
331
332
333
|
# File 'lib/active_groonga/base.rb', line 331
def ==(other)
other.is_a?(self.class) and other.id == id
end
|
.all(options = {}) ⇒ Object
144
145
146
|
# File 'lib/active_groonga/base.rb', line 144
def all(options={})
create_result_set(table)
end
|
.attributes ⇒ Object
321
322
323
|
# File 'lib/active_groonga/base.rb', line 321
def attributes
@attributes
end
|
.attributes=(attributes) ⇒ Object
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
|
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
|
.context ⇒ Object
152
153
154
|
# File 'lib/active_groonga/base.rb', line 152
def context
@@context ||= Groonga::Context.default
end
|
.count ⇒ Object
148
149
150
|
# File 'lib/active_groonga/base.rb', line 148
def count
table.size
end
|
.create(attributes = nil, &block) ⇒ Object
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
|
.custom_reference_class(column_name) ⇒ Object
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
|
.database ⇒ Object
90
91
92
|
# File 'lib/active_groonga/base.rb', line 90
def database
@@database ||= Database.new(database_path)
end
|
.database_path=(path) ⇒ Object
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
|
.define_column_accessors ⇒ Object
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
|
.define_method_attribute(name) ⇒ Object
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
|
.define_method_attribute=(name) ⇒ Object
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
|
.encoding=(new_encoding) ⇒ Object
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
|
.exists?(record_id) ⇒ Boolean
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
|
.find(record_id, options = {}) ⇒ Object
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
|
.hash ⇒ Object
335
336
337
|
# File 'lib/active_groonga/base.rb', line 335
def hash
id.hash
end
|
.have_column?(name) ⇒ Boolean
279
280
281
|
# File 'lib/active_groonga/base.rb', line 279
def have_column?(name)
table.have_column?(name)
end
|
.i18n_scope ⇒ Object
243
244
245
|
# File 'lib/active_groonga/base.rb', line 243
def i18n_scope
:activegroonga
end
|
.id ⇒ Object
283
284
285
|
# File 'lib/active_groonga/base.rb', line 283
def id
@id
end
|
.initialize(record_or_attributes = nil) ⇒ Object
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
|
.inspect ⇒ Object
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
|
.instantiate(record) ⇒ Object
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
|
.key ⇒ Object
287
288
289
|
# File 'lib/active_groonga/base.rb', line 287
def key
@key
end
|
.key=(key) ⇒ Object
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
|
.read_attribute(name) ⇒ Object
339
340
341
|
# File 'lib/active_groonga/base.rb', line 339
def read_attribute(name)
@attributes[name]
end
|
.record_id ⇒ Object
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
|
.record_raw_id ⇒ Object
313
314
315
|
# File 'lib/active_groonga/base.rb', line 313
def record_raw_id
id
end
|
.reference_class(column_name, klass) ⇒ Object
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
|
.score ⇒ Object
297
298
299
|
# File 'lib/active_groonga/base.rb', line 297
def score
@score
end
|
.score=(score) ⇒ Object
301
302
303
|
# File 'lib/active_groonga/base.rb', line 301
def score=(score)
@score = score
end
|
.select(options = {}) ⇒ Object
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
|
.table ⇒ Object
177
178
179
|
# File 'lib/active_groonga/base.rb', line 177
def table
@table ||= context[table_name]
end
|
.table_name(name = nil) ⇒ Object
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
|
.table_name=(name) ⇒ Object
173
174
175
|
# File 'lib/active_groonga/base.rb', line 173
def table_name=(name)
@table_name = name
end
|
.to_key ⇒ Object
317
318
319
|
# File 'lib/active_groonga/base.rb', line 317
def to_key
persisted? ? [record_id] : nil
end
|
.write_attribute(name, value) ⇒ Object
343
344
345
|
# File 'lib/active_groonga/base.rb', line 343
def write_attribute(name, value)
@attributes[name] = value
end
|