Permalink
Browse files

Add support for default/forced values on avoided values

  • Loading branch information...
1 parent a2b80ca commit f9116e315cd23a847bc3b89d2c3fcdfd24ea9807 @olabini committed Sep 10, 2008
View
@@ -1,9 +1,9 @@
- Think about unit of work
-
- Helper to create model objects:
model_from_database :Track
- Ribs should allow advanced functionality such as Unit of Work, Repository, Data Mapper, but also scale down to Active Record level
-- Add default values
-- Add default value to avoid so that AVOID values can be used for NOT NULL
+- Add logging
+- More than one Ribs definition per model
+
@@ -43,7 +43,9 @@ def [](name)
# Return all the properties for this model.
def properties
self.persistent_class.property_iterator.to_a.inject({}) do |h, value|
- h[value.name] = value
+ if !value.respond_to?(:getRubyValue)
+ h[value.name] = value
+ end
h
end
end
@@ -58,12 +60,15 @@ class Rib
attr_reader :primary_keys
# List of all columns to avoid
attr_reader :to_avoid
+ # List of default values for columns
+ attr_reader :default_values
# Initializes object
def initialize
@columns = { }
@primary_keys = { }
@to_avoid = []
+ @default_values = { }
end
# Gets or sets the table name to work with. If +name+ is nil,
@@ -89,7 +94,17 @@ def primary_key(column, property = column, options = {})
# Avoids all the provided columns
def avoid(*columns)
- @to_avoid += columns.map{|s| s.to_s.downcase}
+ options = {}
+ if columns.last.kind_of?(Hash)
+ columns, options = columns[0..-2], columns.last
+ end
+ names = columns.map{|s| s.to_s.downcase}
+ @to_avoid += names
+ if options[:default]
+ names.each do |n|
+ @default_values[n] = options[:default]
+ end
+ end
end
end
@@ -125,6 +140,21 @@ def getRubyData(key)
@data[key]
end
end
+
+ class RubyValuedProperty < org.hibernate.mapping.Property
+ include org.jruby.ribs.WithRubyValue
+
+ # The Ruby class
+ attr_accessor :ruby_value
+
+ def initialize(*args)
+ super
+ end
+
+ def getRubyValue
+ @ruby_value
+ end
+ end
class << self
# Define a rib for the class +on+. If a block is given, will first
@@ -208,6 +238,18 @@ def define_ribs(on, options = {})
else
pc.add_property(prop)
end
+ else
+ if !c.nullable
+ prop = RubyValuedProperty.new
+ prop.ruby_value = rib.default_values[c.name.downcase]
+ prop.persistent_class = pc
+ prop.name = ((v=rib.columns[c.name.downcase]) && v[0]) || c.name
+ val = SimpleValue.new(table)
+ val.add_column(c)
+ val.type_name = get_type_for_sql(c.sql_type, c.sql_type_code)
+ prop.value = val
+ pc.add_property(prop)
+ end
end
end
pc.create_primary_key
@@ -11,6 +11,7 @@
import org.hibernate.PropertyNotFoundException;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
+import org.hibernate.mapping.Property;
import org.hibernate.property.Getter;
import org.hibernate.property.PropertyAccessor;
import org.hibernate.property.Setter;
@@ -27,9 +28,13 @@
public class RubyPropertyAccessor implements PropertyAccessor {
private Type type;
+ private Object defaultValue;
- public RubyPropertyAccessor(Type type) {
- this.type = type;
+ public RubyPropertyAccessor(Property property) {
+ this.type = property.getType();
+ if(property instanceof WithRubyValue) {
+ defaultValue = JavaUtil.convertRubyToJava(((WithRubyValue)property).getRubyValue());
+ }
}
private boolean isRubyProxy(Object o) {
@@ -43,6 +48,10 @@ private boolean isRubyProxy(Object o) {
public Getter getGetter(final Class theClass, final String propertyName) throws PropertyNotFoundException {
return new Getter() {
public Object get(Object owner) throws HibernateException {
+ if(defaultValue != null) {
+ return defaultValue;
+ }
+
Ruby runtime = ((IRubyObject)owner).getRuntime();
String name = propertyName.toLowerCase();
IRubyObject rubyValue;
@@ -99,6 +108,9 @@ public String getMethodName() {
public void set(Object target, Object value, SessionFactoryImplementor factory)
throws HibernateException {
+ if(defaultValue != null) {
+ return;
+ }
Ruby runtime = ((IRubyObject)target).getRuntime();
try {
@@ -27,7 +27,7 @@ private PropertyAccessor buildPropertyAccessor(Property mappedProperty) {
return mappedProperty.getPropertyAccessor(null);
}
else {
- return new RubyPropertyAccessor(mappedProperty.getType());
+ return new RubyPropertyAccessor(mappedProperty);
}
}
@@ -0,0 +1,7 @@
+package org.jruby.ribs;
+
+import org.jruby.runtime.builtin.IRubyObject;
+
+public interface WithRubyValue {
+ public IRubyObject getRubyValue();
+}
View
@@ -79,7 +79,7 @@ def reset_database!
CREATE TABLE DB_TRACK (
TRACK_ID INT NOT NULL,
title VARCHAR(255) NOT NULL,
- filePath VARCHAR(255),
+ filePath VARCHAR(255) NOT NULL,
playTime TIME,
added DATE,
volume INT NOT NULL,
View
@@ -28,7 +28,7 @@ class Track
rib.col :good, :is_good
rib.col :price, :full_price
- rib.avoid :filePath
+ rib.avoid :filePath, :default => "fluxie"
end
# Could do:

0 comments on commit f9116e3

Please sign in to comment.