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.



107
108
109
# File 'lib/palsy/basic/list.rb', line 107

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

#create_tableObject

Define the schema for this type.



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/palsy/basic/list.rb', line 132

def create_table
  @db.execute_t <<-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_t "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.



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

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

#mutate(meth) ⇒ Object

Helper method for mutators.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/palsy/basic/list.rb', line 60

def mutate(meth)
  val = nil

  @db.with_t do
    a = to_a
    val = a.send(meth)
    replace(a)
  end

  return val
end

#popObject

Returns the tail of the list. Destructive.



82
83
84
# File 'lib/palsy/basic/list.rb', line 82

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_t(
    "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.



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/palsy/basic/list.rb', line 90

def replace(ary)
  @db.with_t do
    clear
    return if ary.count == 0

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

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

#shiftObject

Returns the head of the list. Destructive.



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

def shift
  mutate(:shift)
end

#to_aObject

Convert the list to a Ruby Array.



122
123
124
125
126
127
# File 'lib/palsy/basic/list.rb', line 122

def to_a
  @db.execute_t(
    "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
54
55
# File 'lib/palsy/basic/list.rb', line 51

def unshift(val)
  @db.with_t do
    replace([val] + to_a)
  end
end