Module Ribs
In: lib/ribs.rb
lib/ribs/db.rb
lib/ribs/definition.rb
lib/ribs/handle.rb
lib/ribs/repository.rb
lib/ribs/rib.rb

The Ribs module includes most functionality needed for Ribs. The module is strictly a namespace and isn‘t generally supposed to be mixed in.

Methods

Classes and Modules

Module Ribs::Repository
Class Ribs::DB
Class Ribs::Handle
Class Ribs::MetaData
Class Ribs::Rib
Class Ribs::RubyRootClass
Class Ribs::RubyValuedProperty

Constants

Table = org.hibernate.mapping.Table
Column = org.hibernate.mapping.Column
Property = org.hibernate.mapping.Property
SimpleValue = org.hibernate.mapping.SimpleValue
JTypes = java.sql.Types

Public Class methods

Gets a Repository object for the object in question. If the object is a class, the repository returned will be a Repository::Class, otherwise it will be a Repository::Instance. Specifically, what you will get for the class FooBar will be the class Ribs::Repository::DB_default::FooBar and you will get an instance of that class if the object is an instance of FooBar. This allows you to add specific functionality to these repositories. The class Ribs::Repository::DB_default::FooBar will also include Ribs::Repository::FooBar and extend Ribs::Repository::FooBar::ClassMethods so you can add behavior to these that map over all databases.

[Source]

    # File lib/ribs/repository.rb, line 13
13:   def self.Repository(obj, db = :default)
14:     db_name = "DB_#{db}"
15:     model_type = case obj
16:           when Class
17:             obj
18:           else
19:             obj.class
20:           end
21: 
22:     dbmod = Ribs::Repository::const_get(db_name)
23:     name = model_type.name.split(/::/).join("_")
24: 
25:     if !dbmod.constants.include?(name)
26:       Repository::create_repository(name, dbmod)
27:     end
28:     
29:     cls = dbmod::const_get(name.to_sym)
30:     Ribs::Repository.ensure_repository(name, cls, model_type, db)
31: 
32:     return cls if obj.kind_of?(Class)
33: 
34:     ret = cls.allocate
35:     ret.send :initialize
36:     ret.instance_variable_set :@database, db
37:     ret.instance_variable_set :@model, obj
38:     ret
39:   end

Adds metadata for a specific model and an optional database. If the database is nil, it will add the model as a default metadata

[Source]

    # File lib/ribs/definition.rb, line 15
15:     def add_metadata_for(db, model, metadata)
16:       if db
17:         @ribs[[db, model]] = metadata
18:       else
19:         @ribs[model] = metadata
20:       end
21:     end

Defines a model with the given name, defining attribute accessors and also providing the Ribs mapping from the block

[Source]

    # File lib/ribs.rb, line 58
58:     def define_model(name, options = {}, &block)
59:       base = Object
60:       cls = Class.new
61:       names = name.to_s.split(/::/)
62:       names[0..-2].each do |nm|
63:         if !base.constants.include?(nm)
64:           base.const_set nm, Module.new
65:         end
66:         base = base.const_get nm
67:       end
68:       base.const_set names.last, cls
69: 
70:       Ribs!({:on => cls}.merge(options), &block)
71:       R(cls).define_accessors
72:       cls
73:     end

Define a rib for the class on. If a block is given, will first yield an instance of Rib to it and then base the mapping definition on that.

options have several possible values:

  • :db - the database to connect this model to

This method is in urgent need of refactoring.

[Source]

     # File lib/ribs/definition.rb, line 145
145:     def define_ribs(on, options = {})
146:       rib = Rib.new
147:       yield rib if block_given?
148:        
149:       unless options[:metadata]
150:         options[:metadata] = Ribs::MetaData.new      
151:       end
152: 
153:       rm = options[:metadata]
154:       Ribs::add_metadata_for(options[:db], on, rm)
155:       rm.identity_map = options.key?(:identity_map) ? options[:identity_map] : true
156: 
157:       unless options[:from]
158:         options[:from] = R(on, options[:db] || :default)
159:       end
160: 
161:       rm.rib = rib
162:       rib_data = rib.__column_data__
163:       
164:       
165:       db = nil
166:       with_handle(options[:db] || :default) do |h|
167:         db = h.db
168:         m = h.meta_data
169: 
170:         name = table_name_for((options[:table] || on.name), m)
171: 
172:         tables = m.get_tables nil, nil, name.to_s, %w(TABLE VIEW ALIAS SYNONYM).to_java(:String)
173:         if tables.next
174:           table = Table.new(tables.get_string(3))
175:           rm.table = table
176:           c = tables.get_string(1)
177:           table.catalog = c if c && c.length > 0
178:           c = tables.get_string(2)
179:           table.schema = c if c && c.length > 0
180:           
181:           columns_rs = m.get_columns table.catalog, table.schema, table.name, nil
182: 
183:           while columns_rs.next
184:             c = Column.new(columns_rs.get_string(4))
185:             c.default_value = columns_rs.get_string(13)
186:             c.length = columns_rs.get_int(7)
187:             c.nullable = columns_rs.get_string(18) == 'YES'
188:             c.precision = columns_rs.get_int(10)
189:             c.scale = columns_rs.get_int(9)
190:             c.sql_type = columns_rs.get_string(6)
191:             c.sql_type_code = java.lang.Integer.new(columns_rs.get_int(5))
192: 
193:             table.add_column(c)
194:           end
195:           columns_rs.close rescue nil
196:           tables.close rescue nil
197:           
198:           pc = RubyRootClass.new
199:           pc.ruby_class = on
200:           pc.entity_name = on.name
201:           pc.table = table
202:           pc.add_tuplizer(org.hibernate.EntityMode::MAP, "org.jruby.ribs.RubyTuplizer")
203:           
204:           rm.persistent_class = pc
205:           rm.persistent_class["meatspace"] = options[:from] if options[:from]
206:           
207:           table.column_iterator.each do |c|
208:             unless rib_data.to_avoid.include?(c.name.downcase)
209:               prop = Property.new
210:               prop.persistent_class = pc
211:               prop.name = ((v=rib_data.columns.find{ |key, val| val[0].to_s.downcase == c.name.downcase}) && v[0]) || c.name
212:               val = SimpleValue.new(table)
213:               val.add_column(c)
214:               val.type_name = get_type_for_sql(c.sql_type, c.sql_type_code)
215:               prop.value = val
216:               if (!rib_data.primary_keys.empty? && rib_data.primary_keys.include?(prop.name)) || c.name.downcase == 'id'
217:                 pc.identifier_property = prop
218:                 pc.identifier = val
219:               else
220:                 pc.add_property(prop)
221:               end
222:             else
223:               if !c.nullable
224:                 prop = RubyValuedProperty.new
225:                 prop.ruby_value = rib_data.default_values[c.name.downcase]
226:                 prop.persistent_class = pc
227:                 prop.name = ((v=rib_data.columns[c.name.downcase]) && v[0]) || c.name
228:                 val = SimpleValue.new(table)
229:                 val.add_column(c)
230:                 val.type_name = get_type_for_sql(c.sql_type, c.sql_type_code)
231:                 prop.value = val
232:                 pc.add_property(prop)
233:               end
234:             end
235:           end
236:           pc.create_primary_key
237:           db.mappings.add_class(pc)
238:         else
239:           tables.close rescue nil
240:           raise "No table found for: #{name}"
241:         end
242:       end
243:       
244:       db.reset_session_factory!
245:     end

Returns the specific metadata for a combination of model class and database identifier. If there is no such entry, tries to find the default entry for the model class.

[Source]

    # File lib/ribs/definition.rb, line 9
 9:     def metadata(db, model)
10:       @ribs[[db, model]] || @ribs[model]
11:     end

Tries to find metadata for the model in question and defines new metadata with define_ribs if no existing metadata exists. This means that you should do your database definitions before using the method R for that model.

[Source]

    # File lib/ribs/definition.rb, line 27
27:     def metadata_for(db, model, from)
28:       if (v = metadata(db, model))
29:         return v
30:       end
31: 
32:       metadata = Ribs::MetaData.new
33:       define_ribs(model, :db => db, :metadata => metadata, :from => from)
34:       metadata
35:     end

The with_handle method provides an easy way of working with a low level Ribs Handle. It will get a handle for the database in question, yield that handle to the block and then release the handle when finished. This should generally not be needed, but wrapping a block if code with this method is a good way of opening a handle and make sure it doesn‘t get fully closed until after the block.

from decides which database definition to get a handle for.

[Source]

    # File lib/ribs.rb, line 49
49:     def with_handle(from = :default)
50:       h = Ribs::Handle.get(from)
51:       yield h
52:     ensure
53:       h.release
54:     end

[Validate]