Class: ThingTank::Character

Inherits:
CouchRest::Model::Base
  • Object
show all
Includes:
SharedMethods
Defined in:
lib/thingtank/character.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SharedMethods

included

Class Method Details

.-(key) ⇒ Object



69
70
71
# File 'lib/thingtank/character.rb', line 69

def -(key)
  [self,key]
end

.__subclassesObject



50
51
52
# File 'lib/thingtank/character.rb', line 50

def __subclasses
  @__sub_classes ||= {}
end

.after_inherited(child) ⇒ Object

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



41
42
43
44
# File 'lib/thingtank/character.rb', line 41

def after_inherited(child)
  @__sub_classes ||= {}
  @__sub_classes[child.to_s.to_sym] = child
end

.character_propertiesObject



61
62
63
64
65
66
67
# 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) ⇒ Object



46
47
48
# File 'lib/thingtank/character.rb', line 46

def defined(*args)
  superclass.after_inherited(self) if superclass.respond_to?(:after_inherited)
end

.designObject



93
94
95
# 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) ⇒ Object



73
74
75
76
77
# 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) ⇒ Object



79
80
81
82
# File 'lib/thingtank/character.rb', line 79

def get!(id, db = database)
  doc = ThingTank.get!(id)
  doc.to_character(self)
end

.property(name, *args) ⇒ Object



54
55
56
57
58
59
# 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) ⇒ Object



97
98
99
# 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) ⇒ Object



84
85
86
87
# File 'lib/thingtank/character.rb', line 84

def wants(*modules)
  @wishes ||= []
  @wishes = @wishes.concat modules
end

.wishesObject



89
90
91
# File 'lib/thingtank/character.rb', line 89

def wishes
  @wishes
end

Instance Method Details

#-(key) ⇒ Object



103
104
105
# File 'lib/thingtank/character.rb', line 103

def -(key)
  [self,key]
end

#_character_docObject

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



116
117
118
119
120
121
122
# File 'lib/thingtank/character.rb', line 116

def _character_doc
  if database.is_a?(FakeBase) 
    database._character_doc
  else
    nil
  end
end

#_docObject



107
108
109
110
111
112
113
# 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) ⇒ Object



172
173
174
# File 'lib/thingtank/character.rb', line 172

def add_character(klass, key=nil, &code)
  _character_doc.add_character(klass, key, &code)
end

#first(key) ⇒ Object



164
165
166
# File 'lib/thingtank/character.rb', line 164

def first(key)
  _character_doc.first(key)
end

#flush_to_docObject



139
140
141
142
143
144
# File 'lib/thingtank/character.rb', line 139

def flush_to_doc
  if changed?
    to_doc
    _character_doc.save
  end
end

#last(key) ⇒ Object



168
169
170
# File 'lib/thingtank/character.rb', line 168

def last(key)
  _character_doc.last(key)
end

#reloadObject

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



147
148
149
150
151
152
# 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!Object

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



155
156
157
158
# File 'lib/thingtank/character.rb', line 155

def reload!
  _character_doc.reload
  reload
end

#to_character(klass, key, &code) ⇒ Object



160
161
162
# File 'lib/thingtank/character.rb', line 160

def to_character(klass, key, &code)
  _character_doc.to_character(klass, key, &code)
end

#to_docObject

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



125
126
127
128
129
130
131
132
133
134
135
136
137
# 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