Sha256: 8799071ca4cb1e8df926e31aeb9b0e9ca900525eceb0775c8eb1a593acab53ca
Contents?: true
Size: 1.16 KB
Versions: 5
Compression:
Stored size: 1.16 KB
Contents
# frozen_string_literal: true # # # Ruby Notify like class # # Ref: https://ozone.wordpress.com/category/programming/metaprogramming/ ## # Listeners implement: # # def attribute_changed(attr, old, new) # ... # end # # module SknUtils class NotifierBase def initialize @listeners = [] end def register_listener(listener) @listeners.push(listener) unless @listeners.include?(listener) end def unregister_listener(listener) @listeners.delete(listener) end # create writer-with-notify and reader def self.attribute(*attrs) attrs.each do |attr| instance_variable_set("@#{attr}", nil) define_method(attr) do instance_variable_get("@#{attr}") end define_method("#{attr}=") do |value| old_value = instance_variable_get("@#{attr}") unless (value == old_value) instance_variable_set("@#{attr}", value) @listeners.each do |listener| listener.attribute_changed(attr, old_value, value) end end end end # loop on attrs end # end of attribute method end # end of NotifierBase class end
Version data entries
5 entries across 5 versions & 1 rubygems