Class: Lafcadio::ObjectField

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

Overview

ObjectField is the abstract base class of any field for domain objects.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objectType, name, englishName = nil) ⇒ ObjectField

objectType

The domain class that this object field belongs to.

name

The name of this field.

englishName

The descriptive English name of this field. (Deprecated)



42
43
44
45
46
47
48
49
50
# File 'lib/lafcadio/objectField/ObjectField.rb', line 42

def initialize(objectType, name, englishName = nil )
  @objectType = objectType
  @name = name
  @dbFieldName = name
  @notNull = true
  @unique = false
  ( @default, @notUniqueMsg ) = [ nil, nil ]
  @englishNameOrNil = englishName
end

Instance Attribute Details

#dbFieldNameObject

Returns the value of attribute dbFieldName.



10
11
12
# File 'lib/lafcadio/objectField/ObjectField.rb', line 10

def dbFieldName
  @dbFieldName
end

#defaultObject

Returns the value of attribute default.



10
11
12
# File 'lib/lafcadio/objectField/ObjectField.rb', line 10

def default
  @default
end

#defaultFieldNameObject (readonly)

Returns the value of attribute defaultFieldName.



9
10
11
# File 'lib/lafcadio/objectField/ObjectField.rb', line 9

def defaultFieldName
  @defaultFieldName
end

#hideDisplayObject

Returns the value of attribute hideDisplay.



10
11
12
# File 'lib/lafcadio/objectField/ObjectField.rb', line 10

def hideDisplay
  @hideDisplay
end

#hideLabelObject

Returns the value of attribute hideLabel.



10
11
12
# File 'lib/lafcadio/objectField/ObjectField.rb', line 10

def hideLabel
  @hideLabel
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/lafcadio/objectField/ObjectField.rb', line 9

def name
  @name
end

#notNullObject

Returns the value of attribute notNull.



10
11
12
# File 'lib/lafcadio/objectField/ObjectField.rb', line 10

def notNull
  @notNull
end

#notUniqueMsgObject

Returns the value of attribute notUniqueMsg.



10
11
12
# File 'lib/lafcadio/objectField/ObjectField.rb', line 10

def notUniqueMsg
  @notUniqueMsg
end

#objectTypeObject (readonly)

Returns the value of attribute objectType.



9
10
11
# File 'lib/lafcadio/objectField/ObjectField.rb', line 9

def objectType
  @objectType
end

#uniqueObject

Returns the value of attribute unique.



10
11
12
# File 'lib/lafcadio/objectField/ObjectField.rb', line 10

def unique
  @unique
end

#writeOnceObject

Returns the value of attribute writeOnce.



10
11
12
# File 'lib/lafcadio/objectField/ObjectField.rb', line 10

def writeOnce
  @writeOnce
end

Class Method Details

.instantiateFromXml(domainClass, fieldElt) ⇒ Object

:nodoc:



25
26
27
28
# File 'lib/lafcadio/objectField/ObjectField.rb', line 25

def ObjectField.instantiateFromXml( domainClass, fieldElt ) #:nodoc:
  parameters = instantiationParameters( fieldElt )
  instantiateWithParameters( domainClass, parameters )
end

.instantiateWithParameters(domainClass, parameters) ⇒ Object

:nodoc:



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

def self.instantiateWithParameters( domainClass, parameters ) #:nodoc:
  instance = self.new( domainClass, parameters['name'],
                       parameters['englishName'] )
  if ( dbFieldName = parameters['dbFieldName'] )
    instance.dbFieldName = dbFieldName
  end
  instance
end

.instantiationParameters(fieldElt) ⇒ Object

:nodoc:



17
18
19
20
21
22
23
# File 'lib/lafcadio/objectField/ObjectField.rb', line 17

def ObjectField.instantiationParameters( fieldElt ) #:nodoc:
  parameters = {}
  parameters['name'] = fieldElt.attributes['name']
  parameters['englishName'] = fieldElt.attributes['englishName']
  parameters['dbFieldName'] = fieldElt.attributes['dbFieldName']
  parameters
end

.valueTypeObject

:nodoc:



13
14
15
# File 'lib/lafcadio/objectField/ObjectField.rb', line 13

def ObjectField.valueType #:nodoc:
  Object
end

Instance Method Details

#<=>(other) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/lafcadio/objectField/ObjectField.rb', line 130

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

#bind_write?Boolean

:nodoc:

Returns:

  • (Boolean)


52
# File 'lib/lafcadio/objectField/ObjectField.rb', line 52

def bind_write?; false; end

#db_table_and_field_nameObject



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

def db_table_and_field_name
  "#{ objectType.tableName }.#{ dbFieldName }"
end

#dbWillAutomaticallyWriteObject

:nodoc:



138
139
140
# File 'lib/lafcadio/objectField/ObjectField.rb', line 138

def dbWillAutomaticallyWrite #:nodoc:
  false
end

#englishNameObject

:nodoc:



58
59
60
# File 'lib/lafcadio/objectField/ObjectField.rb', line 58

def englishName #:nodoc:
  @englishNameOrNil || English.camelCaseToEnglish(name).capitalize
end

#firstTime(fieldManager) ⇒ Object

:nodoc:



110
111
112
113
# File 'lib/lafcadio/objectField/ObjectField.rb', line 110

def firstTime(fieldManager) #:nodoc:
  pkId = fieldManager.getpkId
  pkId == nil
end

#nameForSQLObject

Returns the name that this field is referenced by in the MySQL table. By default this is the same as the name; to override it, set ObjectField#dbFieldName.



100
101
102
# File 'lib/lafcadio/objectField/ObjectField.rb', line 100

def nameForSQL
  dbFieldName
end

#nullErrorMsgObject

:nodoc:



62
63
64
# File 'lib/lafcadio/objectField/ObjectField.rb', line 62

def nullErrorMsg #:nodoc:
  English.sentence "Please enter %a %nam.", englishName.downcase
end

#prevValue(pkId) ⇒ Object

:nodoc:



115
116
117
118
# File 'lib/lafcadio/objectField/ObjectField.rb', line 115

def prevValue(pkId) #:nodoc:
  prevObject = ObjectStore.getObjectStore.get(@objectType, pkId)
  prevObject.send(name)
end

#processBeforeVerify(value) ⇒ Object

:nodoc:



120
121
122
123
# File 'lib/lafcadio/objectField/ObjectField.rb', line 120

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

#valueForSQL(value) ⇒ Object

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



106
107
108
# File 'lib/lafcadio/objectField/ObjectField.rb', line 106

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

#valueFromSQL(string) ⇒ Object

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



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

def valueFromSQL(string)
  string
end

#verify(value, pkId) ⇒ Object

:nodoc:



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lafcadio/objectField/ObjectField.rb', line 66

def verify(value, pkId) #:nodoc:
  if value.nil? && notNull
    raise FieldValueError, nullErrorMsg, caller
  end
  if value
    valueType = self.class.valueType
    unless value.class <= valueType
      raise FieldValueError, 
          "#{name} needs an object of type #{valueType.name}", caller
    end
    verifyUniqueness(value, pkId) if unique
  end
end

#verifyUniqueness(value, pkId) ⇒ Object

:nodoc:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lafcadio/objectField/ObjectField.rb', line 80

def verifyUniqueness(value, pkId) #:nodoc:
  inferrer = Query::Inferrer.new( @objectType ) { |domain_obj|
    Query.And( domain_obj.send( self.name ).equals( value ),
               domain_obj.pkId.equals( pkId ).not )
  }
  collisions = ObjectStore.getObjectStore.getSubset( inferrer.execute )
  if collisions.size > 0
    if @notUniqueMsg
      notUniqueMsg = @notUniqueMsg
    else
      notUniqueMsg = "That #{englishName.downcase} is already taken. " +
          "Please choose another."
    end
    raise FieldValueError, notUniqueMsg, caller
  end
end