Class: RBase::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/rbase/record.rb

Overview

Class that contains data for particular table row. Should not be created explicitly (use Table#create to create records)

Accessing attributes

You can read and assign values to row’s columns using simple property syntax:

user = users_table[0]
user.name = 'Bob'
user.birth_date = Date.new(1980, 2, 29)
user.save

puts user.name
puts user.birth_date

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, attributes = {}) ⇒ Record

Returns a new instance of Record.



41
42
43
44
45
46
47
# File 'lib/rbase/record.rb', line 41

def initialize(table, attributes = {})
  @table = table
  @values_cached = {}
  @values_changed = {}

  attributes.each { |k, v| @values_changed[k.to_s.upcase] = v }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/rbase/record.rb', line 89

def method_missing(sym, *args)
  name = sym.to_s
  if /=$/ =~ name && args.size == 1
    set_value(name[0..-2], args.first)
  else
    get_value(name)
  end
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



39
40
41
# File 'lib/rbase/record.rb', line 39

def index
  @index
end

#tableObject (readonly)

Returns the value of attribute table.



39
40
41
# File 'lib/rbase/record.rb', line 39

def table
  @table
end

Instance Method Details

#cloneObject

Clone record.



82
83
84
85
86
87
# File 'lib/rbase/record.rb', line 82

def clone
  c = self.class.new(@table, @values_changed)
  c.instance_variable_set("@values_cached", @values_cached)
  c.instance_variable_set("@data", @data)
  c
end

#deleteObject

Delete record from database.



71
72
73
74
# File 'lib/rbase/record.rb', line 71

def delete
  @deleted = true
  save
end

#deleted?Boolean

Returns true if record was marked as deleted; otherwise return false.

Returns:

  • (Boolean)


77
78
79
# File 'lib/rbase/record.rb', line 77

def deleted?
  @deleted ||= new_record? ? false : @data[0, 1] == '*'
end

#new_record?Boolean

Returns true if record was never saved to database; otherwise return false.

Returns:

  • (Boolean)


60
61
62
# File 'lib/rbase/record.rb', line 60

def new_record?
  @data.nil?
end

#saveObject

Save record to database.



65
66
67
68
# File 'lib/rbase/record.rb', line 65

def save
  record = self
  @table.instance_eval { save(record) }
end

#serializeObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rbase/record.rb', line 98

def serialize
  if new_record?
    @data = deleted? ? '*' : ' '
    @data << @table.columns.collect do |column|
      column.pack(@values_changed[column.name])
    end.join
  else
    @data[0, 1] = deleted? ? '*' : ' '
    @values_changed.each do |k, v|
      column = @table.column(k)
      raise UnknownColumnError.new(k) unless column
      begin
        @data[column.offset, column.size] = column.pack(v)
      rescue Object => e
        raise InvalidValueError.new(column, v)
      end
      @values_cached[k] = v
    end
  end
  @data
end