module ActiveEvent::Support::AttrInitializer
Allows to initialize attributes with a hash
example:
class RgbColor include ActiveEvent::AttrInitializer attributes :r, :g, :b end green = RgbColor.new r: 250, g: 20, b: 20
Attributes
attributes[RW]
Public Class Methods
new(*args)
click to toggle source
Calls superclass method
# File lib/active_event/support/attr_initializer.rb, line 20 def initialize(*args) hash = (args.last.is_a?(Hash) ? args.pop : {}) super check_attributes hash init_attributes hash end
Public Instance Methods
attributes_except(*args)
click to toggle source
# File lib/active_event/support/attr_initializer.rb, line 31 def attributes_except(*args) attributes.reject { |k, _| args.include? k } end
freeze()
click to toggle source
# File lib/active_event/support/attr_initializer.rb, line 27 def freeze attributes.freeze end
to_hash()
click to toggle source
# File lib/active_event/support/attr_initializer.rb, line 35 def to_hash attributes.dup end
Protected Instance Methods
init_attributes(attributes)
click to toggle source
# File lib/active_event/support/attr_initializer.rb, line 58 def init_attributes(attributes) self.attributes = attributes.symbolize_keys freeze end
Private Instance Methods
check_attributes(attributes)
click to toggle source
# File lib/active_event/support/attr_initializer.rb, line 65 def check_attributes(attributes) return if attributes.blank? if attributes.respond_to?(:permitted?) && !attributes.permitted? fail ActiveEvent::Support::ForbiddenAttributesError end (attributes.keys.map(&:to_sym) - self.class.attribute_keys).each do |k| fail ActiveEvent::Support::UnknownAttributeError, "unknown attribute: #{k}" end end