class ThingTank::Character

Public Class Methods

-(key) click to toggle source
# File lib/thingtank/character.rb, line 69
def -(key)
  [self,key]
end
__subclasses() click to toggle source
# File lib/thingtank/character.rb, line 50
def __subclasses
  @__sub_classes ||= {}
end
after_inherited(child) click to toggle source

we need this hack with after_inherited and defined or active record is going wild, see: stackoverflow.com/questions/790626/ruby-can-i-have-something-like-classinherited-thats-triggered-only-after-the 'I'm trying to add behavior to activerecord models, but I need all the model customizations to go through before I mess with it. I'm trying to add behavior to activerecord models, but I need all the model customizations to go through before I mess with it. ' exactly my case simple inherited leads to chaos here

# File lib/thingtank/character.rb, line 41
def after_inherited(child)
  @__sub_classes ||= {}
  @__sub_classes[child.to_s.to_sym] = child
end
character_properties() click to toggle source
# File lib/thingtank/character.rb, line 61
def character_properties
  if superclass.respond_to? :character_properties
    (superclass.character_properties || []).concat(@character_properties || []).uniq
  else
    @character_properties
  end
end
defined(*args) click to toggle source
# File lib/thingtank/character.rb, line 46
def defined(*args)
  superclass.after_inherited(self) if superclass.respond_to?(:after_inherited)
end
design() click to toggle source
# File lib/thingtank/character.rb, line 93
def design
  raise "design is not supported in ThingTank::Character, please use the 'character_view' method in a ThingTank subclass design definition"
end
get(id, db = database) click to toggle source
# File lib/thingtank/character.rb, line 73
def get(id, db = database)
  doc = ThingTank.get(id)
  return nil if doc.nil?
  return doc.to_character(self)
end
get!(id, db = database) click to toggle source
# File lib/thingtank/character.rb, line 79
def get!(id, db = database)
  doc = ThingTank.get!(id)
  doc.to_character(self)
end
property(name, *args) click to toggle source
Calls superclass method
# File lib/thingtank/character.rb, line 54
def property(name, *args)
  @character_properties ||= []
  @character_properties << name.to_s
  #p [:prop, self.name, @character_properties]
  super
end
view_by(*args) click to toggle source
# File lib/thingtank/character.rb, line 97
def view_by(*args)
  raise "view_by is not supported in ThingTank::Character, please use the 'character_view_by' method in a ThingTank subclass"
end
wants(*modules) click to toggle source
# File lib/thingtank/character.rb, line 84
def wants(*modules)
  @wishes ||= []
  @wishes = @wishes.concat modules
end
wishes() click to toggle source
# File lib/thingtank/character.rb, line 89
def wishes
  @wishes
end

Public Instance Methods

-(key) click to toggle source
# File lib/thingtank/character.rb, line 103
def -(key)
  [self,key]
end
_character_doc() click to toggle source

the virtual _doc that contains me, you should not need it normally

# File lib/thingtank/character.rb, line 116
def _character_doc
  if database.is_a?(FakeBase) 
    database._character_doc
  else
    nil
  end
end
_doc() click to toggle source
# File lib/thingtank/character.rb, line 107
def _doc
  if database.is_a?(FakeBase) 
    database._doc
  else
    nil
  end
end
add_character(klass, key=nil, &code) click to toggle source
# File lib/thingtank/character.rb, line 172
def add_character(klass, key=nil, &code)
  _character_doc.add_character(klass, key, &code)
end
first(key) click to toggle source
# File lib/thingtank/character.rb, line 164
def first(key)
  _character_doc.first(key)
end
flush_to_doc() click to toggle source
# File lib/thingtank/character.rb, line 139
def flush_to_doc
  if changed?
    to_doc
    _character_doc.save
  end
end
last(key) click to toggle source
# File lib/thingtank/character.rb, line 168
def last(key)
  _character_doc.last(key)
end
reload() click to toggle source

use reload to get the latest properties from the doc without reloading all from the database

# File lib/thingtank/character.rb, line 147
def reload
  attrs = _character_doc.as(self.class).to_character_hash
  prepare_all_attributes(attrs, :directly_set_attributes => true)
  @changed_attributes.clear if @changed_attributes
  self
end
reload!() click to toggle source

use reload! to get the latest properties from the database, the doc will be reloaded and the character to

# File lib/thingtank/character.rb, line 155
def reload!
  _character_doc.reload
  reload
end
to_character(klass, key, &code) click to toggle source
# File lib/thingtank/character.rb, line 160
def to_character(klass, key, &code)
  _character_doc.to_character(klass, key, &code)
end
to_doc() click to toggle source

use to_doc to put characters changes back to the doc without saving the doc

# File lib/thingtank/character.rb, line 125
def to_doc
  if changed?
    valid = valid?
    changed_to_character_hash().each do |k,v|
      _character_doc[k] = v
    end
    if valid
      @changed_attributes.clear if @changed_attributes
    else
      _doc.errors.add self.class.to_s, self.errors.messages
    end
  end
end