Class: ActiveGroonga::Base — activegroonga - ラングバ

Class: ActiveGroonga::Base

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

Methods included from Validations

#save, #save!, #valid?

Methods included from Persistence

#becomes, #delete, #destroy, #destroyed?, #new_record?, #persisted?, #reload, #save, #save!, #update_attribute, #update_attributes, #update_attributes!

Constructor Details

- (Base) initialize(record_or_attributes = nil) {|_self| ... }

Returns a new instance of Base

Yields:

  • (_self)

Yield Parameters:



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/active_groonga/base.rb', line 260

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
  yield(self) if block_given?
end

Class Attribute Details

+ (Integer) limit

The default limit for ResultSet#sort and ResultSet#paginate.

Examples:

# Limits sorted records by default.
User.limit = 20

# Doesn't limit sorted records by default.
User.limit = nil

Returns:

  • (Integer)

    The default limit value.

Since:

  • 1.0.5



75
# File 'lib/active_groonga/base.rb', line 75

class_attribute :limit

+ (Array<Array<String, Symbol>>) sort_keys

The default sort keys for ResultSet#sort and ResultSet#paginate.

Examples:

# Sorts by "name" column value in ascending order by default.
User.sort_keys = [["name", :ascending]]

# Sorts by "name" column value in ascending order and
# sorts by "age" column value in descending order for
# the same name records by default.
User.sort_keys = [["name", :ascending], ["age", :descending]]

# Sorts by id value in ascending order by default.
User.sort_keys = nil

Returns:

  • (Array<Array<String, Symbol>>)

    An array of sort key. Each sort key is an array of sort key column name and order.

Since:

  • 1.0.5



59
# File 'lib/active_groonga/base.rb', line 59

class_attribute :sort_keys

Class Method Details

+ (Object) all(options = {})



142
143
144
# File 'lib/active_groonga/base.rb', line 142

def all(options={})
  create_result_set(table)
end

+ (Object) configure(configuration)



78
79
80
81
82
83
84
85
86
# File 'lib/active_groonga/base.rb', line 78

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



150
151
152
# File 'lib/active_groonga/base.rb', line 150

def context
  @@context ||= Groonga::Context.default
end

+ (Object) count



146
147
148
# File 'lib/active_groonga/base.rb', line 146

def count
  table.size
end

+ (Object) create(attributes = nil, &block)



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_groonga/base.rb', line 92

def create(attributes=nil, &block)
  if attributes.is_a?(Array)
    attributes.collect do |nested_attributes|
      create(nested_attributes, &block)
    end
  else
    object = new(attributes, &block)
    object.save
    object
  end
end

+ (Object) custom_reference_class(column_name)



235
236
237
238
239
# File 'lib/active_groonga/base.rb', line 235

def custom_reference_class(column_name)
  @reference_mapping ||= {}
  column_name = column_name.to_s
  @reference_mapping[column_name]
end

+ (Object) database



88
89
90
# File 'lib/active_groonga/base.rb', line 88

def database
  @@database ||= Database.new(database_path)
end

+ (Object) database_path=(path)



223
224
225
226
227
# File 'lib/active_groonga/base.rb', line 223

def database_path=(path)
  path = Pathname(path) if path.is_a?(String)
  @@database_path = path
  @@database = nil
end

+ (Object) define_column_accessors



179
180
181
182
183
184
# File 'lib/active_groonga/base.rb', line 179

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)



207
208
209
210
211
212
213
# File 'lib/active_groonga/base.rb', line 207

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)



215
216
217
218
219
220
221
# File 'lib/active_groonga/base.rb', line 215

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)



154
155
156
157
158
159
160
161
# File 'lib/active_groonga/base.rb', line 154

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)

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/active_groonga/base.rb', line 119

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 = {})



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/active_groonga/base.rb', line 104

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



241
242
243
# File 'lib/active_groonga/base.rb', line 241

def i18n_scope
  :activegroonga
end

+ (Object) inspect



186
187
188
189
190
191
192
193
194
195
# File 'lib/active_groonga/base.rb', line 186

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)



197
198
199
200
201
202
203
204
205
# File 'lib/active_groonga/base.rb', line 197

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)



229
230
231
232
233
# File 'lib/active_groonga/base.rb', line 229

def reference_class(column_name, klass)
  @reference_mapping ||= {}
  column_name = column_name.to_s
  @reference_mapping[column_name] = klass
end

+ (Object) select(options = {})



133
134
135
136
137
138
139
140
# File 'lib/active_groonga/base.rb', line 133

def select(options={})
  return all(options) unless block_given?
  records = table.select do |record|
    yield(record)
  end
  create_result_set(records,
                    options.merge(:expression => records.expression))
end

+ (Object) table



175
176
177
# File 'lib/active_groonga/base.rb', line 175

def table
  @table ||= context[table_name]
end

+ (Object) table_name(name = nil)



163
164
165
166
167
168
169
# File 'lib/active_groonga/base.rb', line 163

def table_name(name=nil)
  if name.nil?
    @table_name ||= model_name.plural
  else
    self.table_name = name
  end
end

+ (Object) table_name=(name)



171
172
173
# File 'lib/active_groonga/base.rb', line 171

def table_name=(name)
  @table_name = name
end

Instance Method Details

- (Object) ==(other)



330
331
332
# File 'lib/active_groonga/base.rb', line 330

def ==(other)
  other.is_a?(self.class) and other.id == id
end

- (Object) attributes



320
321
322
# File 'lib/active_groonga/base.rb', line 320

def attributes
  @attributes
end

- (Object) attributes=(attributes)



324
325
326
327
328
# File 'lib/active_groonga/base.rb', line 324

def attributes=(attributes)
  attributes.each do |key, value|
    send("#{key}=", value)
  end
end

- (Object) hash



334
335
336
# File 'lib/active_groonga/base.rb', line 334

def hash
  id.hash
end

- (Boolean) have_column?(name)

Returns:

  • (Boolean)


278
279
280
# File 'lib/active_groonga/base.rb', line 278

def have_column?(name)
  table.have_column?(name)
end

- (Object) id



282
283
284
# File 'lib/active_groonga/base.rb', line 282

def id
  @id
end

- (Object) inspect



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/active_groonga/base.rb', line 346

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



286
287
288
# File 'lib/active_groonga/base.rb', line 286

def key
  @key
end

- (Object) key=(key)

Raises:



290
291
292
293
294
# File 'lib/active_groonga/base.rb', line 290

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)



338
339
340
# File 'lib/active_groonga/base.rb', line 338

def read_attribute(name)
  @attributes[name]
end

- (Object) record_id



304
305
306
307
308
309
310
# File 'lib/active_groonga/base.rb', line 304

def record_id
  if table.support_key?
    key
  else
    id
  end
end

- (Object) record_raw_id



312
313
314
# File 'lib/active_groonga/base.rb', line 312

def record_raw_id
  id
end

- (Object) score



296
297
298
# File 'lib/active_groonga/base.rb', line 296

def score
  @score
end

- (Object) score=(score)



300
301
302
# File 'lib/active_groonga/base.rb', line 300

def score=(score)
  @score = score
end

- (Object) table



362
363
364
# File 'lib/active_groonga/base.rb', line 362

def table
  @table ||= self.class.table
end

- (Object) to_key



316
317
318
# File 'lib/active_groonga/base.rb', line 316

def to_key
  persisted? ? [record_id] : nil
end

- (Object) write_attribute(name, value)



342
343
344
# File 'lib/active_groonga/base.rb', line 342

def write_attribute(name, value)
  @attributes[name] = value
end