Class: Palsy::Map

Inherits:
Set show all
Defined in:
lib/palsy/basic/map.rb

Overview

Palsy::Map is an emulation of Ruby’s Hash class, where objects are unordered and unique by the keyed value. They are a Palsy::Set and Palsy::Collection and have Enumerable support. Take a look at Palsy::Object which is similar but semantically different in a few ways.

Palsy::Map keys and values must be marshallable, and their uniqueness for sanity’s sake must also be enforced in their marshalled output.

Here’s an example:

obj = Palsy::Map.new("some_maps", "a_specific_map")
obj[1] = 2
obj[1] == 2 #=> true

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

Another example:

obj1 = Palsy::Map.new("some_maps", "a_specific_map")
obj2 = Palsy::Map.new("some_maps", "a_specific_map")
obj3 = Palsy::Map.new("some_maps", "a_different_map")

obj1 == obj2 #=> true
obj1[1] = 2
obj2[1] == 2 #=> also true
obj3 == obj1 #=> false (different map keys)
obj3[1] = 3
obj2[1] == 3 #=> also false

Instance Attribute Summary

Attributes inherited from Generic

#db, #object_name, #table_name

Instance Method Summary collapse

Methods inherited from Set

#add, #clear, #delete, #has_key?, #keys, #replace

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

#[](key) ⇒ Object

Obtain the value for a given key. Returns nil if the key does not have an entry.



42
43
44
45
# File 'lib/palsy/basic/map.rb', line 42

def [](key)
  value = @db.execute("select value from #{@table_name} where name=? and key=?", [@object_name, Marshal.dump(key)]).first.first rescue nil
  return value && Marshal.load(value)
end

#[]=(key, value) ⇒ Object

Associate a value with a given key. Keys will be deleted from the database before attempting to write the new pair.



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

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

#create_tableObject

Defines the schema for Maps.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/palsy/basic/map.rb', line 78

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,
      key varchar(255) not null,
      value text not null,
      UNIQUE(name, key)
    )
  EOF

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

#eachObject

Successively yields key and value for each item in the map. Modifications to either the key or value yielded will not persist.



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

def each
  keys.each do |key|
    yield key, self[key]
  end
end

#to_hashObject

Convert this map to a Ruby Hash object.



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

def to_hash
  rows = @db.execute("select key, value from #{@table_name} where name=?", [@object_name])
  Hash[rows.map { |x| x.map { |y| Marshal.load(y) } }]
end