Class: Palsy::List

Inherits:
Collection show all
Defined in:
lib/palsy/basic/list.rb

Overview

Palsy::List is a kind of Palsy::Collection and implements List semantics similar to Ruby’s Array class. For more random-access behavior that Array provides, convert to a Ruby array with Palsy::List#to_a.

All values of the list must be capable of being Marshalled.

Here’s an example:

obj = Palsy::List.new("some_lists", "a_specific_list")
obj.push 2
obj.pop == 2 #=> true

This will create a table called “some_lists” and all i/o will be directed to that table with an additional key of “a_specific_list”. This allows you to coordinate multiple lists in a single table.

Another example:

obj1 = Palsy::List.new("some_lists", "a_specific_list")
obj2 = Palsy::List.new("some_lists", "a_specific_list")
obj3 = Palsy::List.new("some_lists", "a_different_list")

obj1 == obj2 #=> true
obj1.push 2
obj2.pop == 2 #=> also true
obj3 == obj1 #=> false (different list keys)
obj.push 2
obj3.push 3
obj2.pop == 3 #=> also false

Instance Attribute Summary

Attributes inherited from Generic

#db, #object_name, #table_name

Instance Method Summary collapse

Methods inherited from Collection

#initialize

Methods inherited from Generic

#==, #_dump, _load, #initialize, #post_marshal_init

Constructor Details

This class inherits a constructor from Palsy::Collection

Instance Method Details

#clearObject

Empty the list.



97
98
99
# File 'lib/palsy/basic/list.rb', line 97

def clear
  @db.execute("delete from #{@table_name} where name=?", [@object_name])
end

#create_tableObject

Define the schema for this type.



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/palsy/basic/list.rb', line 122

def create_table
  @db.execute <<-EOF
    create table if not exists #{@table_name} (
      id integer not null primary key autoincrement,
      name varchar(255) not null,
      value text not null
    )
  EOF

  @db.execute "create index if not exists #{@table_name}_name_index on #{@table_name} (name)"
end

#eachObject

Yield each item in the list successively. Note that changes made to the items yielded will not persist, unless they are Palsy types themselves.



105
106
107
# File 'lib/palsy/basic/list.rb', line 105

def each
  to_a.each { |x| yield x }
end

#mutate(meth) ⇒ Object

Helper method for mutators.



58
59
60
61
62
63
# File 'lib/palsy/basic/list.rb', line 58

def mutate(meth)
  a = to_a
  val = a.send(meth)
  replace(a)
  return val
end

#popObject

Returns the tail of the list. Destructive.



75
76
77
# File 'lib/palsy/basic/list.rb', line 75

def pop
  mutate(:pop)
end

#push(val) ⇒ Object Also known as: <<

Add a value to the tail of the list.



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

def push(val)
  @db.execute(
    "insert into #{@table_name} (name, value) values (?, ?)",
    [@object_name, Marshal.dump(val)]
  )
end

#replace(ary) ⇒ Object

Replace the list with the argument, which should be something that acts like an Enumerable.



83
84
85
86
87
88
89
90
91
92
# File 'lib/palsy/basic/list.rb', line 83

def replace(ary)
  clear

  value_string = ("(?, ?)," * ary.count).chop

  @db.execute(
      "insert into #{@table_name} (name, value) values #{value_string}",
      ary.map { |x| [@object_name, Marshal.dump(x)] }.flatten
  )
end

#shiftObject

Returns the head of the list. Destructive.



68
69
70
# File 'lib/palsy/basic/list.rb', line 68

def shift
  mutate(:shift)
end

#to_aObject

Convert the list to a Ruby Array.



112
113
114
115
116
117
# File 'lib/palsy/basic/list.rb', line 112

def to_a
  @db.execute(
    "select value from #{@table_name} where name=? order by id",
    [@object_name]
  ).map { |x| Marshal.load(x.first) }
end

#unshift(val) ⇒ Object

Add a value to the head of the list.



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

def unshift(val)
  replace([val] + to_a)
end