Sha256: 071c2cba92c48427e9336a595f4662f76178bfa837d47c86e22141a8140bc1d6
Contents?: true
Size: 1.37 KB
Versions: 2
Compression:
Stored size: 1.37 KB
Contents
# frozen_string_literal: true require 'attribute_extras/version' # Extra macros for auto attribute manipulation. module AttributeExtras # Parent class of the various extras. class AttributeExtra < Module attr_reader :name def initialize(name, attributes, perform) @name = name define_extra(name, attributes, perform) end def included(clazz) clazz.before_validation(name) end private def define_extra(name, attributes, perform) define_method(name) do attributes.each do |attribute| value = public_send(attribute) public_send(:"#{attribute}=", perform[self, attribute, value]) end end end end def self.define_extra(name, &perform) extra = Class.new(AttributeExtra) extra_name = name.to_s.gsub(/(?:\A|_)([a-z])/i) { $1.upcase }.to_sym AttributeExtras.const_set(extra_name, extra) ActiveRecord::Base.define_singleton_method(name) do |*attributes| include extra.new(name, attributes, perform) end end define_extra :nullify_attributes do |*, value| value.presence end define_extra :strip_attributes do |*, value| value.is_a?(String) ? value.strip : value end define_extra :truncate_attributes do |record, attribute, value| limit = record.class.columns_hash[attribute.to_s].limit value.is_a?(String) ? value[0...limit] : value end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
attribute_extras-1.0.1 | lib/attribute_extras.rb |
attribute_extras-1.0.0 | lib/attribute_extras.rb |