Class: Palsy::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/palsy/basic/generic.rb

Overview

Palsy::Generic is the base type for Palsy types. It implements the basic things needed for Palsy to work.

Creating your own type is just a function of subclassing this and overriding the method Palsy::Generic#create_table, and adding methods you expect users to use to operate against the type. For example, this is what Palsy::Object#create_table looks like:

class Palsy::Object < Palsy::Generic
  def create_table
    @db.execute_t %Q[
      create table if not exists #{@table_name} (
        id integer not null primary key autoincrement,
        key varchar(255) not null,
        value text not null,
        UNIQUE(key)
      )
    ]
  end
end

Direct Known Subclasses

Collection, Object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, object_name = nil) ⇒ Generic

This is the base constructor for all Palsy types and should always be run in subclasses before any action is taken by the class’s initializer.



39
40
41
42
43
44
45
46
# File 'lib/palsy/basic/generic.rb', line 39

def initialize(table_name, object_name=nil)
  raise "a table_name must be provided!" unless table_name

  @table_name   = table_name
  @object_name  = object_name
  post_marshal_init
  create_table
end

Instance Attribute Details

#dbObject

The instance of Palsy. Overwriting this is probably not a good idea.



28
29
30
# File 'lib/palsy/basic/generic.rb', line 28

def db
  @db
end

#object_nameObject (readonly)

The name of the object this object is bound to inside the table. May be nil for certain types.



33
34
35
# File 'lib/palsy/basic/generic.rb', line 33

def object_name
  @object_name
end

#table_nameObject (readonly)

The name of the table this object is bound to.



30
31
32
# File 'lib/palsy/basic/generic.rb', line 30

def table_name
  @table_name
end

Class Method Details

._load(value) ⇒ Object

Marshal helper to load objects.



51
52
53
54
# File 'lib/palsy/basic/generic.rb', line 51

def self._load(value)
  obj = self.new(*Marshal.load(value))
  return obj
end

Instance Method Details

#==(other) ⇒ Object

Equality method for comparing Palsy objects.



85
86
87
# File 'lib/palsy/basic/generic.rb', line 85

def ==(other)
  [:db, :table_name, :object_name].all? { |x| self.send(x) == other.send(x) }
end

#_dump(level) ⇒ Object

Marshal helper to dump objects. Only the table and object names are preserved.



60
61
62
63
64
65
# File 'lib/palsy/basic/generic.rb', line 60

def _dump(level)
  self.db = nil
  res = Marshal.dump([@table_name, @object_name])
  post_marshal_init
  return res
end

#create_tableObject

Virtual method to define the create_table interface. Just raises, intended to be overridden.



78
79
80
# File 'lib/palsy/basic/generic.rb', line 78

def create_table
  raise "Do not use the Generic type directly!"
end

#post_marshal_initObject

Helper to manage the database instance for Marshal operations.



70
71
72
# File 'lib/palsy/basic/generic.rb', line 70

def post_marshal_init
  @db = Palsy.instance
end