Class: ChefWorkflow::DatabaseSupport::List

Inherits:
Collection 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 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

#clearObject



108
109
110
# File 'lib/chef-workflow/support/db/basic.rb', line 108

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

#create_tableObject



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/chef-workflow/support/db/basic.rb', line 123

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



112
113
114
# File 'lib/chef-workflow/support/db/basic.rb', line 112

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

#popObject



93
94
95
# File 'lib/chef-workflow/support/db/basic.rb', line 93

def pop
  to_a.pop
end

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



76
77
78
79
80
81
# File 'lib/chef-workflow/support/db/basic.rb', line 76

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

#replace(ary) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/chef-workflow/support/db/basic.rb', line 97

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



89
90
91
# File 'lib/chef-workflow/support/db/basic.rb', line 89

def shift
  to_a.shift
end

#to_aObject



116
117
118
119
120
121
# File 'lib/chef-workflow/support/db/basic.rb', line 116

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



85
86
87
# File 'lib/chef-workflow/support/db/basic.rb', line 85

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