Module: Heatwave::AttributeNormalizing
- Extended by:
- ActiveSupport::Concern
- Included in:
- Customer::NewCustomer, Customer::PricingProgramAssigner, Pdf::Document::PriceList, Report::BaseCommand, RoomConfiguration::CalculateQuote
- Defined in:
- app/concerns/heatwave/attribute_normalizing.rb
Overview
Lightweight replacement for Normalizr::Concern for ActiveModel::Attributes
-based service objects (Service, Report::BaseCommand, ActiveModel::API
command/result objects). ActiveRecord models already use Rails 7.1
normalizes directly; this concern fills the gap for non-AR classes that
previously relied on the normalizr gem's setter-override behaviour.
Usage mirrors the old DSL exactly:
class MyCommand
include ActiveModel::API
include ActiveModel::Attributes
include Heatwave::AttributeNormalizing
attribute :email, :string
attribute :phone, :string
normalize :email, with: %i[strip blank downcase]
normalize :phone # applies the default chain
end
with: accepts the same symbol names as Heatwave::Normalizers.chain.
Omitting it applies Heatwave::Normalizers.default (strip + blank).
ActiveSupport::Concern mixin: attribute normalizing.
Class Method Summary collapse
-
.normalize(*attrs, with: nil) ⇒ void
Defines setter overrides that normalize the given attributes on write.
Class Method Details
.normalize(*attrs, with: nil) ⇒ void
This method returns an undefined value.
Defines setter overrides that normalize the given attributes on write.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/concerns/heatwave/attribute_normalizing.rb', line 39 def normalize(*attrs, with: nil) chain = Array(with).map(&:to_sym) attrs.each do |attr| define_method("#{attr}=") do |value| normalized = if chain.empty? Heatwave::Normalizers.default(value) else Heatwave::Normalizers.chain(value, *chain) end super(normalized) end end end |