Class: Diametric::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/diametric/bucket.rb

Instance Method Summary collapse

Constructor Details

#initializeBucket

Returns a new instance of Bucket


3
4
5
6
7
8
# File 'lib/diametric/bucket.rb', line 3

def initialize
  @tempids = Array.new
  @holder = Hash.new
  @temp_ref = -1000000
  @entity_class = nil
end

Instance Method Details

#[](key) ⇒ Object


25
26
27
# File 'lib/diametric/bucket.rb', line 25

def [](key)
  @holder[key]
end

#build(entity_class, attributes) ⇒ Object

Builds transact data


11
12
13
14
15
16
17
18
19
# File 'lib/diametric/bucket.rb', line 11

def build(entity_class, attributes)
  tempid = entity_class.tempid(:"db.part/user", next_temp_ref)
  data = Hash[attributes.map {|k, v| [(entity_class.prefix + "/" + k.to_s).to_sym, v]}]
  data.merge!({:"db/id" => tempid})
  @tempids << tempid
  @holder[tempid] = data
  @entity_class ||= entity_class
  tempid
end

#next_temp_refObject


21
22
23
# File 'lib/diametric/bucket.rb', line 21

def next_temp_ref
  @temp_ref -= 1
end

#save(conn = nil) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/diametric/bucket.rb', line 42

def save(conn=nil)
  if @entity_class.ancestors.include?(Diametric::Persistence::REST)
    conn ||= Diametric::Persistence::REST.connection
    conn.transact(Diametric::Persistence::REST.database, tx_data)
  else
    conn ||= Diametric::Persistence::Peer.connect
    conn.transact(tx_data).get
  end
  @tempids = Array.new
  @holder = Hash.new
  @temp_ref = -1000000
  @entity_class = nil
end

#sizeObject Also known as: length, count


29
30
31
# File 'lib/diametric/bucket.rb', line 29

def size
  @tempids.size
end

#tx_dataObject


35
36
37
38
39
40
# File 'lib/diametric/bucket.rb', line 35

def tx_data
  @tempids.inject([]) do |memo, id|
    memo << @holder[id]
    memo
  end
end