Module: Rhubarb::Persisting

Defined in:
lib/rhubarb/persisting.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(other) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rhubarb/persisting.rb', line 16

def self.included(other)
  class << other
    include PersistingClassMixins
  end

  other.class_eval do
    attr_reader :row_id
    attr_reader :created
    attr_reader :updated
  end
  
  other.instance_eval do
    private_class_method :ensure_accessors
  end
end

Instance Method Details

#==(other) ⇒ Object



47
48
49
50
# File 'lib/rhubarb/persisting.rb', line 47

def ==(other)
  freshen
  self.class == other.class && other.row_id == self.row_id
end

#dbObject



32
33
34
# File 'lib/rhubarb/persisting.rb', line 32

def db
  self.class.db
end

#deleteObject

Deletes the row corresponding to this object from the database; invalidates =self= and any other objects backed by this row



73
74
75
76
77
78
# File 'lib/rhubarb/persisting.rb', line 73

def delete
  db.do_query("delete from #{self.class.quoted_table_name} where row_id = ?", @row_id)
  mark_dirty
  @tuple = nil
  @row_id = nil
end

#deleted?Boolean

Returns true if the row backing this object has been deleted from the database

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/rhubarb/persisting.rb', line 37

def deleted?
  freshen
  not @tuple
end

#hashObject



42
43
44
45
# File 'lib/rhubarb/persisting.rb', line 42

def hash
  freshen
  @row_id ^ self.class.table_name.hash
end

#initialize(tup) ⇒ Object

Initializes a new instance backed by a tuple of values. Do not call this directly. Create new instances with the create or find methods.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rhubarb/persisting.rb', line 54

def initialize(tup)
  @backed = true
  @tuple = tup
  mark_fresh
  
  unless @tuple.is_a?(Hash)
    @tuple = Hash[*self.class.columns.map{|c| c.name}.zip(tup).flatten]
  end
  
  @row_id = @tuple["row_id"]
  @created = @tuple["created"]
  @updated = @tuple["updated"]
  resolve_referents
  self.class.dirtied[@row_id] ||= @expired_after
  self
end

#to_hashObject



80
81
82
83
84
85
86
# File 'lib/rhubarb/persisting.rb', line 80

def to_hash
  result = {}
  @tuple.each_pair do |key, value|
    result[key.to_sym] = value unless key.class == Fixnum
  end
  result
end