class Reorm::Model

Attributes

errors[R]

Public Class Methods

all() click to toggle source
# File lib/reorm/model.rb, line 173
def self.all
  Cursor.new(self, r.table(table_name))
end
create(properties={}) click to toggle source
# File lib/reorm/model.rb, line 167
def self.create(properties={})
  object = self.new(properties)
  object.save
  object
end
filter(predicate=nil, &block) click to toggle source
# File lib/reorm/model.rb, line 177
def self.filter(predicate=nil, &block)
  if predicate.nil?
    Cursor.new(self, r.table(table_name).filter(&block))
  else
    Cursor.new(self, r.table(table_name).filter(predicate))
  end
end
get(id) click to toggle source
# File lib/reorm/model.rb, line 153
def self.get(id)
  model = nil
  Reorm.connection do |connection|
    if table_exists?(table_name, connection)
      properties = r.table(table_name).get(id).run(connection)
      if !properties.nil?
        model = self.new
        model.assign_properties(properties)
      end
    end
  end
  model
end
new(properties={}) click to toggle source
# File lib/reorm/model.rb, line 14
def initialize(properties={})
  @properties = {}
  @errors     = PropertyErrors.new
  properties.each {|key, value| set_property(key, value)}
end

Private Class Methods

table_exists?(name, connection) click to toggle source
# File lib/reorm/model.rb, line 205
def self.table_exists?(name, connection)
  r.table_list.run(connection).include?(name)
end

Public Instance Methods

[](property_name) click to toggle source
# File lib/reorm/model.rb, line 97
def [](property_name)
  @properties[property_name.to_sym]
end
[]=(property_name, value) click to toggle source
# File lib/reorm/model.rb, line 101
def []=(property_name, value)
  @properties[property_name.to_sym] = value
  value
end
assign_properties(properties={}) click to toggle source
# File lib/reorm/model.rb, line 126
def assign_properties(properties={})
  properties.each do |key, value|
    @properties[key.to_sym] = value
  end
  self
end
delete() click to toggle source
# File lib/reorm/model.rb, line 81
def delete
  key = get_property(primary_key)
  if key
    Reorm.connection do |connection|
      fire_events(events: [:before_delete])
      result = r.table(table_name).get(key).delete.run(connection)
      if result["deleted"] != 1
        raise Error, "Deletion of record for a #{self.class.name} class instance with a primary key of #{key} failed."
      end
      fire_events(events: [:after_delete])
      set_property(primary_key, nil)
    end
  end
  self
end
get_property(property) click to toggle source
# File lib/reorm/model.rb, line 110
def get_property(property)
  has_property?(property) ? self.__send__(property_name(property)) : nil
end
has_property?(property) click to toggle source
# File lib/reorm/model.rb, line 106
def has_property?(property)
  @properties.include?(property_name(property))
end
include?(field) click to toggle source
# File lib/reorm/model.rb, line 33
def include?(field)
  @properties.include?(property_name(field))
end
method_missing(method_name, *arguments, &block) click to toggle source
Calls superclass method
# File lib/reorm/model.rb, line 137
def method_missing(method_name, *arguments, &block)
  if method_name.to_s[-1,1] != "="
    if @properties.include?(property_name(method_name))
      @properties[method_name]
    else
      super
    end
  else
    @properties[property_name(method_name)] = arguments.first
  end
end
respond_to?(method_name, include_private=false) click to toggle source
Calls superclass method
# File lib/reorm/model.rb, line 133
def respond_to?(method_name, include_private=false)
  method_name.to_s[-1, 1] == "=" || @properties.include?(property_name(method_name)) || super
end
save(validated=true) click to toggle source
# File lib/reorm/model.rb, line 37
def save(validated=true)
  if validated && !valid?
    raise Error, "Validation error encountered saving an instance of the #{self.class.name} class."
  end

  action_type = (@properties[primary_key] ? :update : :create)
  if action_type == :create
    fire_events(events: [:before_create, :before_save])
  else
    fire_events(events: [:before_update, :before_save])
  end

  Reorm.connection do |connection|
    ensure_table_exists(connection)
    if !@properties.include?(primary_key)
      result = r.table(table_name).insert(self.to_h, return_changes: true).run(connection)
      if !result["inserted"] || result["inserted"] != 1
        raise Error, "Creation of database record for an instance of the #{self.class.name} class failed."
      end
      @properties[primary_key] = result["generated_keys"].first
    else
      result = r.table(table_name).update(self.to_h).run(connection)
      if !result["replaced"] || !result["replaced"] == 1
        raise Error, "Update of database record for an instance of the #{self.class.name} class failed."
      end
    end
  end

  if action_type == :create
    fire_events(events: [:after_save, :after_create])
  else
    fire_events(events: [:after_save, :after_update])
  end
  self
end
set_properties(settings={}) click to toggle source
# File lib/reorm/model.rb, line 119
def set_properties(settings={})
  settings.each do |property, value|
    set_property(property, value)
  end
  self
end
set_property(property, value) click to toggle source
# File lib/reorm/model.rb, line 114
def set_property(property, value)
  self.__send__(setter_name(property), value)
  self
end
to_h() click to toggle source
# File lib/reorm/model.rb, line 149
def to_h
  {}.merge(@properties)
end
update(properties={}) click to toggle source
# File lib/reorm/model.rb, line 73
def update(properties={})
  properties.each do |property, value|
    set_property(property, value)
  end
  self.save if !properties.empty?
  self
end
valid?() click to toggle source
# File lib/reorm/model.rb, line 21
def valid?
  validate
  @errors.clear?
end
validate() click to toggle source
# File lib/reorm/model.rb, line 26
def validate
  fire_events(events: [:before_validate])
  @errors.reset
  fire_events(events: [:after_validate])
  self
end

Private Instance Methods

ensure_table_exists(connection) click to toggle source
# File lib/reorm/model.rb, line 195
def ensure_table_exists(connection)
  if !table_exists?(table_name, connection)
    r.table_create(table_name, primary_key: primary_key).run(connection)
  end
end
property_name(name) click to toggle source
# File lib/reorm/model.rb, line 187
def property_name(name)
  name.to_s[-1,1] == "=" ? name.to_s[0...-1].to_sym : name
end
setter_name(name) click to toggle source
# File lib/reorm/model.rb, line 191
def setter_name(name)
  "#{name}=".to_sym
end
table_exists?(name, connection) click to toggle source
# File lib/reorm/model.rb, line 201
def table_exists?(name, connection)
  Model.table_exists?(name, connection)
end