Class: ChefWorkflow::DatabaseSupport::Map

Inherits:
Set show all
Defined in:
lib/chef-workflow/support/db/basic.rb

Instance Attribute Summary

Attributes inherited from Generic

#db

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 ChefWorkflow::DatabaseSupport::Collection

Instance Method Details

#[](key) ⇒ Object



188
189
190
191
# File 'lib/chef-workflow/support/db/basic.rb', line 188

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

#[]=(key, value) ⇒ Object



193
194
195
196
197
# File 'lib/chef-workflow/support/db/basic.rb', line 193

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

#create_tableObject



210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/chef-workflow/support/db/basic.rb', line 210

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



199
200
201
202
203
# File 'lib/chef-workflow/support/db/basic.rb', line 199

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

#to_hashObject



205
206
207
208
# File 'lib/chef-workflow/support/db/basic.rb', line 205

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