Class: Lafcadio::BooleanField
- Inherits:
-
ObjectField
- Object
- ObjectField
- Lafcadio::BooleanField
- Defined in:
- lib/lafcadio/objectField/BooleanField.rb
Overview
BooleanField represents a boolean value. By default, it assumes that the table field represents True and False with the integers 1 and 0. There are two different ways to change this default.
First, BooleanField includes a few enumerated defaults. Currently there are only
* BooleanField::ENUMS_ONE_ZERO (the default, uses integers 1 and 0)
* BooleanField::ENUMS_CAPITAL_YES_NO (uses characters 'Y' and 'N')
In the XML class definition, this field would look like
<field name="field_name" class="BooleanField"
enumType="ENUMS_CAPITAL_YES_NO"/>
If you’re defining a field in Ruby, simply set BooleanField#enumType to one of the values.
For more fine-grained specification you can pass specific values in. Use this format for the XML class definition:
<field name="field_name" class="BooleanField">
<enums>
<enum key="true">yin</enum>
<enum key="false">tang</enum>
</enums>
</field>
If you’re defining the field in Ruby, set BooleanField#enums to a hash.
myBooleanField.enums = { true => 'yin', false => 'yang' }
enums
takes precedence over enumType
.
Constant Summary collapse
- ENUMS_ONE_ZERO =
0
- ENUMS_CAPITAL_YES_NO =
1
Instance Attribute Summary collapse
-
#enums ⇒ Object
Returns the value of attribute enums.
-
#enumType ⇒ Object
Returns the value of attribute enumType.
Attributes inherited from ObjectField
#dbFieldName, #default, #defaultFieldName, #hideDisplay, #hideLabel, #name, #notNull, #notUniqueMsg, #objectType, #unique, #writeOnce
Instance Method Summary collapse
-
#falseEnum ⇒ Object
:nodoc:.
-
#getEnums(value = nil) ⇒ Object
:nodoc:.
-
#initialize(objectType, name, englishName = nil) ⇒ BooleanField
constructor
A new instance of BooleanField.
-
#textEnumType ⇒ Object
:nodoc:.
-
#trueEnum(value = nil) ⇒ Object
:nodoc:.
-
#valueForSQL(value) ⇒ Object
:nodoc:.
-
#valueFromSQL(value, lookupLink = true) ⇒ Object
:nodoc:.
Methods inherited from ObjectField
#<=>, #bind_write?, #dbWillAutomaticallyWrite, #db_table_and_field_name, #englishName, #firstTime, instantiateFromXml, instantiateWithParameters, instantiationParameters, #nameForSQL, #nullErrorMsg, #prevValue, #processBeforeVerify, valueType, #verify, #verifyUniqueness
Constructor Details
#initialize(objectType, name, englishName = nil) ⇒ BooleanField
Returns a new instance of BooleanField.
36 37 38 39 40 |
# File 'lib/lafcadio/objectField/BooleanField.rb', line 36 def initialize(objectType, name, englishName = nil) super(objectType, name, englishName) @enumType = ENUMS_ONE_ZERO @enums = nil end |
Instance Attribute Details
#enums ⇒ Object
Returns the value of attribute enums.
34 35 36 |
# File 'lib/lafcadio/objectField/BooleanField.rb', line 34 def enums @enums end |
#enumType ⇒ Object
Returns the value of attribute enumType.
34 35 36 |
# File 'lib/lafcadio/objectField/BooleanField.rb', line 34 def enumType @enumType end |
Instance Method Details
#falseEnum ⇒ Object
:nodoc:
62 63 64 |
# File 'lib/lafcadio/objectField/BooleanField.rb', line 62 def falseEnum # :nodoc: getEnums[false] end |
#getEnums(value = nil) ⇒ Object
:nodoc:
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/lafcadio/objectField/BooleanField.rb', line 42 def getEnums( value = nil ) # :nodoc: if @enums @enums elsif @enumType == ENUMS_ONE_ZERO if value.class == String { true => '1', false => '0' } else { true => 1, false => 0 } end elsif @enumType == ENUMS_CAPITAL_YES_NO { true => 'Y', false => 'N' } else raise MissingError end end |
#textEnumType ⇒ Object
:nodoc:
66 67 68 |
# File 'lib/lafcadio/objectField/BooleanField.rb', line 66 def textEnumType # :nodoc: @enums ? @enums[true].class == String : @enumType == ENUMS_CAPITAL_YES_NO end |
#trueEnum(value = nil) ⇒ Object
:nodoc:
58 59 60 |
# File 'lib/lafcadio/objectField/BooleanField.rb', line 58 def trueEnum( value = nil ) # :nodoc: getEnums( value )[true] end |
#valueForSQL(value) ⇒ Object
:nodoc:
70 71 72 73 74 75 76 77 |
# File 'lib/lafcadio/objectField/BooleanField.rb', line 70 def valueForSQL(value) # :nodoc: if value vfs = trueEnum else vfs = falseEnum end textEnumType ? "'#{vfs}'" : vfs end |
#valueFromSQL(value, lookupLink = true) ⇒ Object
:nodoc:
79 80 81 |
# File 'lib/lafcadio/objectField/BooleanField.rb', line 79 def valueFromSQL(value, lookupLink = true) # :nodoc: value == trueEnum( value ) end |