Class: Conreality::Database::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/conreality/database/row.rb

Overview

Encapsulates a database row.

Direct Known Subclasses

Binary, Event, Message, Object, Theater

Constant Summary collapse

SCHEMA =
Conreality::Database::SCHEMA

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Row

Returns a new instance of Row.

Parameters:


9
10
11
# File 'lib/conreality/database/row.rb', line 9

def initialize(client)
  @client = client
end

Class Method Details

.attr_accessor(attr_name)

This method returns an undefined value.

Parameters:

  • attr_name (Symbol)

    the attribute name


55
56
57
58
# File 'lib/conreality/database/row.rb', line 55

def self.attr_accessor(attr_name)
  self.attr_reader(attr_name)
  self.attr_writer(attr_name)
end

.attr_reader(attr_name)

This method returns an undefined value.

Parameters:

  • attr_name (Symbol)

    the attribute name


28
29
30
31
32
33
34
35
36
37
# File 'lib/conreality/database/row.rb', line 28

def self.attr_reader(attr_name)
  attr_var = :"@#{attr_name}"
  self.send(:define_method, attr_name) do
    if self.instance_variable_defined?(attr_var)
      self.instance_variable_get(attr_var)
    else
      self.get(attr_name)
    end
  end
end

.attr_wrapper(attr_name, class_name)

This method returns an undefined value.

Parameters:

  • attr_name (Symbol)

    the attribute name

  • class_name (Class, Symbol)

    the class name


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/conreality/database/row.rb', line 64

def self.attr_wrapper(attr_name, class_name)
  self.send(:define_method, attr_name) do
    klass = find_class(class_name)
    key = self.get(attr_name)
    key ? klass.new(@client, key) : nil
  end
  self.send(:define_method, "#{attr_name}=") do |value|
    case value
      when Integer
        self.set!(attr_name, value)
      when Database::Row
        klass = find_class(class_name)
        key_attr = klass.key_attr
        key = value.instance_variable_get(:"@#{key_attr}")
        self.set!(attr_name, key)
      else
        raise TypeError, "expected Integer or Database::Row, got #{value.inspect}"
    end
  end
end

.attr_writer(attr_name)

This method returns an undefined value.

Parameters:

  • attr_name (Symbol)

    the attribute name


42
43
44
45
46
47
48
49
50
# File 'lib/conreality/database/row.rb', line 42

def self.attr_writer(attr_name)
  attr_var = :"@#{attr_name}"
  self.send(:define_method, "#{attr_name}=") do |value|
    self.set!(attr_name, value)
    if self.instance_variable_defined?(attr_var)
      self.instance_variable_set(attr_var, value)
    end
  end
end

.key_attrSymbol

Returns:

  • (Symbol)

21
22
23
# File 'lib/conreality/database/row.rb', line 21

def self.key_attr
  self.const_get(:KEY)
end

.table_nameSymbol

Returns:

  • (Symbol)

15
16
17
# File 'lib/conreality/database/row.rb', line 15

def self.table_name
  self.const_get(:TABLE)
end

Instance Method Details

#get(name, default_value = NOTHING) ⇒ any

Fetches the value of a field.

Parameters:

  • name (Symbol)
  • default_value (String, Numeric, nil) (defaults to: NOTHING)

Returns:

  • (any)

Raises:

  • (NoSuchRow)

    if the SELECT query failed to match a row and default_value was not given


100
101
102
103
104
105
106
107
108
# File 'lib/conreality/database/row.rb', line 100

def get(name, default_value = NOTHING)
  table_name, key_attr, key = self.class.table_name, self.class.key_attr, self.key
  @client.exec_with_params("SELECT #{q(name)} FROM #{q(SCHEMA)}.#{q(table_name)} WHERE #{q(key_attr)} = $1 LIMIT 1", key) do |result|
    if result.num_tuples.zero? && default_value.equal?(NOTHING)
      raise NoSuchRow, "Failed to select row <<#{key}>> from table '#{table_name}'"
    end
    result.num_tuples.zero? ? default_value : result.getvalue(0, 0)
  end
end

#keyInteger, String

Returns the primary key for this row.

Returns:

  • (Integer, String)

89
90
91
# File 'lib/conreality/database/row.rb', line 89

def key
  self.instance_variable_get(:"@#{self.class.key_attr}").freeze
end

#set!(name, value)

This method returns an undefined value.

Updates the value of a field.

Parameters:

  • name (Symbol)
  • value (String, Numeric, nil)

Raises:

  • (NoSuchRow)

    if the UPDATE query failed to match a row


117
118
119
120
121
122
123
124
# File 'lib/conreality/database/row.rb', line 117

def set!(name, value)
  table_name, key_attr, key = self.class.table_name, self.class.key_attr, self.key
  @client.exec_with_params("UPDATE #{q(SCHEMA)}.#{q(table_name)} SET #{q(name)} = $1 WHERE #{q(key_attr)} = $2", value, key) do |result|
    if result.cmd_tuples.zero?
      raise NoSuchRow, "Failed to update row <<#{key}>> in table '#{table_name}'"
    end
  end
end