| Module | Ribs::Repository |
| In: |
lib/ribs/repository.rb
|
A Repository is the main gateway into all functionality in Ribs. This is where you send your objects to live in the DB, etc.
A Repository is a combination implementation of both Data Mapper and Repository.
| database | [R] | Every repository is tied to a specific database |
| model | [R] | Every repository is tied to a specific model. This is either a class or an instance of a class |
Dynamically create new database modules
# File lib/ribs/repository.rb, line 213
213: def const_missing(name)
214: if /^DB_(.*?)$/ =~ name.to_s
215: db_name = $1
216: mod = Module.new
217: mod.instance_variable_set :@database_name, db_name.to_sym
218: const_set name, mod
219: mod
220: else
221: super
222: end
223: end
Create a repository for a model inside a database
# File lib/ribs/repository.rb, line 200
200: def create_repository(name, dbmod)
201: c = Class.new
202: mod1 = if Repository.constants.include?(name)
203: Repository.const_get(name)
204: else
205: mod = Module.new
206: Repository.const_set(name, mod)
207: mod
208: end
209: dbmod.const_set name, c
210: end
Makes sure the class sent in is actually a repostiroy. It checks all invariants for a Repository and makes them true if they aren‘t already.
# File lib/ribs/repository.rb, line 172
172: def ensure_repository(name, cls, real, db)
173: unless cls.kind_of?(Repository)
174: mod1 = if Repository.constants.include?(name)
175: Repository.const_get(name)
176: else
177: mod = Module.new
178: Repository.const_set(name, mod)
179: mod
180: end
181:
182: unless mod1.kind_of?(Repository)
183: mod1.send :include, Repository::InstanceMethods
184: end
185:
186: unless mod1.constants.include?("ClassMethods")
187: mod1.const_set(:ClassMethods, Module.new)
188: end
189:
190: cls.send :include, mod1
191: cls.send :extend, mod1.const_get(:ClassMethods)
192: cls.send :extend, Repository::ClassMethods
193: cls.instance_variable_set :@database, db
194: cls.instance_variable_set :@model, real
195: cls.instance_variable_set :@metadata, Ribs::metadata_for(db, real, cls)
196: end
197: end