hicknhack-software/rails-disco

View on GitHub
active_event/lib/active_event/support/attr_setter.rb

Summary

Maintainability
A
0 mins
Test Coverage
module ActiveEvent
  module Support
    # Allows to initialize and set attributes with a hash
    #
    # example:
    #   class RgbColor
    #     include ActiveEvent::AttrSetter
    #     attributes :r, :g, :b
    #   end
    #   green = RgbColor.new r: 250, g: 20, b: 20
    #   green.r = 255
    module AttrSetter
      extend ActiveSupport::Concern
      include ActiveEvent::Support::AttrInitializer

      # override to skip the freezing!
      def init_attributes(attributes)
        self.attributes = attributes.symbolize_keys
      end

      module ClassMethods
        def attributes(*args)
          super
          args.each do |attr|
            define_method "#{attr}=", ->(value) { attributes[attr] = value }
          end
        end
      end
    end
  end
end