Sha256: f6b5f7a6fca6f7535a7be2e61f47c4403192482ef98b5000d780ebdf11c664f5
Contents?: true
Size: 1.61 KB
Versions: 1
Compression:
Stored size: 1.61 KB
Contents
# frozen_string_literal: true module Tramway module Forms # This is the same `normalizes` feature like in Rails # https://api.rubyonrails.org/v7.1/classes/ActiveRecord/Normalization/ClassMethods.html#method-i-normalizes module Normalizations # A collection of methods that would be using in users forms module ClassMethods # :reek:BooleanParameter { enabled: false } def normalizes(*attributes, with:, apply_to_nil: false) attributes.each do |attribute| @normalizations.merge!(attribute => { proc: with, apply_to_nil: }) end end def normalizations __ancestor_normalizations.merge(@normalizations) end # :reek:ManualDispatch { enabled: false } def __ancestor_normalizations(klass = superclass) superklass = klass.superclass return {} unless superklass.respond_to?(:normalizations) klass.normalizations.merge!(__ancestor_normalizations(superklass)) end # :reek:UtilityFunction { enabled: false } def __initialize_normalizations(subclass) subclass.instance_variable_set(:@normalizations, {}) end end def self.included(base) base.extend ClassMethods end def __apply_normalizations(params) self.class.normalizations.reduce(params) do |hash, (attribute, normalization)| if hash.key?(attribute) || normalization[:apply_to_nil] hash.merge(attribute => instance_exec(hash[attribute], &normalization[:proc])) else hash end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
tramway-0.4.2 | lib/tramway/forms/normalizations.rb |