Class: Lafcadio::DomainObjectField
- Inherits:
-
ObjectField
- Object
- ObjectField
- Lafcadio::DomainObjectField
- Defined in:
- lib/lafcadio/objectField.rb,
lib/lafcadio/test.rb
Overview
A DomainObjectField is used to link from one domain class to another. To add such an association in a class definition, call DomainObject.domain_object:
class Invoice < Lafcadio::DomainObject
domain_object Client
end
By default, the field name is assumed to be the same as the class name, only lower-cased and camel-case.
class LineItem < Lafcadio::DomainObject
domain_object Product # field name 'product'
domain_object CatalogOrder # field name 'catalog_order'
end
The field name can be explicitly set as the 2nd argument of DomainObject.domain_object.
class Message < Lafcadio::DomainObject
domain_object User, 'sender'
domain_object User, 'recipient'
end
Setting delete_cascade
to true means that if the domain object being associated to is deleted, this domain object will also be deleted.
class Invoice < Lafcadio::DomainObject
domain_object Client, 'client', { 'delete_cascade' => true }
end
cli = Client.new( 'name' => 'big company' ).commit
inv = Invoice.new( 'client' => cli ).commit
cli.delete!
inv_prime = Invoice[inv.pk_id] # => will raise DomainObjectNotFoundError
Direct Known Subclasses
Instance Attribute Summary collapse
-
#delete_cascade ⇒ Object
Returns the value of attribute delete_cascade.
-
#linked_type ⇒ Object
readonly
Returns the value of attribute linked_type.
Attributes inherited from ObjectField
#db_field_name, #domain_class, #mock_value, #name, #not_nil
Class Method Summary collapse
-
.auto_name(linked_type) ⇒ Object
:nodoc:.
-
.create_with_args(domain_class, parameters) ⇒ Object
:nodoc:.
-
.creation_parameters(fieldElt) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#default_mock_value ⇒ Object
:nodoc:.
-
#initialize(domain_class, linked_type, name = nil, delete_cascade = false) ⇒ DomainObjectField
constructor
A new instance of DomainObjectField.
-
#value_for_sql(value) ⇒ Object
:nodoc:.
-
#value_from_sql(string) ⇒ Object
:nodoc:.
-
#verify_non_nil_value(value, pk_id) ⇒ Object
:nodoc:.
-
#verify_subset_link_field(subsetDomainObjectField, pk_id) ⇒ Object
:nodoc:.
Methods inherited from ObjectField
#<=>, #bind_write?, create_from_xml, #db_column, #db_will_automatically_write?, #prev_value, #process_before_verify, value_type, #verify
Constructor Details
#initialize(domain_class, linked_type, name = nil, delete_cascade = false) ⇒ DomainObjectField
Returns a new instance of DomainObjectField.
336 337 338 339 340 341 |
# File 'lib/lafcadio/objectField.rb', line 336 def initialize( domain_class, linked_type, name = nil, delete_cascade = false ) #:nodoc: name = self.class.auto_name( linked_type ) unless name super( domain_class, name ) ( @linked_type, @delete_cascade ) = linked_type, delete_cascade end |
Instance Attribute Details
#delete_cascade ⇒ Object
Returns the value of attribute delete_cascade.
334 335 336 |
# File 'lib/lafcadio/objectField.rb', line 334 def delete_cascade @delete_cascade end |
#linked_type ⇒ Object (readonly)
Returns the value of attribute linked_type.
333 334 335 |
# File 'lib/lafcadio/objectField.rb', line 333 def linked_type @linked_type end |
Class Method Details
.auto_name(linked_type) ⇒ Object
:nodoc:
307 308 309 |
# File 'lib/lafcadio/objectField.rb', line 307 def self.auto_name( linked_type ) #:nodoc: linked_type.basename.camel_case_to_underscore end |
.create_with_args(domain_class, parameters) ⇒ Object
:nodoc:
311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/lafcadio/objectField.rb', line 311 def self.create_with_args( domain_class, parameters ) #:nodoc: linked_type = parameters['linked_type'] instance = self.new( domain_class, linked_type, parameters['name'] || auto_name( linked_type ), parameters['delete_cascade'] ) if parameters['db_field_name'] instance.db_field_name = parameters['db_field_name'] end instance end |
.creation_parameters(fieldElt) ⇒ Object
:nodoc:
324 325 326 327 328 329 330 331 |
# File 'lib/lafcadio/objectField.rb', line 324 def self.creation_parameters( fieldElt ) #:nodoc: parameters = super( fieldElt ) linked_typeStr = fieldElt.attributes['linked_type'] parameters['linked_type'] = Class.by_name linked_typeStr parameters['delete_cascade'] = ( fieldElt.attributes['delete_cascade'] == 'y' ) parameters end |
Instance Method Details
#default_mock_value ⇒ Object
:nodoc:
306 307 308 |
# File 'lib/lafcadio/test.rb', line 306 def default_mock_value #:nodoc: DomainObjectProxy.new( linked_type, 1 ) end |
#value_for_sql(value) ⇒ Object
:nodoc:
343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/lafcadio/objectField.rb', line 343 def value_for_sql(value) #:nodoc: if value.nil? "null" elsif value.pk_id value.pk_id else raise( DomainObjectInitError, "Can't commit #{name} without pk_id", caller ) end end |
#value_from_sql(string) ⇒ Object
:nodoc:
355 356 357 |
# File 'lib/lafcadio/objectField.rb', line 355 def value_from_sql(string) #:nodoc: string ? DomainObjectProxy.new(@linked_type, string.to_i) : nil end |
#verify_non_nil_value(value, pk_id) ⇒ Object
:nodoc:
359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/lafcadio/objectField.rb', line 359 def verify_non_nil_value(value, pk_id) #:nodoc: super if @linked_type != @domain_class && pk_id subsetDomainObjectField = @linked_type.class_fields.find { |field| field.is_a?( SubsetDomainObjectField ) && field.subset_field == @name } if subsetDomainObjectField verify_subset_link_field( subsetDomainObjectField, pk_id ) end end end |
#verify_subset_link_field(subsetDomainObjectField, pk_id) ⇒ Object
:nodoc:
371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/lafcadio/objectField.rb', line 371 def verify_subset_link_field( subsetDomainObjectField, pk_id ) #:nodoc: begin prevObjLinkedTo = domain_class[pk_id].send(name) possiblyMyObj = prevObjLinkedTo.send subsetDomainObjectField.name if possiblyMyObj && possiblyMyObj.pk_id == pk_id cantChangeMsg = "You can't change that." raise FieldValueError, cantChangeMsg, caller end rescue DomainObjectNotFoundError # no previous value, so nothing to check for end end |