Class: ActiveGroonga::Base

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

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!

Class Attribute Details

.limitInteger

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

.sort_keysArray<Array<String, Symbol>>

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

.==(other) ⇒ Object



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

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

.all(options = {}) ⇒ Object



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

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

.attributesObject



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

def attributes
  @attributes
end

.attributes=(attributes) ⇒ Object



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

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

.configure(configuration) ⇒ Object



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

.contextObject



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

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

.countObject



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

def count
  table.size
end

.create(attributes = nil, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# 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)
    yield(object) if block_given?
    object.save
    object
  end
end

.custom_reference_class(column_name) ⇒ Object



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

.databaseObject



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

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

.database_path=(path) ⇒ Object



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

.define_column_accessorsObject



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

.define_method_attribute(name) ⇒ Object



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

.define_method_attribute=(name) ⇒ Object



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

.encoding=(new_encoding) ⇒ Object



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

.exists?(record_id) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

.hashObject



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

def hash
  id.hash
end

.have_column?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.i18n_scopeObject



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

def i18n_scope
  :activegroonga
end

.idObject



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

def id
  @id
end

.initialize(record_or_attributes = nil) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# 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
end

.inspectObject



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

.instantiate(record) ⇒ Object



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

.keyObject



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

def key
  @key
end

.key=(key) ⇒ Object

Raises:



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

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



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

def read_attribute(name)
  @attributes[name]
end

.record_idObject



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

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

.record_raw_idObject



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

def record_raw_id
  id
end

.reference_class(column_name, klass) ⇒ Object



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

.scoreObject



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

def score
  @score
end

.score=(score) ⇒ Object



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

def score=(score)
  @score = score
end

.select(options = {}) ⇒ Object



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

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

.tableObject



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

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

.table_name(name = nil) ⇒ Object



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

.table_name=(name) ⇒ Object



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

def table_name=(name)
  @table_name = name
end

.to_keyObject



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

def to_key
  persisted? ? [record_id] : nil
end

.write_attribute(name, value) ⇒ Object



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

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