module SugarCRM; module AttributeMethods
# Returns a hash of the module fields from the
def attributes_from_module_fields
fields = {}
self.class._module.fields.keys.sort.each do |k|
fields[k.to_s] = nil
end
fields
end
# Generates get/set methods for keys in the attributes hash
def define_attribute_methods
return if attribute_methods_generated?
@attributes.each_pair do |k,v|
self.class.module_eval %Q?
def #{k}
read_attribute :#{k}
end
def #{k}=(value)
write_attribute :#{k},value
end
?
end
self.class.attribute_methods_generated = true
end
# Returns an #inspect-like string for the value of the
# attribute +attr_name+. String attributes are elided after 50
# characters, and Date and Time attributes are returned in the
# :db format. Other attributes return the value of
# #inspect without modification.
#
# person = Person.create!(:name => "David Heinemeier Hansson " * 3)
#
# person.attribute_for_inspect(:name)
# # => '"David Heinemeier Hansson David Heinemeier Hansson D..."'
#
# person.attribute_for_inspect(:created_at)
# # => '"2009-01-12 04:48:57"'
def attribute_for_inspect(attr_name)
value = read_attribute(attr_name)
if value.is_a?(String) && value.length > 50
"#{value[0..50]}...".inspect
elsif value.is_a?(Date) || value.is_a?(Time)
%("#{value.to_s(:db)}")
else
value.inspect
end
end
protected
# Wrapper around attributes hash
def read_attribute(key)
@attributes[key]
end
# Wrapper around attributes hash
def write_attribute(key, value)
@attributes[key] = value
end
end; end