Class: ThingTank::FakeBase

Inherits:
Object
  • Object
show all
Defined in:
lib/thingtank/fakebase.rb

Overview

a replacement database for characters

Instance Method Summary collapse

Constructor Details

#initialize(doc, character, add_dep = true) ⇒ FakeBase

Returns a new instance of FakeBase.



4
5
6
7
8
9
# File 'lib/thingtank/fakebase.rb', line 4

def initialize(doc, character, add_dep = true)
  @db = doc.database
  @doc = doc
  @character = character
  @doc.dependencies.add_character(character) if add_dep
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



11
12
13
# File 'lib/thingtank/fakebase.rb', line 11

def method_missing(meth, *args)
  raise "don't call #{self.class}: #{meth.inspect}(#{args.inspect})"
end

Instance Method Details

#_character_docObject



58
59
60
# File 'lib/thingtank/fakebase.rb', line 58

def _character_doc
  @doc
end

#_docObject



62
63
64
# File 'lib/thingtank/fakebase.rb', line 62

def _doc
  @doc._root
end

#delete_doc(*character_docs) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/thingtank/fakebase.rb', line 37

def delete_doc(*character_docs)    
  character_docs.each do |character_doc|
    # delete only the attributes from me that are no part of another character, since we will have different doc ids
    # for each doc we will need to identify the remaining attributes
    doc, save = (character_doc["_id"].nil? || character_doc["_id"] == @doc.id) ?
      [@doc, false] : 
      [@db.get(character_doc["_id"]), true]

    doc.delete_character(character_doc)
    doc.save if save # if all characters are from our doc, only save once: at the end
  end
  @doc.save if @doc.changed?
  result()
end

#get(id = :doc_id) ⇒ Object



52
53
54
55
56
# File 'lib/thingtank/fakebase.rb', line 52

def get(id=:doc_id)
  id = @doc.id if id == :doc_id
  doc = id == @doc.id ? @doc : @db.get(id)
  doc.get_character(@character, self)
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/thingtank/fakebase.rb', line 15

def is_a?(klass)
  return true if klass == CouchRest::Database
  super
end

#result(ok = true) ⇒ Object



24
25
26
# File 'lib/thingtank/fakebase.rb', line 24

def result(ok=true)
  {"ok" => ok}
end

#save_doc(character_doc) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/thingtank/fakebase.rb', line 28

def save_doc(character_doc)
  if character_doc.is_a? ThingTank # I dunno why this happens
    @doc.dependencies.refresh_parent()
  else
    @doc.save_character_attributes(character_doc) if character_doc.changed?
  end
  result
end

#to_character(klass) ⇒ Object



20
21
22
# File 'lib/thingtank/fakebase.rb', line 20

def to_character(klass)
  self.class.new(@doc, klass).get()
end