Class: ChefWorkflow::DatabaseSupport::VMGroup
- Inherits:
-
Object
- Object
- ChefWorkflow::DatabaseSupport::VMGroup
- Includes:
- Enumerable
- Defined in:
- lib/chef-workflow/support/db/group.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #each ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(table_name, box_nil) ⇒ VMGroup
constructor
A new instance of VMGroup.
- #keys ⇒ Object
Constructor Details
#initialize(table_name, box_nil) ⇒ VMGroup
Returns a new instance of VMGroup.
8 9 10 11 12 13 14 |
# File 'lib/chef-workflow/support/db/group.rb', line 8 def initialize(table_name, box_nil) raise "Must provide a table name!" unless table_name @table_name = table_name @box_nil = box_nil @db = ChefWorkflow::DatabaseSupport.instance create_table end |
Instance Method Details
#[](key) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/chef-workflow/support/db/group.rb', line 16 def [](key) rows = @db.execute("select value from #{@table_name} where name=? order by id", [key]) if rows.count == 0 @box_nil ? [] : nil else rows.to_a.map { |x| Marshal.load(x.first) } end end |
#[]=(key, value) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/chef-workflow/support/db/group.rb', line 25 def []=(key, value) delete(key) return value if value.empty? values = value.map { |x| Marshal.dump(x) } value_string = ("(?, ?)," * values.count).chop @db.execute("insert into #{@table_name} (name, value) values #{value_string}", values.map { |x| [key, x] }.flatten) end |
#delete(key) ⇒ Object
40 41 42 |
# File 'lib/chef-workflow/support/db/group.rb', line 40 def delete(key) @db.execute("delete from #{@table_name} where name=?", [key]) end |
#each ⇒ Object
48 49 50 51 52 53 |
# File 'lib/chef-workflow/support/db/group.rb', line 48 def each # XXX very slow, but fuck it keys.each do |key| yield key, self[key] end end |
#has_key?(key) ⇒ Boolean
44 45 46 |
# File 'lib/chef-workflow/support/db/group.rb', line 44 def has_key?(key) @db.execute("select count(*) from #{@table_name} where name=?", [key]).first.first.to_i > 0 end |
#keys ⇒ Object
36 37 38 |
# File 'lib/chef-workflow/support/db/group.rb', line 36 def keys @db.execute("select distinct name from #{@table_name}").map(&:first) end |