Class: Lafcadio::ObjectField

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/lafcadio/objectField.rb,
lib/lafcadio/test.rb

Overview

ObjectField is the abstract base class of any field for domain objects. Fields can be added to a domain class using DomainObject.string, DomainObject.integer, etc.

class User < Lafcadio::DomainObject
  string 'fname'
  date   'birthday'
end

All fields accept the following arguments in hashes:

not_nil

This is true by default. Set it to false to avoid checking for nil values in tests.

db_field_name

By default, fields are assumed to have the same name in the database, but you can override this assumption using db_field_name.

class User < Lafcadio::DomainObject
  string 'fname', { 'not_nil' => false }
  date   'birthday', { 'db_field_name' => 'bday' }
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain_class, name) ⇒ ObjectField

domain_class

The domain class that this field belongs to.

name

The name of this field.



58
59
60
61
62
63
# File 'lib/lafcadio/objectField.rb', line 58

def initialize( domain_class, name )
	@domain_class = domain_class
	@name = name
	@db_field_name = name
	@not_nil = true
end

Instance Attribute Details

#db_field_nameObject

Returns the value of attribute db_field_name.



54
55
56
# File 'lib/lafcadio/objectField.rb', line 54

def db_field_name
  @db_field_name
end

#domain_classObject (readonly)

Returns the value of attribute domain_class.



53
54
55
# File 'lib/lafcadio/objectField.rb', line 53

def domain_class
  @domain_class
end

#mock_value=(value) ⇒ Object (writeonly)

Sets the attribute mock_value

Parameters:

  • value

    the value to set the attribute mock_value to.



317
318
319
# File 'lib/lafcadio/test.rb', line 317

def mock_value=(value)
  @mock_value = value
end

#nameObject (readonly)

Returns the value of attribute name.



53
54
55
# File 'lib/lafcadio/objectField.rb', line 53

def name
  @name
end

#not_nilObject

Returns the value of attribute not_nil.



54
55
56
# File 'lib/lafcadio/objectField.rb', line 54

def not_nil
  @not_nil
end

Class Method Details

.create_from_xml(domain_class, fieldElt) ⇒ Object

:nodoc:



27
28
29
30
# File 'lib/lafcadio/objectField.rb', line 27

def self.create_from_xml( domain_class, fieldElt ) #:nodoc:
	parameters = creation_parameters( fieldElt )
	create_with_args( domain_class, parameters )
end

.create_with_args(domain_class, parameters) ⇒ Object

:nodoc:



32
33
34
35
36
37
38
# File 'lib/lafcadio/objectField.rb', line 32

def self.create_with_args( domain_class, parameters ) #:nodoc:
	instance = self.new( domain_class, parameters['name'] )
	if ( db_field_name = parameters['db_field_name'] )
		instance.db_field_name = db_field_name
	end
	instance
end

.creation_parameters(fieldElt) ⇒ Object

:nodoc:



40
41
42
43
44
45
# File 'lib/lafcadio/objectField.rb', line 40

def self.creation_parameters( fieldElt ) #:nodoc:
	parameters = {}
	parameters['name'] = fieldElt.attributes['name']
	parameters['db_field_name'] = fieldElt.attributes['db_field_name']
	parameters
end

.value_typeObject

:nodoc:



47
48
49
# File 'lib/lafcadio/objectField.rb', line 47

def self.value_type #:nodoc:
	Object
end

Instance Method Details

#<=>(other) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/lafcadio/objectField.rb', line 65

def <=>(other)
	if @domain_class == other.domain_class && name == other.name
		0
	else
		object_id <=> other.object_id
	end
end

#bind_write?Boolean

:nodoc:

Returns:

  • (Boolean)


73
74
75
# File 'lib/lafcadio/objectField.rb', line 73

def bind_write? #:nodoc:
	false
end

#db_columnObject

:nodoc:



77
78
79
# File 'lib/lafcadio/objectField.rb', line 77

def db_column #:nodoc:
	"#{ domain_class.table_name }.#{ db_field_name }"
end

#db_will_automatically_write?Boolean

:nodoc:

Returns:

  • (Boolean)


81
82
83
# File 'lib/lafcadio/objectField.rb', line 81

def db_will_automatically_write? #:nodoc:
	false
end

#default_mock_valueObject

:nodoc:



319
320
321
# File 'lib/lafcadio/test.rb', line 319

def default_mock_value #:nodoc:
	self.class.mock_value
end

#prev_value(pk_id) ⇒ Object

:nodoc:



85
86
87
88
# File 'lib/lafcadio/objectField.rb', line 85

def prev_value(pk_id) #:nodoc:
	prevObject = ObjectStore.get_object_store.get( @domain_class, pk_id )
	prevObject.send(name)
end

#process_before_verify(value) ⇒ Object

:nodoc:



90
91
92
93
# File 'lib/lafcadio/objectField.rb', line 90

def process_before_verify(value) #:nodoc:
	value = @default if value == nil
	value
end

#value_for_sql(value) ⇒ Object

Returns a string value suitable for committing this field’s value to MySQL.



97
98
99
# File 'lib/lafcadio/objectField.rb', line 97

def value_for_sql(value)
	value || 'null'
end

#value_from_sql(string) ⇒ Object

Given the SQL value string, returns a Ruby-native value.



125
126
127
# File 'lib/lafcadio/objectField.rb', line 125

def value_from_sql(string)
	string
end

#verify(value, pk_id) ⇒ Object

:nodoc:



101
102
103
104
105
106
107
108
109
110
# File 'lib/lafcadio/objectField.rb', line 101

def verify(value, pk_id) #:nodoc:
	if value.nil? && not_nil
		raise(
			FieldValueError,
			"#{ self.domain_class.name }##{ name } can not be nil.",
			caller
		)
	end
	verify_non_nil_value( value, pk_id ) if value
end

#verify_non_nil_value(value, pk_id) ⇒ Object

:nodoc:



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/lafcadio/objectField.rb', line 112

def verify_non_nil_value( value, pk_id ) #:nodoc:
	value_type = self.class.value_type
	unless value.class <= value_type
		raise(
			FieldValueError, 
			"#{ domain_class.name }##{ name } needs a " + value_type.name +
		           " value.",
			caller
		)
	end
end