Class: Lafcadio::BooleanField

Inherits:
ObjectField show all
Defined in:
lib/lafcadio/objectField.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

* :one_zero (the default, uses integers 1 and 0)
* :capital_yes_no (uses characters 'Y' and 'N')
* :raw_true_false (uses unquoted values 'true' and 'false')

In a class definition, this would look like

class User < Lafcadio::DomainObject
  boolean 'administrator', { 'enum_type' => :capital_yes_no }
end

For more fine-grained specification you can pass a hash with the keys true and false using the argument enums.

class User < Lafcadio::DomainObject
  boolean 'administrator',
          { 'enums' => { true => 'yin', false => 'yang' } }
end

enums takes precedence over enum_type.

Instance Attribute Summary collapse

Attributes inherited from ObjectField

#db_field_name, #domain_class, #mock_value, #name, #not_nil

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ObjectField

#<=>, #bind_write?, create_from_xml, create_with_args, creation_parameters, #db_column, #db_will_automatically_write?, #default_mock_value, #prev_value, #process_before_verify, value_type, #verify, #verify_non_nil_value

Constructor Details

#initialize(domain_class, name) ⇒ BooleanField

Returns a new instance of BooleanField.



188
189
190
191
# File 'lib/lafcadio/objectField.rb', line 188

def initialize( domain_class, name )
	super( domain_class, name )
	@enums = nil
end

Instance Attribute Details

#enum_typeObject

Returns the value of attribute enum_type.



185
186
187
# File 'lib/lafcadio/objectField.rb', line 185

def enum_type
  @enum_type
end

#enums(value = nil) ⇒ Object

:nodoc:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/lafcadio/objectField.rb', line 202

def enums( value = nil ) # :nodoc:
	if @enums
		@enums
	else
		enum_type = ( @enum_type or default_enum_type )
		case enum_type
		when :one_zero
			if value.is_a?( String )
				{ true => '1', false => '0' }
			else
				{ true => 1, false => 0 }
			end
		when :capital_yes_no
			{ true => 'Y', false => 'N' }
		when :raw_true_false
			{ true => true, false => false }
		else
			raise MissingError
		end
	end
end

Class Method Details

.mock_valueObject

:nodoc:



6
7
8
# File 'lib/lafcadio/test.rb', line 6

def BooleanField.mock_value #:nodoc:
	true
end

Instance Method Details

#default_enum_typeObject



193
194
195
196
197
198
199
200
# File 'lib/lafcadio/objectField.rb', line 193

def default_enum_type
	case ObjectStore.db_type
	when 'Mysql'
		:one_zero
	when 'Pg'
		:raw_true_false
	end
end

#false_enumObject

:nodoc:



224
225
226
# File 'lib/lafcadio/objectField.rb', line 224

def false_enum # :nodoc:
	enums[false]
end

#text_enum_type?Boolean

:nodoc:

Returns:

  • (Boolean)


228
229
230
# File 'lib/lafcadio/objectField.rb', line 228

def text_enum_type? # :nodoc:
	@enums ? @enums[true].class == String : @enum_type == :capital_yes_no
end

#true_enum(value = nil) ⇒ Object

:nodoc:



232
233
234
# File 'lib/lafcadio/objectField.rb', line 232

def true_enum( value = nil ) # :nodoc:
	enums( value )[true]
end

#value_for_sql(value) ⇒ Object

:nodoc:



236
237
238
239
# File 'lib/lafcadio/objectField.rb', line 236

def value_for_sql(value) # :nodoc:
	vfs = value ? true_enum : false_enum
	text_enum_type? ? "'#{ vfs }'" : vfs
end

#value_from_sql(value) ⇒ Object

:nodoc:



241
242
243
# File 'lib/lafcadio/objectField.rb', line 241

def value_from_sql( value ) # :nodoc:
	value == true_enum( value )
end