Attributes - Transformations¶
Modify attribute values after coercion but before validation. Perfect for normalization, formatting, and data cleanup.
Declarations¶
Symbol References¶
Reference instance methods by symbol for dynamic value transformations:
Proc or Lambda¶
Use anonymous functions for dynamic value transformations:
class CacheContent < CMDx::Task
# Proc
attribute :expire_hours, transform: proc { |v| v * 2 }
# Lambda
attribute :compression, transform: ->(v) { v.to_s.upcase.strip[0..2] }
end
Class or Module¶
Use any object that responds to call
for reusable transformation logic:
class EmailNormalizer
def call(value)
value.to_s.downcase.strip
end
end
class ProcessContacts < CMDx::Task
# Class or Module
attribute :email, transform: EmailNormalizer
# Instance
attribute :email, transform: EmailNormalizer.new
end
Validations¶
Validations run on transformed values, ensuring data consistency: