Module: MatrixSdk::Extensions

Included in:
Api, Client, Event, Room, User
Defined in:
lib/matrix_sdk/util/extensions.rb

Instance Method Summary collapse

Instance Method Details

#events(*symbols) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/matrix_sdk/util/extensions.rb', line 13

def events(*symbols)
  module_name = "#{name}Events"

  initializers = []
  readers = []
  methods = []

  symbols.each do |sym|
    name = sym.to_s

    initializers << "
      @on_#{name} = MatrixSdk::EventHandlerArray.new
    "
    readers << ":on_#{name}"
    methods << "
      def fire_#{name}(ev, filter = nil)
        @on_#{name}.fire(ev, filter)
        when_#{name}(ev) if !ev.handled?
      end

      def when_#{name}(ev); end
    "
  end

  class_eval "
    module #{module_name}
      attr_reader #{readers.join ', '}

      def event_initialize
        #{initializers.join}
      end

      #{methods.join}
    end

    include #{module_name}
  ", __FILE__, __LINE__ - 12
end

#ignore_inspect(*symbols) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/matrix_sdk/util/extensions.rb', line 52

def ignore_inspect(*symbols)
  class_eval %*
    def inspect
      reentrant = caller_locations.any? { |l| l.absolute_path == __FILE__ && l.label == 'inspect' }
      "\\\#<\#{self.class} \#{instance_variables
        .reject { |f| %i[#{symbols.map { |s| "@#{s}" }.join ' '}].include? f }
        .map { |f| "\#{f}=\#{reentrant ? instance_variable_get(f) : instance_variable_get(f).inspect}" }.join " " }}>"
    end
  *, __FILE__, __LINE__ - 7
end