class ActiveGroonga::Base

Public Class Methods

all(options={}) click to toggle source
# File lib/active_groonga/base.rb, line 142
def all(options={})
  create_result_set(table)
end
configure(configuration) click to toggle source
# 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
context() click to toggle source
# File lib/active_groonga/base.rb, line 150
def context
  @@context ||= Groonga::Context.default
end
count() click to toggle source
# File lib/active_groonga/base.rb, line 146
def count
  table.size
end
create(attributes=nil, &block) click to toggle source
# 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
custom_reference_class(column_name) click to toggle source
# 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
database() click to toggle source
# File lib/active_groonga/base.rb, line 88
def database
  @@database ||= Database.new(database_path)
end
database_path=(path) click to toggle source
# 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_accessors() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
find(record_id, options={}) click to toggle source
# 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
i18n_scope() click to toggle source
# File lib/active_groonga/base.rb, line 241
def i18n_scope
  :activegroonga
end
inspect() click to toggle source
Calls superclass method
# 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) click to toggle source
# 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
new(record_or_attributes=nil) { |self| ... } click to toggle source
# 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
reference_class(column_name, klass) click to toggle source
# 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
select(options={}) { |record| ... } click to toggle source
# 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
table() click to toggle source
# File lib/active_groonga/base.rb, line 175
def table
  @table ||= context[table_name]
end
table_name(name=nil) click to toggle source
# 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) click to toggle source
# File lib/active_groonga/base.rb, line 171
def table_name=(name)
  @table_name = name
end

Protected Class Methods

instance_method_already_implemented?(method_name) click to toggle source
Calls superclass method
# File lib/active_groonga/base.rb, line 246
def instance_method_already_implemented?(method_name)
  super(method_name)
end

Private Class Methods

create_result_set(records, options={}) click to toggle source
# File lib/active_groonga/base.rb, line 251
def create_result_set(records, options={})
  default_options = {
    :default_sort_keys => sort_keys,
    :default_limit => limit,
  }
  ResultSet.new(records, self, default_options.merge(options))
end

Public Instance Methods

==(other) click to toggle source
# File lib/active_groonga/base.rb, line 330
def ==(other)
  other.is_a?(self.class) and other.id == id
end
attributes() click to toggle source
# File lib/active_groonga/base.rb, line 320
def attributes
  @attributes
end
attributes=(attributes) click to toggle source
# File lib/active_groonga/base.rb, line 324
def attributes=(attributes)
  attributes.each do |key, value|
    send("#{key}=", value)
  end
end
hash() click to toggle source
# File lib/active_groonga/base.rb, line 334
def hash
  id.hash
end
have_column?(name) click to toggle source
# File lib/active_groonga/base.rb, line 278
def have_column?(name)
  table.have_column?(name)
end
id() click to toggle source
# File lib/active_groonga/base.rb, line 282
def id
  @id
end
inspect() click to toggle source
# 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
key() click to toggle source
# File lib/active_groonga/base.rb, line 286
def key
  @key
end
key=(key) click to toggle source
# 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
read_attribute(name) click to toggle source
# File lib/active_groonga/base.rb, line 338
def read_attribute(name)
  @attributes[name]
end
record_id() click to toggle source
# File lib/active_groonga/base.rb, line 304
def record_id
  if table.support_key?
    key
  else
    id
  end
end
record_raw_id() click to toggle source
# File lib/active_groonga/base.rb, line 312
def record_raw_id
  id
end
score() click to toggle source
# File lib/active_groonga/base.rb, line 296
def score
  @score
end
score=(score) click to toggle source
# File lib/active_groonga/base.rb, line 300
def score=(score)
  @score = score
end
table() click to toggle source
# File lib/active_groonga/base.rb, line 362
def table
  @table ||= self.class.table
end
to_key() click to toggle source
# File lib/active_groonga/base.rb, line 316
def to_key
  persisted? ? [record_id] : nil
end
write_attribute(name, value) click to toggle source
# File lib/active_groonga/base.rb, line 342
def write_attribute(name, value)
  @attributes[name] = value
end

Private Instance Methods

attribute(name) click to toggle source
# File lib/active_groonga/base.rb, line 367
def attribute(name)
  read_attribute(name)
end
attribute=(name, value) click to toggle source
# File lib/active_groonga/base.rb, line 371
def attribute=(name, value)
  write_attribute(name, value)
end