module ActiveGroonga::Persistence

Public Instance Methods

becomes(klass) click to toggle source
# File lib/active_groonga/persistence.rb, line 50
def becomes(klass)
  became = klass.new
  became.instance_variable_set("@attributes", @attributes)
  became.instance_variable_set("@attributes_cache", @attributes_cache)
  became.instance_variable_set("@new_record", new_record?)
  became.instance_variable_set("@destroyed", destroyed?)
  became
end
delete() click to toggle source
# File lib/active_groonga/persistence.rb, line 38
def delete
  table.delete(record_id) if persisted?
  @destroyed = true
  freeze
end
destroy() click to toggle source
# File lib/active_groonga/persistence.rb, line 44
def destroy
  table.delete(record_id) if persisted?
  @destroyed = true
  freeze
end
destroyed?() click to toggle source
# File lib/active_groonga/persistence.rb, line 22
def destroyed?
  @destroyed
end
new_record?() click to toggle source
# File lib/active_groonga/persistence.rb, line 18
def new_record?
  @new_record
end
persisted?() click to toggle source
# File lib/active_groonga/persistence.rb, line 26
def persisted?
  !new_record? and !destroyed?
end
reload() click to toggle source
# File lib/active_groonga/persistence.rb, line 75
def reload
  if new_record?
    record = nil
  else
    record = table[record_id]
  end
  reload_attributes(record)
  @attributes_cache = {}
  self
end
save(options={}) click to toggle source
# File lib/active_groonga/persistence.rb, line 30
def save(options={})
  create_or_update
end
save!(options={}) click to toggle source
# File lib/active_groonga/persistence.rb, line 34
def save!(options={})
  create_or_update or raise(RecordNotSaved)
end
update_attribute(name, value) click to toggle source
# File lib/active_groonga/persistence.rb, line 59
def update_attribute(name, value)
  name = name.to_s
  send("#{name}=", value)
  save(:validate => false)
end
update_attributes(attributes) click to toggle source
# File lib/active_groonga/persistence.rb, line 65
def update_attributes(attributes)
  self.attributes = attributes
  save
end
update_attributes!(attributes) click to toggle source
# File lib/active_groonga/persistence.rb, line 70
def update_attributes!(attributes)
  self.attributes = attributes
  save!
end

Private Instance Methods

create() click to toggle source
# File lib/active_groonga/persistence.rb, line 92
def create
  attributes = {}
  @attributes.each do |key, value|
    next if value.nil?
    if value.is_a?(Base)
      value.save if value.new_record?
      value = value.record_id
    end
    attributes[key] = value
  end
  if table.support_key?
    record = table.add(key, attributes)
  else
    record = table.add(attributes)
  end
  current_time = Time.now
  record["created_at"] = current_time if record.have_column?("created_at")
  record["updated_at"] = current_time if record.have_column?("updated_at")
  reload_attributes(record)
  @id = record.id
  @key = record.key if record.support_key?
  @new_record = false
  true
end
create_or_update() click to toggle source
# File lib/active_groonga/persistence.rb, line 87
def create_or_update
  success = new_record? ? create : update
  success != false
end
extract_attributes(record) click to toggle source
# File lib/active_groonga/persistence.rb, line 140
def extract_attributes(record)
  attributes = {}
  if record.support_key?
    attributes["key"] = record.key
  else
    attributes["id"] = record.id
  end
  attributes["score"] = record.score if record.support_score?
  record.columns.each do |column|
    next if column.is_a?(Groonga::IndexColumn)
    value = record[column.local_name]
    if value and column.reference?
      value_class = self.class.custom_reference_class(column.local_name)
      value_class ||= column.range.name.camelize.singularize.constantize
      if column.vector?
        value = Vector.new(self, value_class, value)
      else
        value = value_class.instantiate(value)
      end
    end
    attributes[column.local_name] = value
  end
  attributes
end
initial_attributes() click to toggle source
# File lib/active_groonga/persistence.rb, line 131
def initial_attributes
  attributes = {}
  self.class.table.columns.each do |column|
    next if column.index?
    attributes[column.local_name] = nil
  end
  attributes
end
reload_attributes(record=nil) click to toggle source
# File lib/active_groonga/persistence.rb, line 165
def reload_attributes(record=nil)
  if record.nil?
    @attributes = initial_attributes
  else
    @attributes = extract_attributes(record)
  end
  @id = @attributes.delete("id")
  @key = @attributes.delete("key")
  @score = @attributes.delete("score")
end
update() click to toggle source
# File lib/active_groonga/persistence.rb, line 117
def update
  record = self.class.table[record_id]
  @attributes.each do |key, value|
    if value.respond_to?(:record_id)
      value = value.record_id
    elsif value.is_a?(Hash) and value["id"]
      value = value["id"]
    end
    record[key] = value
  end
  record["updated_at"] = Time.now if record.have_column?("updated_at")
  true
end