Module: ActiveEvent::Support::AttrInitializer

Extended by:
ActiveSupport::Concern
Included in:
EventType, AttrSetter
Defined in:
lib/active_event/support/attr_initializer.rb

Overview

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#attributes_except(*args) ⇒ Object



31
32
33
# File 'lib/active_event/support/attr_initializer.rb', line 31

def attributes_except(*args)
  attributes.reject { |k, _| args.include? k }
end

#freezeObject



27
28
29
# File 'lib/active_event/support/attr_initializer.rb', line 27

def freeze
  attributes.freeze
end

#initialize(*args) ⇒ Object



20
21
22
23
24
25
# 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

#to_hashObject



35
36
37
# File 'lib/active_event/support/attr_initializer.rb', line 35

def to_hash
  attributes.dup
end