lafcadio 0.4.3 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/lafcadio_schema +28 -0
- data/lib/lafcadio.rb +3 -4
- data/lib/lafcadio.rb~ +3 -4
- data/lib/lafcadio/TestSuite.rb +2 -0
- data/lib/lafcadio/TestSuite.rb~ +16 -0
- data/lib/lafcadio/dateTime.rb +93 -2
- data/lib/lafcadio/{dateTime/Month.rb → dateTime.rb~} +33 -33
- data/lib/lafcadio/depend.rb +3 -0
- data/lib/lafcadio/domain.rb +574 -70
- data/lib/lafcadio/domain.rb~ +570 -70
- data/lib/lafcadio/mock.rb +92 -2
- data/lib/lafcadio/mock.rb~ +93 -0
- data/lib/lafcadio/objectField.rb +614 -3
- data/lib/lafcadio/objectField.rb~ +618 -0
- data/lib/lafcadio/objectStore.rb +662 -19
- data/lib/lafcadio/objectStore.rb~ +746 -0
- data/lib/lafcadio/query.rb +415 -31
- data/lib/lafcadio/query.rb~ +572 -0
- data/lib/lafcadio/schema.rb +57 -2
- data/lib/lafcadio/test.rb +17 -2
- data/lib/lafcadio/{test/LafcadioTestCase.rb → test.rb~} +5 -5
- data/lib/lafcadio/test/testconfig.dat +1 -1
- data/lib/lafcadio/util.rb +337 -20
- metadata +16 -77
- data/lib/lafcadio/domain/DomainObject.rb +0 -375
- data/lib/lafcadio/domain/DomainObject.rb~ +0 -371
- data/lib/lafcadio/domain/MapObject.rb +0 -22
- data/lib/lafcadio/domain/ObjectType.rb +0 -80
- data/lib/lafcadio/includer.rb +0 -18
- data/lib/lafcadio/mock/MockDbBridge.rb +0 -78
- data/lib/lafcadio/mock/MockDbBridge.rb~ +0 -74
- data/lib/lafcadio/mock/MockObjectStore.rb +0 -20
- data/lib/lafcadio/objectField/AutoIncrementField.rb +0 -25
- data/lib/lafcadio/objectField/BooleanField.rb +0 -83
- data/lib/lafcadio/objectField/DateField.rb +0 -33
- data/lib/lafcadio/objectField/DateTimeField.rb +0 -25
- data/lib/lafcadio/objectField/DecimalField.rb +0 -41
- data/lib/lafcadio/objectField/EmailField.rb +0 -28
- data/lib/lafcadio/objectField/EnumField.rb +0 -62
- data/lib/lafcadio/objectField/FieldValueError.rb +0 -4
- data/lib/lafcadio/objectField/IntegerField.rb +0 -15
- data/lib/lafcadio/objectField/LinkField.rb +0 -92
- data/lib/lafcadio/objectField/LinkField.rb~ +0 -86
- data/lib/lafcadio/objectField/MoneyField.rb +0 -13
- data/lib/lafcadio/objectField/MonthField.rb +0 -16
- data/lib/lafcadio/objectField/ObjectField.rb +0 -142
- data/lib/lafcadio/objectField/PasswordField.rb +0 -29
- data/lib/lafcadio/objectField/StateField.rb +0 -13
- data/lib/lafcadio/objectField/SubsetLinkField.rb +0 -25
- data/lib/lafcadio/objectField/TextField.rb +0 -23
- data/lib/lafcadio/objectField/TextListField.rb +0 -21
- data/lib/lafcadio/objectField/TimeStampField.rb +0 -15
- data/lib/lafcadio/objectStore/Cache.rb +0 -81
- data/lib/lafcadio/objectStore/Committer.rb +0 -65
- data/lib/lafcadio/objectStore/CouldntMatchObjectTypeError.rb +0 -4
- data/lib/lafcadio/objectStore/DbBridge.rb +0 -140
- data/lib/lafcadio/objectStore/DbBridge.rb~ +0 -140
- data/lib/lafcadio/objectStore/DomainComparable.rb +0 -25
- data/lib/lafcadio/objectStore/DomainObjectInitError.rb +0 -9
- data/lib/lafcadio/objectStore/DomainObjectNotFoundError.rb +0 -4
- data/lib/lafcadio/objectStore/DomainObjectProxy.rb +0 -62
- data/lib/lafcadio/objectStore/DomainObjectSqlMaker.rb +0 -74
- data/lib/lafcadio/objectStore/ObjectStore.rb +0 -207
- data/lib/lafcadio/objectStore/ObjectStore.rb~ +0 -207
- data/lib/lafcadio/objectStore/SqlValueConverter.rb +0 -30
- data/lib/lafcadio/objectStore/SqlValueConverter.rb~ +0 -30
- data/lib/lafcadio/query/Compare.rb +0 -55
- data/lib/lafcadio/query/CompoundCondition.rb +0 -39
- data/lib/lafcadio/query/Condition.rb +0 -66
- data/lib/lafcadio/query/Condition.rb~ +0 -66
- data/lib/lafcadio/query/Equals.rb +0 -45
- data/lib/lafcadio/query/In.rb +0 -20
- data/lib/lafcadio/query/Like.rb +0 -48
- data/lib/lafcadio/query/Link.rb +0 -20
- data/lib/lafcadio/query/Max.rb +0 -32
- data/lib/lafcadio/query/Max.rb~ +0 -25
- data/lib/lafcadio/query/Not.rb +0 -21
- data/lib/lafcadio/query/Query.rb +0 -92
- data/lib/lafcadio/schema/CreateTableStatement.rb +0 -61
- data/lib/lafcadio/schema/CreateTableStatement.rb~ +0 -59
- data/lib/lafcadio/util/Context.rb +0 -61
- data/lib/lafcadio/util/ContextualService.rb +0 -33
- data/lib/lafcadio/util/English.rb +0 -117
- data/lib/lafcadio/util/HashOfArrays.rb +0 -48
- data/lib/lafcadio/util/LafcadioConfig.rb +0 -25
- data/lib/lafcadio/util/QueueHash.rb +0 -67
- data/lib/lafcadio/util/UsStates.rb +0 -29
- data/lib/lafcadio/xml.rb +0 -2
data/lib/lafcadio/domain.rb~
CHANGED
@@ -1,37 +1,101 @@
|
|
1
|
-
require 'lafcadio/includer'
|
2
|
-
Includer.include( 'domain' )
|
3
1
|
require 'lafcadio/objectField'
|
4
|
-
require 'lafcadio/util'
|
2
|
+
require 'lafcadio/util'
|
5
3
|
require 'rexml/document'
|
6
4
|
|
7
5
|
module Lafcadio
|
8
6
|
class ClassDefinitionXmlParser # :nodoc: all
|
9
|
-
|
7
|
+
def initialize( domain_class, xml )
|
8
|
+
@domain_class = domain_class
|
9
|
+
@xmlDocRoot = REXML::Document.new( xml ).root
|
10
|
+
@namesProcessed = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def get_class_field( fieldElt )
|
14
|
+
className = fieldElt.attributes['class'].to_s
|
15
|
+
name = fieldElt.attributes['name']
|
16
|
+
begin
|
17
|
+
fieldClass = Class.get_class( 'Lafcadio::' + className )
|
18
|
+
register_name( name )
|
19
|
+
field = fieldClass.instantiate_from_xml( @domain_class, fieldElt )
|
20
|
+
set_field_attributes( field, fieldElt )
|
21
|
+
rescue MissingError
|
22
|
+
msg = "Couldn't find field class '#{ className }' for field " +
|
23
|
+
"'#{ name }'"
|
24
|
+
raise( MissingError, msg, caller )
|
25
|
+
end
|
26
|
+
field
|
27
|
+
end
|
10
28
|
|
29
|
+
def get_class_fields
|
30
|
+
namesProcessed = {}
|
31
|
+
pk_field = PrimaryKeyField.new( @domain_class )
|
32
|
+
if ( spkn = @xmlDocRoot.attributes['sql_primary_key_name'] )
|
33
|
+
pk_field.db_field_name = spkn
|
34
|
+
end
|
35
|
+
fields = [ pk_field ]
|
36
|
+
@xmlDocRoot.elements.each('field') { |fieldElt|
|
37
|
+
fields << get_class_field( fieldElt )
|
38
|
+
}
|
39
|
+
fields
|
40
|
+
end
|
41
|
+
|
42
|
+
def possible_field_attributes
|
43
|
+
fieldAttr = []
|
44
|
+
fieldAttr << FieldAttribute.new( 'size', FieldAttribute::INTEGER )
|
45
|
+
fieldAttr << FieldAttribute.new( 'unique', FieldAttribute::BOOLEAN )
|
46
|
+
fieldAttr << FieldAttribute.new( 'not_null', FieldAttribute::BOOLEAN )
|
47
|
+
fieldAttr << FieldAttribute.new( 'enum_type', FieldAttribute::ENUM,
|
48
|
+
BooleanField )
|
49
|
+
fieldAttr << FieldAttribute.new( 'enums', FieldAttribute::HASH )
|
50
|
+
fieldAttr << FieldAttribute.new( 'range', FieldAttribute::ENUM,
|
51
|
+
DateField )
|
52
|
+
fieldAttr << FieldAttribute.new( 'large', FieldAttribute::BOOLEAN )
|
53
|
+
end
|
54
|
+
|
55
|
+
def register_name( name )
|
56
|
+
raise InvalidDataError if @namesProcessed[name]
|
57
|
+
@namesProcessed[name] = true
|
58
|
+
end
|
59
|
+
|
60
|
+
def set_field_attributes( field, fieldElt )
|
61
|
+
possible_field_attributes.each { |fieldAttr|
|
62
|
+
fieldAttr.maybe_set_field_attr( field, fieldElt )
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
def table_name
|
67
|
+
@xmlDocRoot.attributes['table_name']
|
68
|
+
end
|
69
|
+
|
11
70
|
class FieldAttribute
|
12
71
|
INTEGER = 1
|
13
72
|
BOOLEAN = 2
|
14
73
|
ENUM = 3
|
15
74
|
HASH = 4
|
16
75
|
|
17
|
-
attr_reader :name, :
|
76
|
+
attr_reader :name, :value_class
|
18
77
|
|
19
|
-
def initialize( name,
|
20
|
-
@name = name; @
|
78
|
+
def initialize( name, value_class, objectFieldClass = nil )
|
79
|
+
@name = name; @value_class = value_class
|
21
80
|
@objectFieldClass = objectFieldClass
|
22
81
|
end
|
23
82
|
|
24
|
-
def
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
83
|
+
def maybe_set_field_attr( field, fieldElt )
|
84
|
+
setterMethod = "#{ name }="
|
85
|
+
if field.respond_to?( setterMethod )
|
86
|
+
if value_class != FieldAttribute::HASH
|
87
|
+
if ( attrStr = fieldElt.attributes[name] )
|
88
|
+
field.send( setterMethod, value_from_string( attrStr ) )
|
89
|
+
end
|
90
|
+
else
|
91
|
+
if ( attrElt = fieldElt.elements[name] )
|
92
|
+
field.send( setterMethod, value_from_elt( attrElt ) )
|
93
|
+
end
|
94
|
+
end
|
31
95
|
end
|
32
96
|
end
|
33
|
-
|
34
|
-
def
|
97
|
+
|
98
|
+
def value_from_elt( elt )
|
35
99
|
hash = {}
|
36
100
|
elt.elements.each( English.singular( @name ) ) { |subElt|
|
37
101
|
key = subElt.attributes['key'] == 'true'
|
@@ -41,79 +105,515 @@ module Lafcadio
|
|
41
105
|
hash
|
42
106
|
end
|
43
107
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
else
|
52
|
-
if ( attrElt = fieldElt.elements[name] )
|
53
|
-
field.send( setterMethod, valueFromElt( attrElt ) )
|
54
|
-
end
|
55
|
-
end
|
108
|
+
def value_from_string( valueStr )
|
109
|
+
if @value_class == INTEGER
|
110
|
+
valueStr.to_i
|
111
|
+
elsif @value_class == BOOLEAN
|
112
|
+
valueStr == 'y'
|
113
|
+
elsif @value_class == ENUM
|
114
|
+
eval "#{ @objectFieldClass.name }::#{ valueStr }"
|
56
115
|
end
|
57
116
|
end
|
58
117
|
end
|
59
118
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
119
|
+
class InvalidDataError < ArgumentError; end
|
120
|
+
end
|
121
|
+
|
122
|
+
module DomainComparable
|
123
|
+
include Comparable
|
124
|
+
|
125
|
+
# A DomainObject or DomainObjectProxy is compared by +object_type+ and by
|
126
|
+
# +pk_id+.
|
127
|
+
def <=>(anOther)
|
128
|
+
if anOther.respond_to?( 'object_type' )
|
129
|
+
if self.object_type == anOther.object_type
|
130
|
+
self.pk_id <=> anOther.pk_id
|
131
|
+
else
|
132
|
+
self.object_type.name <=> anOther.object_type.name
|
133
|
+
end
|
134
|
+
else
|
135
|
+
nil
|
136
|
+
end
|
64
137
|
end
|
65
138
|
|
66
|
-
def
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
139
|
+
def eql?(otherObj)
|
140
|
+
self == otherObj
|
141
|
+
end
|
142
|
+
|
143
|
+
def hash; "#{ self.class.name } #{ pk_id }".hash; end
|
144
|
+
end
|
145
|
+
|
146
|
+
# All classes that correspond to a table in the database need to be children
|
147
|
+
# of DomainObject.
|
148
|
+
#
|
149
|
+
# = Defining fields
|
150
|
+
# There are two ways to define the fields of a DomainObject subclass.
|
151
|
+
# 1. Defining fields in an XML file. To do this,
|
152
|
+
# 1. Set one directory to contain all your XML files, by setting
|
153
|
+
# +classDefinitionDir+ in your LafcadioConfig file.
|
154
|
+
# 2. Write one XML file per domain class. For example, a User.xml file
|
155
|
+
# might look like:
|
156
|
+
# <lafcadio_class_definition name="User">
|
157
|
+
# <field name="lastName" class="TextField"/>
|
158
|
+
# <field name="email" class="TextField"/>
|
159
|
+
# <field name="password" class="TextField"/>
|
160
|
+
# <field name="birthday" class="DateField"/>
|
161
|
+
# </lafcadio_class_definition>
|
162
|
+
# 2. Overriding DomainObject.get_class_fields. The method should return an Array
|
163
|
+
# of instances of ObjectField or its children. The order is unimportant.
|
164
|
+
# For example:
|
165
|
+
# class User < DomainObject
|
166
|
+
# def User.get_class_fields
|
167
|
+
# fields = []
|
168
|
+
# fields << TextField.new(self, 'firstName')
|
169
|
+
# fields << TextField.new(self, 'lastName')
|
170
|
+
# fields << TextField.new(self, 'email')
|
171
|
+
# fields << TextField.new(self, 'password')
|
172
|
+
# fields << DateField.new(self, 'birthday')
|
173
|
+
# fields
|
174
|
+
# end
|
175
|
+
# end
|
176
|
+
#
|
177
|
+
# = Setting and retrieving fields
|
178
|
+
# Once your fields are defined, you can create an instance by passing in a
|
179
|
+
# hash of field names and values.
|
180
|
+
# john = User.new( 'firstName' => 'John', 'lastName' => 'Doe',
|
181
|
+
# 'email' => 'john.doe@email.com',
|
182
|
+
# 'password' => 'my_password',
|
183
|
+
# 'birthday' => tenYearsAgo )
|
184
|
+
#
|
185
|
+
# You can read and write these fields like normal instance attributes.
|
186
|
+
# john.email => 'john.doe@email.com'
|
187
|
+
# john.email = 'john.doe@mail.email.com'
|
188
|
+
#
|
189
|
+
# If your domain class has fields that refer to other domain classes, or even
|
190
|
+
# to another row in the same table, you can use a LinkField to express the
|
191
|
+
# relation.
|
192
|
+
# <lafcadio_class_definition name="Message">
|
193
|
+
# <field name="subject" class="TextField" />
|
194
|
+
# <field name="body" class="TextField" />
|
195
|
+
# <field name="author" class="LinkField" linked_type="User" />
|
196
|
+
# <field name="recipient" class="LinkField" linked_type="User" />
|
197
|
+
# <field name="dateSent" class="DateField" />
|
198
|
+
# </lafcadio_class_definition>
|
199
|
+
#
|
200
|
+
# msg = Message.new( 'subject' => 'hi there',
|
201
|
+
# 'body' => 'You wanna go to the movies on Saturday?',
|
202
|
+
# 'author' => john, 'recipient' => jane,
|
203
|
+
# 'dateSent' => Date.today )
|
204
|
+
#
|
205
|
+
# = pk_id and committing
|
206
|
+
# Lafcadio requires that each table has a numeric primary key. It assumes that
|
207
|
+
# this key is named +pk_id+ in the database, though that can be overridden.
|
208
|
+
#
|
209
|
+
# When you create a domain object by calling new, you should not assign a
|
210
|
+
# +pk_id+ to the new instance. The pk_id will automatically be set when you
|
211
|
+
# commit the object by calling DomainObject#commit.
|
212
|
+
#
|
213
|
+
# However, you may want to manually set +pk_id+ when setting up a test case, so
|
214
|
+
# you can ensure that a domain object has a given primary key.
|
215
|
+
#
|
216
|
+
# = Naming assumptions, and how to override them
|
217
|
+
# By default, Lafcadio assumes that every domain object is indexed by the
|
218
|
+
# field +pk_id+ in the database schema. If you're dealing with a table that
|
219
|
+
# uses a different field name, override DomainObject.sql_primary_key_name.
|
220
|
+
# However, you will always use +pk_id+ in your Ruby code.
|
221
|
+
#
|
222
|
+
# Lafcadio assumes that a domain class corresponds to a table whose name is
|
223
|
+
# the plural of the class name, and whose first letter is lowercase. A User
|
224
|
+
# class is assumed to be stored in a "users" table, while a ProductCategory
|
225
|
+
# class is assumed to be stored in a "productCategories" table. Override
|
226
|
+
# DomainObject.table_name to override this behavior.
|
227
|
+
#
|
228
|
+
# = Inheritance
|
229
|
+
# Domain classes can inherit from other domain classes; they have all the
|
230
|
+
# fields of any concrete superclasses plus any new fields defined for
|
231
|
+
# themselves. You can use normal inheritance to define this:
|
232
|
+
# class User < DomainObject
|
233
|
+
# ...
|
234
|
+
# end
|
235
|
+
#
|
236
|
+
# class Administrator < User
|
237
|
+
# ...
|
238
|
+
# end
|
239
|
+
#
|
240
|
+
# Lafcadio assumes that each concrete class has a corresponding table, and
|
241
|
+
# that each table has a +pk_id+ field that is used to match rows between
|
242
|
+
# different levels.
|
243
|
+
class DomainObject
|
244
|
+
@@subclassHash = {}
|
245
|
+
@@class_fields = {}
|
246
|
+
|
247
|
+
COMMIT_ADD = 1
|
248
|
+
COMMIT_EDIT = 2
|
249
|
+
COMMIT_DELETE = 3
|
250
|
+
|
251
|
+
include DomainComparable
|
252
|
+
|
253
|
+
def self.abstract_subclasses #:nodoc:
|
254
|
+
require 'lafcadio/domain'
|
255
|
+
[ MapObject ]
|
256
|
+
end
|
257
|
+
|
258
|
+
# Returns an array of all fields defined for this class and all concrete
|
259
|
+
# superclasses.
|
260
|
+
def self.all_fields
|
261
|
+
all_fields = []
|
262
|
+
self_and_concrete_superclasses.each { |aClass|
|
263
|
+
aClass.class_fields.each { |field| all_fields << field }
|
264
|
+
}
|
265
|
+
all_fields
|
266
|
+
end
|
267
|
+
|
268
|
+
def self.class_fields #:nodoc:
|
269
|
+
class_fields = @@class_fields[self]
|
270
|
+
unless class_fields
|
271
|
+
@@class_fields[self] = self.get_class_fields
|
272
|
+
class_fields = @@class_fields[self]
|
80
273
|
end
|
274
|
+
class_fields
|
275
|
+
end
|
276
|
+
|
277
|
+
def self.create_field( field_class, name, att_hash )
|
278
|
+
class_fields = @@class_fields[self]
|
279
|
+
if class_fields.nil?
|
280
|
+
class_fields = [ PrimaryKeyField.new( self ) ]
|
281
|
+
@@class_fields[self] = class_fields
|
282
|
+
end
|
283
|
+
att_hash['name'] = name
|
284
|
+
field = field_class.instantiate_with_parameters( self, att_hash )
|
285
|
+
att_hash.each { |field_name, value|
|
286
|
+
setter = field_name + '='
|
287
|
+
field.send( setter, value ) if field.respond_to?( setter )
|
288
|
+
}
|
289
|
+
class_fields << field
|
290
|
+
end
|
291
|
+
|
292
|
+
def self.dependent_classes #:nodoc:
|
293
|
+
dependent_classes = {}
|
294
|
+
DomainObject.subclasses.each { |aClass|
|
295
|
+
if aClass != DomainObjectProxy &&
|
296
|
+
(!DomainObject.abstract_subclasses.index(aClass))
|
297
|
+
aClass.class_fields.each { |field|
|
298
|
+
if field.class <= LinkField && field.linked_type == self.object_type
|
299
|
+
dependent_classes[aClass] = field
|
300
|
+
end
|
301
|
+
}
|
302
|
+
end
|
303
|
+
}
|
304
|
+
dependent_classes
|
305
|
+
end
|
306
|
+
|
307
|
+
def self.get_class_field(fieldName) #:nodoc:
|
308
|
+
field = nil
|
309
|
+
self.class_fields.each { |aField|
|
310
|
+
field = aField if aField.name == fieldName
|
311
|
+
}
|
81
312
|
field
|
82
313
|
end
|
314
|
+
|
315
|
+
def DomainObject.get_class_field_by_db_name( fieldName ) #:nodoc:
|
316
|
+
self.class_fields.find { |field| field.db_field_name == fieldName }
|
317
|
+
end
|
83
318
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
319
|
+
# Returns an Array of ObjectField instances for this domain class, parsing
|
320
|
+
# them from XML if necessary.
|
321
|
+
def self.get_class_fields
|
322
|
+
if self.methods( false ).include?( 'get_class_fields' )
|
323
|
+
[ PrimaryKeyField.new( self ) ]
|
324
|
+
else
|
325
|
+
xmlParser = try_load_xml_parser
|
326
|
+
if xmlParser
|
327
|
+
xmlParser.get_class_fields
|
328
|
+
else
|
329
|
+
error_msg = "Couldn't find either an XML class description file " +
|
330
|
+
"or get_class_fields method for " + self.name
|
331
|
+
raise MissingError, error_msg, caller
|
332
|
+
end
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
def self.get_domain_dirs #:nodoc:
|
337
|
+
config = LafcadioConfig.new
|
338
|
+
classPath = config['classpath']
|
339
|
+
domainDirStr = config['domainDirs']
|
340
|
+
if domainDirStr
|
341
|
+
domainDirs = domainDirStr.split(',')
|
342
|
+
else
|
343
|
+
domainDirs = [ classPath + 'domain/' ]
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
def self.get_field( fieldName ) #:nodoc:
|
348
|
+
aDomainClass = self
|
349
|
+
field = nil
|
350
|
+
while aDomainClass < DomainObject && !field
|
351
|
+
field = aDomainClass.get_class_field( fieldName )
|
352
|
+
aDomainClass = aDomainClass.superclass
|
353
|
+
end
|
354
|
+
if field
|
355
|
+
field
|
356
|
+
else
|
357
|
+
errStr = "Couldn't find field \"#{ field }\" in " +
|
358
|
+
"#{ self } domain class"
|
359
|
+
raise( MissingError, errStr, caller )
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
def self.get_object_type_from_string(typeString) #:nodoc:
|
364
|
+
object_type = nil
|
365
|
+
require_domain_file( typeString )
|
366
|
+
subclasses.each { |subclass|
|
367
|
+
object_type = subclass if subclass.to_s == typeString
|
89
368
|
}
|
90
|
-
|
369
|
+
if object_type
|
370
|
+
object_type
|
371
|
+
else
|
372
|
+
raise CouldntMatchObjectTypeError,
|
373
|
+
"couldn't match object_type #{typeString}", caller
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
def self.inherited(subclass) #:nodoc:
|
378
|
+
@@subclassHash[subclass] = true
|
91
379
|
end
|
92
380
|
|
93
|
-
def
|
94
|
-
|
95
|
-
|
381
|
+
def self.is_based_on? #:nodoc:
|
382
|
+
self.superclass.is_concrete?
|
383
|
+
end
|
384
|
+
|
385
|
+
def self.is_concrete? #:nodoc:
|
386
|
+
(self != DomainObject && abstract_subclasses.index(self).nil?)
|
387
|
+
end
|
388
|
+
|
389
|
+
def self.method_missing( methodId, *args ) #:nodoc:
|
390
|
+
method_name = methodId.id2name
|
391
|
+
maybe_field_class_name = ( method_name.gsub( /^(.)/ ) { $&.upcase } ) +
|
392
|
+
'Field'
|
393
|
+
field_class = Lafcadio.const_get( maybe_field_class_name )
|
394
|
+
create_field( field_class, args[0], args[1] || {} )
|
395
|
+
end
|
396
|
+
|
397
|
+
def self.object_type #:nodoc:
|
398
|
+
self
|
399
|
+
end
|
400
|
+
|
401
|
+
def self.require_domain_file( typeString )
|
402
|
+
typeString =~ /([^\:]*)$/
|
403
|
+
fileName = $1
|
404
|
+
get_domain_dirs.each { |domainDir|
|
405
|
+
if Dir.entries(domainDir).index("#{fileName}.rb")
|
406
|
+
require "#{ domainDir }#{ fileName }"
|
407
|
+
end
|
408
|
+
}
|
409
|
+
if (domainFilesStr = LafcadioConfig.new['domainFiles'])
|
410
|
+
domainFilesStr.split(',').each { |domainFile|
|
411
|
+
require domainFile
|
412
|
+
}
|
413
|
+
end
|
414
|
+
end
|
415
|
+
|
416
|
+
def self.self_and_concrete_superclasses # :nodoc:
|
417
|
+
classes = [ ]
|
418
|
+
anObjectType = self
|
419
|
+
until(anObjectType == DomainObject ||
|
420
|
+
abstract_subclasses.index(anObjectType) != nil)
|
421
|
+
classes << anObjectType
|
422
|
+
anObjectType = anObjectType.superclass
|
423
|
+
end
|
424
|
+
classes
|
425
|
+
end
|
426
|
+
|
427
|
+
# Returns the name of the primary key in the database, retrieving it from
|
428
|
+
# the class definition XML if necessary.
|
429
|
+
def self.sql_primary_key_name( set_sql_primary_key_name = nil )
|
430
|
+
if set_sql_primary_key_name
|
431
|
+
get_field( 'pk_id' ).db_field_name = set_sql_primary_key_name
|
432
|
+
end
|
433
|
+
get_field( 'pk_id' ).db_field_name
|
434
|
+
end
|
435
|
+
|
436
|
+
def self.subclasses #:nodoc:
|
437
|
+
@@subclassHash.keys
|
438
|
+
end
|
439
|
+
|
440
|
+
# Returns the table name, which is assumed to be the domain class name
|
441
|
+
# pluralized, and with the first letter lowercase. A User class is
|
442
|
+
# assumed to be stored in a "users" table, while a ProductCategory class is
|
443
|
+
# assumed to be stored in a "productCategories" table.
|
444
|
+
def self.table_name( set_table_name = nil )
|
445
|
+
if set_table_name
|
446
|
+
@table_name = set_table_name
|
447
|
+
elsif @table_name
|
448
|
+
@table_name
|
449
|
+
else
|
450
|
+
xmlParser = try_load_xml_parser
|
451
|
+
if (!xmlParser.nil? && table_name = xmlParser.table_name)
|
452
|
+
table_name
|
453
|
+
else
|
454
|
+
table_name = self.bare_name
|
455
|
+
table_name[0] = table_name[0..0].downcase
|
456
|
+
English.plural table_name
|
457
|
+
end
|
458
|
+
end
|
459
|
+
end
|
460
|
+
|
461
|
+
def self.try_load_xml_parser
|
462
|
+
require 'lafcadio/domain'
|
463
|
+
dirName = LafcadioConfig.new['classDefinitionDir']
|
464
|
+
xmlFileName = self.bare_name + '.xml'
|
465
|
+
xmlPath = File.join( dirName, xmlFileName )
|
466
|
+
xml = ''
|
467
|
+
begin
|
468
|
+
File.open( xmlPath ) { |file| xml = file.readlines.join }
|
469
|
+
ClassDefinitionXmlParser.new( self, xml )
|
470
|
+
rescue Errno::ENOENT
|
471
|
+
# no xml file, so no @xmlParser
|
472
|
+
end
|
96
473
|
end
|
97
474
|
|
98
|
-
|
99
|
-
|
475
|
+
attr_accessor :error_messages, :last_commit, :fields, :fields_set
|
476
|
+
attr_reader :delete
|
477
|
+
protected :fields, :fields_set
|
478
|
+
|
479
|
+
# fieldHash should contain key-value associations for the different
|
480
|
+
# fields of this domain class. For example, instantiating a User class
|
481
|
+
# might look like:
|
482
|
+
#
|
483
|
+
# User.new( 'firstNames' => 'John', 'lastName' => 'Doe',
|
484
|
+
# 'email' => 'john.doe@email.com', 'password' => 'l33t' )
|
485
|
+
#
|
486
|
+
# In normal usage any code you write that creates a domain object will not
|
487
|
+
# define the +pk_id+ field. The system assumes that a domain object with an
|
488
|
+
# undefined +pk_id+ has yet to be inserted into the database, and when you
|
489
|
+
# commit the domain object a +pk_id+ will automatically be assigned.
|
490
|
+
#
|
491
|
+
# If you're creating mock objects for unit tests, you can explicitly set
|
492
|
+
# the +pk_id+ to represent objects that already exist in the database.
|
493
|
+
def initialize(fieldHash)
|
494
|
+
@fieldHash = fieldHash
|
495
|
+
@error_messages = []
|
496
|
+
@fields = {}
|
497
|
+
@fields_set = []
|
498
|
+
check_fields = LafcadioConfig.new()['checkFields']
|
499
|
+
verify if %w( onInstantiate onAllStates ).include?( check_fields )
|
100
500
|
end
|
101
501
|
|
102
|
-
|
103
|
-
|
502
|
+
# Returns a clone, with all of the fields copied.
|
503
|
+
def clone
|
504
|
+
copy = super
|
505
|
+
copy.fields = @fields.clone
|
506
|
+
copy.fields_set = @fields_set.clone
|
507
|
+
copy
|
104
508
|
end
|
105
509
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
510
|
+
# Commits this domain object to the database.
|
511
|
+
def commit
|
512
|
+
ObjectStore.get_object_store.commit self
|
513
|
+
end
|
514
|
+
|
515
|
+
# Set the delete value to true if you want this domain object to be deleted
|
516
|
+
# from the database during its next commit.
|
517
|
+
def delete=(value)
|
518
|
+
if value && !pk_id
|
519
|
+
raise "No point deleting an object that's not already in the DB"
|
520
|
+
end
|
521
|
+
@delete = value
|
522
|
+
end
|
523
|
+
|
524
|
+
def get_field( field ) #:nodoc:
|
525
|
+
unless @fields_set.include?( field )
|
526
|
+
set_field( field, @fieldHash[field.name] )
|
527
|
+
end
|
528
|
+
@fields[field.name]
|
529
|
+
end
|
530
|
+
|
531
|
+
def get_getter_field( methId ) #:nodoc:
|
532
|
+
begin
|
533
|
+
self.class.get_field( methId.id2name )
|
534
|
+
rescue MissingError
|
535
|
+
nil
|
536
|
+
end
|
537
|
+
end
|
538
|
+
|
539
|
+
def get_setter_field( methId ) #:nodoc:
|
540
|
+
if methId.id2name =~ /(.*)=$/
|
541
|
+
begin
|
542
|
+
self.class.get_field( $1 )
|
543
|
+
rescue MissingError
|
544
|
+
nil
|
545
|
+
end
|
546
|
+
else
|
547
|
+
nil
|
548
|
+
end
|
549
|
+
end
|
550
|
+
|
551
|
+
def method_missing( methId, *args ) #:nodoc:
|
552
|
+
if ( field = get_setter_field( methId ) )
|
553
|
+
set_field( field, args.first )
|
554
|
+
elsif ( field = get_getter_field( methId ) )
|
555
|
+
get_field( field )
|
556
|
+
else
|
557
|
+
super( methId, *args )
|
558
|
+
end
|
559
|
+
end
|
560
|
+
|
561
|
+
# Returns the subclass of DomainObject that this instance represents.
|
562
|
+
# Because of the way that proxying works, clients should call this method
|
563
|
+
# instead of Object.class.
|
564
|
+
def object_type
|
565
|
+
self.class.object_type
|
566
|
+
end
|
567
|
+
|
568
|
+
# This template method is called before every commit. Subclasses can
|
569
|
+
# override it to ensure code is executed before a commit.
|
570
|
+
def pre_commit_trigger
|
571
|
+
nil
|
572
|
+
end
|
573
|
+
|
574
|
+
# This template method is called after every commit. Subclasses can
|
575
|
+
# override it to ensure code is executed after a commit.
|
576
|
+
def post_commit_trigger
|
577
|
+
nil
|
578
|
+
end
|
579
|
+
|
580
|
+
def set_field( field, value ) #:nodoc:
|
581
|
+
if field.class <= LinkField
|
582
|
+
if value.class != DomainObjectProxy && value
|
583
|
+
value = DomainObjectProxy.new(value)
|
584
|
+
end
|
585
|
+
end
|
586
|
+
if ( LafcadioConfig.new()['checkFields'] == 'onAllStates' &&
|
587
|
+
!field.instance_of?( PrimaryKeyField ) )
|
588
|
+
field.verify( value, pk_id )
|
589
|
+
end
|
590
|
+
@fields[field.name] = value
|
591
|
+
@fields_set << field
|
592
|
+
end
|
593
|
+
|
594
|
+
def verify
|
595
|
+
self.class.get_class_fields.each { |field|
|
596
|
+
field.verify( self.send( field.name ), self.pk_id )
|
597
|
+
}
|
598
|
+
end
|
599
|
+
end
|
600
|
+
|
601
|
+
# Any domain class that is used mostly to map between two other domain
|
602
|
+
# classes should be a subclass of MapObject. Subclasses of MapObject should
|
603
|
+
# override MapObject.mappedTypes, returning a two-element array containing
|
604
|
+
# the domain classes that the map object maps between.
|
605
|
+
class MapObject < DomainObject
|
606
|
+
def self.other_mapped_type(firstType) #:nodoc:
|
607
|
+
types = mappedTypes
|
608
|
+
if types.index(firstType) == 0
|
609
|
+
types[1]
|
610
|
+
else
|
611
|
+
types[0]
|
612
|
+
end
|
613
|
+
end
|
614
|
+
|
615
|
+
def self.subsidiary_map #:nodoc:
|
616
|
+
nil
|
117
617
|
end
|
118
618
|
end
|
119
619
|
end
|