Class: DBStruct::BogoHash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/internal/bogohash.rb,
lib/dbstruct.rb

Overview

BogoHash provides a Hash-like interface to the database table corresponding to a DBStruct subclass.

You can think if it as a Hash that holds all instances of a particular DBStruct subclass keyed on the objects’ rowid attributes. Most of the Hash interface is available (including Enumerable) with the exception of []=. delete and delete_if work as expected, however.

You should never explicitly create a BogoHash. Instead, use the class’s items or where methods.

Since these methods can be used to narrow the range of items, a BogoHash does not necessarily encompass all instances of its associated DBStruct subclass. Rows that do not match the selection criteria are not available even when explicitly addressed by row ID.

Under the hood, a BogoHash is a wrapper around a Sequel::Dataset.

Instance Method Summary collapse

Instance Method Details

#[](rowid) ⇒ Object

Retrieve the item at rowid or nil if its not present.



44
45
46
47
48
# File 'lib/internal/bogohash.rb', line 44

def [](rowid)
  row = @dataset.first(_id: rowid)
  return nil unless row
  return @dbs_klass.new(_id: rowid)
end

#clearObject

Delete all entries



118
119
120
121
# File 'lib/internal/bogohash.rb', line 118

def clear
  @dataset.delete
  return self
end

#delete(rowid, &block) ⇒ DBStruct.with(...)

Delete rowid and its associated value from the underlying table (and self by extension). If rowid is not present, does nothing.

Note that the deleted row object will be returned but can no longer be used. In particular, it does not hold the contents of the deleted row.

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/internal/bogohash.rb', line 89

def delete(rowid, &block)
  transaction {
    unless has_key?(rowid)
      return block.value if block
      return nil
    end

    val = self[rowid]
    @dataset.where(_id: rowid).delete
    return val
  }
end

#delete_if {|value| ... } ⇒ Object Also known as: reject!

Evaluate the block on each key-value pair in self end delete each entry for which the block returns true.

Like all enumeration operations, the tests and deletions occur in a single transaction.

Yields:

  • (value)

    The block to evaluate



109
110
111
112
113
114
# File 'lib/internal/bogohash.rb', line 109

def delete_if(&block)
  transaction {
    self.each{ |k, v| block.call(k,v) and delete(k) }
  }
  return self
end

#each(&block) ⇒ Object Also known as: each_pair

Evaluate block over each key-and-value pair (i.e. row and row-id). If no block is given, returns an Enumerator instead.

It is safe to perform other database operations inside the block or enumeration loop. This includes modifications to the database being enumerated, although whether or not added or removed items are still visited is undefined.

If called with a block, the entire operation happens within a single transaction.

Since this class imports Enumerable, all of its functionality is also available.



142
# File 'lib/internal/bogohash.rb', line 142

def each(&block) = each_backend(:basic_each, block)

#each_key(&block) ⇒ Object

Like #each but only yields the key (i.e. row ID).



146
# File 'lib/internal/bogohash.rb', line 146

def each_key(&block) = each_backend(:basic_each_key, block)

#each_value(&block) ⇒ Object

Like #each but only yields the value.



149
# File 'lib/internal/bogohash.rb', line 149

def each_value(&block) = each_backend(:basic_each_value, block)

#empty?Boolean

Test if self has no items

Returns:

  • (Boolean)


61
# File 'lib/internal/bogohash.rb', line 61

def empty? = size() == 0

#has_key?(rowid) ⇒ Boolean Also known as: include?, member?, key?

Test if rowid is one of the keys in self.

Returns:

  • (Boolean)


51
# File 'lib/internal/bogohash.rb', line 51

def has_key?(rowid) = !! self[rowid]

#inspectString Also known as: to_s

Return a human-friendly string describing this object.

Returns:

  • (String)


36
37
38
39
40
# File 'lib/internal/bogohash.rb', line 36

def inspect
  params = ""
  params = "(#{@query.map(&:inspect).join(", ")})" unless @query.empty?
  return "#{@dbs_klass}.items#{params}"
end

#keysArray[Integer]

Return all keys.

Returns:

  • (Array[Integer])
    • the row IDs



65
# File 'lib/internal/bogohash.rb', line 65

def keys = map{|k,v| k}

#sizeObject Also known as: length

Return the number of items in self.



57
# File 'lib/internal/bogohash.rb', line 57

def size = @dataset.count

#to_aArray[Array[Integer, DBStruct.with(...)]]

Return all keys and values as an array of two-element arrays.

Returns:



73
# File 'lib/internal/bogohash.rb', line 73

def to_a = map{|k,v| [k,v]}

#transaction(*args, **kwargs, &block) ⇒ Object

Calls the underling ‘Sequel::Database#transaction` methods with all arguments.

Convenience method.



224
225
# File 'lib/internal/bogohash.rb', line 224

def transaction(*args, **kwargs, &block) =
@dbs_klass.transaction(*args, **kwargs, &block)

#valuesArray[DBStruct.with(...)]

Return all values.

Returns:



69
# File 'lib/internal/bogohash.rb', line 69

def values = map{|k,v| v}

#where(*cond, **kwcond, &block) ⇒ BogoHash

Expose ‘Sequel::Dataset#where` for more advanced querying.

The underlying Sequel::Dataset‘s where method is called with all of the arguments and the result is a new BogoHash containing only the items that match the query.

This is the Bigger Hammer you can use to narrow a down to a subset of your data when groups are not enough.

Returns:



210
211
212
213
214
# File 'lib/internal/bogohash.rb', line 210

def where(*cond, **kwcond, &block) 
  new_dataset = @dataset.where(*cond, **kwcond, &block)
  desc = ["SQL:#{new_dataset.sql}"]
  return self.class.new(@dbs_klass, new_dataset, desc)
end