Sha256: 64af44f65fa8f3451d61dafbda57f297781375836cc54139a727b704c4e53fe2

Contents?: true

Size: 1.29 KB

Versions: 9

Compression:

Stored size: 1.29 KB

Contents

module SugarCRM; module AttributeTypeCast

  protected
  
  # Returns the attribute type for a given attribute
  def attr_type_for(attribute)
    fields = self.class._module.fields
    field  = fields[attribute]
    raise UninitializedModule, "SugarCRM::Module #{self.class._module.name} was not initialized properly (fields.length == 0)" if fields.length == 0
    raise InvalidAttribute, "#{self.class}_module.fields does not contain an entry for #{attribute} (of type: #{attribute.class})\nValid fields: #{self.class._module.fields.keys.sort.join(", ")}" if field.nil?
    raise InvalidAttributeType, "#{self.class}._module.fields[#{attribute}] does not have a key for \'type\'" if field["type"].nil?
    field["type"].to_sym
  end

  # Attempts to typecast each attribute based on the module field type
  def typecast_attributes
    @attributes.each_pair do |name,value|
      # skip primary key columns
      next if name == "id"
      attr_type = attr_type_for(name)
      case attr_type
      when :bool
        @attributes[name] = (value == "1")
      when :datetime, :datetimecombo
        begin
          @attributes[name] = DateTime.parse(value)
        rescue
          @attributes[name] = value
        end
      when :int
        @attributes[name] = value.to_i
      end
    end
    @attributes
  end

end; end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sugarcrm-0.9.9 lib/sugarcrm/attributes/attribute_typecast.rb
sugarcrm-0.9.8 lib/sugarcrm/attributes/attribute_typecast.rb
sugarcrm-0.9.7 lib/sugarcrm/attributes/attribute_typecast.rb
sugarcrm-0.9.6 lib/sugarcrm/attributes/attribute_typecast.rb
sugarcrm-0.9.5 lib/sugarcrm/attributes/attribute_typecast.rb
sugarcrm-0.9.4 lib/sugarcrm/attributes/attribute_typecast.rb
sugarcrm-0.9.3 lib/sugarcrm/attributes/attribute_typecast.rb
sugarcrm-0.9.2 lib/sugarcrm/attributes/attribute_typecast.rb
sugarcrm-0.9.1 lib/sugarcrm/attributes/attribute_typecast.rb