Module: SugarCRM::AttributeMethods

Defined in:
lib/sugarcrm/attribute_methods.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#attribute_for_inspect(attr_name) ⇒ Object

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"'


43
44
45
46
47
48
49
50
51
52
# File 'lib/sugarcrm/attribute_methods.rb', line 43

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

#define_attribute_methodsObject

Generates get/set methods for keys in the attributes hash



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sugarcrm/attribute_methods.rb', line 15

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