Class: Lafcadio::EnumField
- Inherits:
-
StringField
- Object
- ObjectField
- StringField
- Lafcadio::EnumField
- Defined in:
- lib/lafcadio/objectField.rb
Overview
EnumField represents an enumerated field that can only be set to one of a set range of string values. To set the enumeration in the class definition, pass in an Array of values as enums
.
class IceCream < Lafcadio::DomainObject
enum 'flavor', { 'enums' => %w( Vanilla Chocolate Lychee ) }
end
Direct Known Subclasses
Instance Attribute Summary collapse
-
#enums ⇒ Object
readonly
Returns the value of attribute enums.
Attributes inherited from ObjectField
#db_field_name, #domain_class, #mock_value, #name, #not_nil
Class Method Summary collapse
-
.create_with_args(domain_class, parameters) ⇒ Object
:nodoc:.
-
.creation_parameters(fieldElt) ⇒ Object
:nodoc:.
-
.enum_queue_hash(fieldElt) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#initialize(domain_class, name, enums) ⇒ EnumField
constructor
:nodoc:.
-
#value_for_sql(value) ⇒ Object
:nodoc:.
-
#verify_non_nil_value(value, pk_id) ⇒ Object
:nodoc:.
Methods inherited from StringField
Methods inherited from ObjectField
#<=>, #bind_write?, create_from_xml, #db_column, #db_will_automatically_write?, #default_mock_value, #prev_value, #process_before_verify, #value_from_sql, value_type, #verify
Constructor Details
#initialize(domain_class, name, enums) ⇒ EnumField
:nodoc:
455 456 457 458 459 460 461 462 |
# File 'lib/lafcadio/objectField.rb', line 455 def initialize( domain_class, name, enums ) #:nodoc: super( domain_class, name ) if enums.class == Array @enums = QueueHash.new_from_array enums else @enums = enums end end |
Instance Attribute Details
#enums ⇒ Object (readonly)
Returns the value of attribute enums.
453 454 455 |
# File 'lib/lafcadio/objectField.rb', line 453 def enums @enums end |
Class Method Details
.create_with_args(domain_class, parameters) ⇒ Object
:nodoc:
427 428 429 |
# File 'lib/lafcadio/objectField.rb', line 427 def self.create_with_args( domain_class, parameters ) #:nodoc: self.new( domain_class, parameters['name'], parameters['enums'] ) end |
.creation_parameters(fieldElt) ⇒ Object
:nodoc:
440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/lafcadio/objectField.rb', line 440 def self.creation_parameters( fieldElt ) #:nodoc: parameters = super( fieldElt ) if fieldElt.elements['enums'][1].attributes['key'] parameters['enums'] = enum_queue_hash( fieldElt ) else parameters['enums'] = [] fieldElt.elements.each( 'enums/enum' ) { |enumElt| parameters['enums'] << enumElt.text.to_s } end parameters end |
.enum_queue_hash(fieldElt) ⇒ Object
:nodoc:
431 432 433 434 435 436 437 438 |
# File 'lib/lafcadio/objectField.rb', line 431 def self.enum_queue_hash( fieldElt ) #:nodoc: enumValues = [] fieldElt.elements.each( 'enums/enum' ) { |enumElt| enumValues << enumElt.attributes['key'] enumValues << enumElt.text.to_s } QueueHash.new( *enumValues ) end |
Instance Method Details
#value_for_sql(value) ⇒ Object
:nodoc:
464 465 466 |
# File 'lib/lafcadio/objectField.rb', line 464 def value_for_sql(value) #:nodoc: value != '' ? (super(value)) : 'null' end |
#verify_non_nil_value(value, pk_id) ⇒ Object
:nodoc:
468 469 470 471 472 473 474 475 476 477 |
# File 'lib/lafcadio/objectField.rb', line 468 def verify_non_nil_value( value, pk_id ) #:nodoc: super if @enums[value].nil? key_str = '[ ' + ( @enums.keys.map { |key| "\"#{ key }\"" } ).join(', ') + ' ]' err_str = "#{ @domain_class.name }##{ name } needs a value that is " + "one of #{ key_str }" raise( FieldValueError, err_str, caller ) end end |