activematrix 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ module ActiveMatrix
5
5
  class Room
6
6
  extend ActiveMatrix::Extensions
7
7
  include ActiveMatrix::Logging
8
- include ActiveMatrix::Util::Cacheable
8
+ include ActiveMatrix::Cacheable
9
9
 
10
10
  # @!attribute [rw] event_history_limit
11
11
  # @return [Fixnum] the limit of events to keep in the event log
@@ -179,8 +179,6 @@ module ActiveMatrix
179
179
  # Return cached instance if available
180
180
  return @cached_joined_members if @cached_joined_members
181
181
 
182
- return fetch_joined_members unless cache_available?
183
-
184
182
  # Cache the raw data that can be used to reconstruct User objects
185
183
  members_data = cache.fetch(cache_key(:joined_members), expires_in: 1.hour) do
186
184
  # Convert API response to cacheable format
@@ -226,7 +224,7 @@ module ActiveMatrix
226
224
  # Return cached instance if available and no params
227
225
  return @cached_all_members if @cached_all_members && params.empty?
228
226
 
229
- return fetch_all_members(**params) if !params.empty? || client.cache == :none || !cache_available?
227
+ return fetch_all_members(**params) if !params.empty? || client.cache == :none
230
228
 
231
229
  # Cache the raw member state keys, not User objects
232
230
  members_data = cache.fetch(cache_key(:all_members), expires_in: 1.hour) do
@@ -269,7 +267,7 @@ module ActiveMatrix
269
267
  rooms = client.direct_rooms
270
268
  dirty = false
271
269
  list_for_room = (rooms[id.to_s] ||= [])
272
- if direct && !list_for_room.include?(id.to_s)
270
+ if direct && list_for_room.exclude?(id.to_s)
273
271
  list_for_room << id.to_s
274
272
  dirty = true
275
273
  elsif !direct && list_for_room.include?(id.to_s)
@@ -330,9 +328,9 @@ module ActiveMatrix
330
328
  end
331
329
 
332
330
  def room_state
333
- return ActiveMatrix::Util::StateEventCache.new self if client.cache == :none
331
+ return ActiveMatrix::StateEventCache.new self if client.cache == :none
334
332
 
335
- @room_state ||= ActiveMatrix::Util::StateEventCache.new self
333
+ @room_state ||= ActiveMatrix::StateEventCache.new self
336
334
  end
337
335
 
338
336
  # Gets a state object in the room
@@ -365,7 +363,7 @@ module ActiveMatrix
365
363
  # @param canonical_only [Boolean] Should the list of aliases only contain the canonical ones
366
364
  # @return [Array[String]] The assigned room aliases
367
365
  def aliases(canonical_only: true)
368
- return fetch_aliases(canonical_only: canonical_only) if !canonical_only || client.cache == :none || !cache_available?
366
+ return fetch_aliases(canonical_only: canonical_only) if !canonical_only || client.cache == :none
369
367
 
370
368
  cache.fetch(cache_key(:aliases), expires_in: 1.hour) do
371
369
  fetch_aliases(canonical_only: true)
@@ -506,10 +504,8 @@ module ActiveMatrix
506
504
  # @param content [Hash] The custom content of the message
507
505
  # @param msgtype [String] The type of the message, should be one of the known types (m.text, m.notice, m.emote, etc)
508
506
  def send_custom_message(body, content = {}, msgtype: nil)
509
- content.merge!(
510
- body: body,
511
- msgtype: msgtype || 'm.text'
512
- )
507
+ content[:body] = body
508
+ content[:msgtype] = msgtype || 'm.text'
513
509
 
514
510
  client.api.send_message_event(id, 'm.room.message', content)
515
511
  end
@@ -622,9 +618,9 @@ module ActiveMatrix
622
618
  end
623
619
 
624
620
  def account_data
625
- return ActiveMatrix::Util::AccountDataCache.new client, room: self if client.cache == :none
621
+ return ActiveMatrix::AccountDataCache.new client, room: self if client.cache == :none
626
622
 
627
- @account_data ||= ActiveMatrix::Util::AccountDataCache.new client, room: self
623
+ @account_data ||= ActiveMatrix::AccountDataCache.new client, room: self
628
624
  end
629
625
 
630
626
  # Retrieves a custom entry from the room-specific account data
@@ -790,7 +786,7 @@ module ActiveMatrix
790
786
  def add_alias(room_alias)
791
787
  client.api.set_room_alias(id, room_alias)
792
788
  # Clear the cache to force refresh
793
- cache.delete(cache_key(:aliases)) if cache_available?
789
+ cache.delete(cache_key(:aliases))
794
790
  true
795
791
  end
796
792
 
@@ -801,7 +797,7 @@ module ActiveMatrix
801
797
  # alias list updates.
802
798
  def reload_aliases!
803
799
  room_state.expire('m.room.canonical_alias')
804
- cache.delete(cache_key(:aliases)) if cache_available?
800
+ cache.delete(cache_key(:aliases))
805
801
  end
806
802
  alias refresh_aliases! reload_aliases!
807
803
 
@@ -981,7 +977,7 @@ module ActiveMatrix
981
977
  # @param params [Hash] other power-level params to change
982
978
  # @return [Boolean] if the change was successful
983
979
  def modify_required_power_levels(events = nil, params = {})
984
- return false if events.nil? && (params.nil? || params.empty?)
980
+ return false if events.nil? && params.blank?
985
981
 
986
982
  room_state.expire 'm.room.power_levels'
987
983
 
@@ -1005,10 +1001,6 @@ module ActiveMatrix
1005
1001
  "activematrix:room:#{id}:#{method_name}"
1006
1002
  end
1007
1003
 
1008
- def cache_available?
1009
- defined?(::Rails) && ::Rails.respond_to?(:cache) && ::Rails.cache
1010
- end
1011
-
1012
1004
  def cache
1013
1005
  ::Rails.cache
1014
1006
  end
@@ -1021,7 +1013,7 @@ module ActiveMatrix
1021
1013
  @pre_populated_members << member unless @pre_populated_members.include?(member)
1022
1014
 
1023
1015
  # Clear the cache to force a refresh on next access
1024
- cache.delete(cache_key(:joined_members)) if cache_available?
1016
+ cache.delete(cache_key(:joined_members))
1025
1017
  end
1026
1018
 
1027
1019
  def handle_room_member(event)
@@ -1038,15 +1030,15 @@ module ActiveMatrix
1038
1030
  @cached_all_members = nil
1039
1031
 
1040
1032
  # Clear the cache when membership changes
1041
- cache.delete(cache_key(:joined_members)) if cache_available?
1042
- cache.delete(cache_key(:all_members)) if cache_available?
1033
+ cache.delete(cache_key(:joined_members))
1034
+ cache.delete(cache_key(:all_members))
1043
1035
  end
1044
1036
 
1045
1037
  def handle_room_canonical_alias(event)
1046
1038
  room_state.write('m.room.canonical_alias', event[:content])
1047
1039
 
1048
1040
  # Clear the aliases cache
1049
- cache.delete(cache_key(:aliases)) if cache_available?
1041
+ cache.delete(cache_key(:aliases))
1050
1042
  end
1051
1043
 
1052
1044
  def room_handlers?
@@ -46,7 +46,7 @@ module ActiveMatrix::Rooms
46
46
 
47
47
  room = self if entry == id
48
48
  room ||= rooms.find { |r| r.id == entry }
49
- puts "Unable to find room for entry #{entry}" unless room
49
+ Rails.logger.debug { "Unable to find room for entry #{entry}" } unless room
50
50
  # next if room.nil?
51
51
 
52
52
  ret = {
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ActiveMatrix::Util
3
+ module ActiveMatrix
4
4
  class StateEventCache
5
5
  extend ActiveMatrix::Extensions
6
6
  include Enumerable
@@ -18,13 +18,11 @@ module ActiveMatrix::Util
18
18
  @cache_time = cache_time
19
19
  end
20
20
 
21
- def client
22
- @room.client
23
- end
21
+ delegate :client, to: :@room
24
22
 
25
23
  def reload!
26
24
  # Clear all cache entries for this room's state
27
- cache.delete_matched("activematrix:room:#{room.id}:state:*") if cache_available?
25
+ cache.delete_matched("activematrix:room:#{room.id}:state:*")
28
26
  end
29
27
 
30
28
  def keys
@@ -42,11 +40,11 @@ module ActiveMatrix::Util
42
40
  end
43
41
 
44
42
  def key?(type, key = nil)
45
- cache_available? && cache.exist?(cache_key(type, key))
43
+ cache.exist?(cache_key(type, key))
46
44
  end
47
45
 
48
46
  def expire(type, key = nil)
49
- cache.delete(cache_key(type, key)) if cache_available?
47
+ cache.delete(cache_key(type, key))
50
48
  end
51
49
 
52
50
  def each(live: false)
@@ -57,12 +55,12 @@ module ActiveMatrix::Util
57
55
  def delete(type, key = nil)
58
56
  type = type.to_s unless type.is_a? String
59
57
  client.api.set_room_state(room.id, type, {}, **{ state_key: key }.compact)
60
- cache.delete(cache_key(type, key)) if cache_available?
58
+ cache.delete(cache_key(type, key))
61
59
  end
62
60
 
63
61
  def [](type, key = nil)
64
62
  type = type.to_s unless type.is_a? String
65
- return fetch_state(type, key) if !cache_available? || client.cache == :none
63
+ return fetch_state(type, key) if client.cache == :none
66
64
 
67
65
  begin
68
66
  cached_value = cache.fetch(cache_key(type, key), expires_in: @cache_time) do
@@ -109,8 +107,6 @@ module ActiveMatrix::Util
109
107
  type = type.to_s unless type.is_a? String
110
108
  client.api.set_room_state(room.id, type, value, **{ state_key: key }.compact)
111
109
 
112
- return unless cache_available?
113
-
114
110
  # Convert to plain hash for caching to avoid serialization issues with Mocha
115
111
  cacheable_value = if value.is_a?(Hash)
116
112
  clean_hash = {}
@@ -131,7 +127,6 @@ module ActiveMatrix::Util
131
127
  # Alias for writing without API call
132
128
  def write(type, value, key = nil)
133
129
  type = type.to_s unless type.is_a? String
134
- return unless cache_available?
135
130
 
136
131
  # Convert to plain hash for caching to avoid serialization issues
137
132
  cacheable_value = if value.is_a?(Hash)
@@ -156,10 +151,6 @@ module ActiveMatrix::Util
156
151
  "activematrix:room:#{room.id}:state:#{type}#{"|#{key}" if key}"
157
152
  end
158
153
 
159
- def cache_available?
160
- defined?(::Rails) && ::Rails.respond_to?(:cache) && ::Rails.cache
161
- end
162
-
163
154
  def cache
164
155
  ::Rails.cache
165
156
  end
@@ -2,27 +2,19 @@
2
2
 
3
3
  require 'uri'
4
4
 
5
- module URI
6
- # A mxc:// Matrix content URL
7
- class MXC < Generic
8
- def full_path
9
- select(:host, :port, :path, :query, :fragment)
10
- .compact
11
- .join
5
+ module ActiveMatrix
6
+ module Uri
7
+ # A mxc:// Matrix content URL
8
+ class MXC < ::URI::Generic
9
+ def full_path
10
+ select(:host, :port, :path, :query, :fragment)
11
+ .compact
12
+ .join
13
+ end
12
14
  end
13
- end
14
-
15
- # TODO: Use +register_scheme+ on Ruby >=3.1 and fall back to old behavior
16
- # for older Rubies. May be removed at EOL of Ruby 3.0.
17
- if respond_to? :register_scheme
18
- register_scheme 'MXC', MXC
19
- else
20
- @@schemes['MXC'] = MXC
21
- end
22
15
 
23
- unless scheme_list.key? 'MATRIX'
24
16
  # A matrix: URI according to MSC2312
25
- class MATRIX < Generic
17
+ class MATRIX < ::URI::Generic
26
18
  attr_reader :authority, :action, :mxid, :mxid2, :via
27
19
 
28
20
  def initialize(*)
@@ -62,7 +54,7 @@ module URI
62
54
  end
63
55
 
64
56
  component = components.shift
65
- raise InvalidComponentError, "component can't be empty" if component.nil? || component.empty?
57
+ raise InvalidComponentError, "component can't be empty" if component.blank?
66
58
 
67
59
  @mxid = ActiveMatrix::MXID.new("#{sigil}#{component}")
68
60
 
@@ -74,7 +66,7 @@ module URI
74
66
  raise InvalidComponentError, 'invalid component in path'
75
67
  end
76
68
  component = components.shift
77
- raise InvalidComponentError, "component can't be empty" if component.nil? || component.empty?
69
+ raise InvalidComponentError, "component can't be empty" if component.blank?
78
70
 
79
71
  @mxid2 = ActiveMatrix::MXID.new("#{sigil2}#{component}")
80
72
  end
@@ -90,12 +82,12 @@ module URI
90
82
  end
91
83
  end
92
84
 
93
- # TODO: Use +register_scheme+ on Ruby >=3.1 and fall back to old behavior
94
- # for older Rubies. May be removed at EOL of Ruby 3.0.
95
- if respond_to? :register_scheme
96
- register_scheme 'MATRIX', MATRIX
97
- else
98
- @@schemes['MATRIX'] = MATRIX
99
- end
85
+ # Register URI schemes using modern Ruby
86
+ ::URI.register_scheme 'MXC', MXC
87
+ ::URI.register_scheme 'MATRIX', MATRIX unless ::URI.scheme_list.key?('MATRIX')
88
+
89
+ # Make them available in URI namespace for backward compatibility
90
+ ::URI.const_set(:MXC, MXC) unless ::URI.const_defined?(:MXC)
91
+ ::URI.const_set(:MATRIX, MATRIX) unless ::URI.const_defined?(:MATRIX)
100
92
  end
101
93
  end
@@ -4,7 +4,7 @@ module ActiveMatrix
4
4
  # A class for tracking information about a user on Matrix
5
5
  class User
6
6
  extend ActiveMatrix::Extensions
7
- include ActiveMatrix::Util::Cacheable
7
+ include ActiveMatrix::Cacheable
8
8
 
9
9
  attr_reader :id, :client
10
10
  alias user_id :id
@@ -140,7 +140,7 @@ module ActiveMatrix
140
140
  since = raw_presence[:last_active_ago]
141
141
  return unless since
142
142
 
143
- Time.now - (since / 1000)
143
+ Time.zone.now - (since / 1000)
144
144
  end
145
145
 
146
146
  # Gets a direct message room with the user if one exists
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveMatrix
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.4'
5
5
  end
data/lib/active_matrix.rb CHANGED
@@ -1,17 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Version is required for gem specification
3
4
  require_relative 'active_matrix/version'
4
- require_relative 'active_matrix/logging'
5
- require_relative 'active_matrix/util/extensions'
6
- require_relative 'active_matrix/util/uri'
7
- require_relative 'active_matrix/util/events'
8
- require_relative 'active_matrix/errors'
9
5
 
10
6
  require 'json'
11
7
  require 'zeitwerk'
12
8
  require 'active_support'
13
9
  require 'active_support/core_ext/integer/time'
14
10
  require 'active_support/core_ext/time/calculations'
11
+ require 'active_support/core_ext/time/zones'
15
12
  require 'active_support/core_ext/hash/keys'
16
13
  require 'active_support/core_ext/object/blank'
17
14
 
@@ -53,16 +50,43 @@ module ActiveMatrix
53
50
  end
54
51
  end
55
52
 
53
+ # Logger methods
54
+ class << self
55
+ attr_writer :logger
56
+
57
+ def logger
58
+ @logger ||= if defined?(::Rails) && ::Rails.respond_to?(:logger)
59
+ ::Rails.logger
60
+ else
61
+ require 'logger'
62
+ ::Logger.new($stdout)
63
+ end
64
+ end
65
+
66
+ def debug!
67
+ logger.level = if defined?(::Rails)
68
+ :debug
69
+ else
70
+ ::Logger::DEBUG
71
+ end
72
+ end
73
+
74
+ def global_logger?
75
+ instance_variable_defined?(:@logger)
76
+ end
77
+ end
78
+
56
79
  # Set up Zeitwerk loader
57
80
  Loader = Zeitwerk::Loader.for_gem
58
81
 
59
- # Ignore directories that shouldn't be autoloaded
82
+ # Ignore directories and files that shouldn't be autoloaded
60
83
  Loader.ignore("#{__dir__}/generators")
84
+ Loader.ignore("#{__dir__}/activematrix.rb")
61
85
 
62
86
  # Configure inflections for special cases
63
87
  Loader.inflector.inflect(
64
88
  'mxid' => 'MXID',
65
- 'uri' => 'URI',
89
+ 'uri_module' => 'Uri',
66
90
  'as' => 'AS',
67
91
  'cs' => 'CS',
68
92
  'is' => 'IS',
@@ -73,8 +97,10 @@ module ActiveMatrix
73
97
  # Setup Zeitwerk autoloading
74
98
  Loader.setup
75
99
 
76
- # Eager load all classes if in Rails eager loading mode
77
- Loader.eager_load if defined?(Rails) && Rails.respond_to?(:application) && Rails.application&.config&.eager_load
100
+ # Load core classes that are used in metaprogramming
101
+ require_relative 'active_matrix/errors'
102
+ require_relative 'active_matrix/events'
103
+ require_relative 'active_matrix/uri_module'
78
104
 
79
105
  # Load Railtie for Rails integration
80
106
  require 'active_matrix/railtie' if defined?(Rails::Railtie)
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'active_matrix'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activematrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
@@ -66,6 +66,48 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-performance
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
69
111
  - !ruby/object:Gem::Dependency
70
112
  name: simplecov
71
113
  requirement: !ruby/object:Gem::Requirement
@@ -236,17 +278,20 @@ files:
236
278
  - LICENSE.txt
237
279
  - README.md
238
280
  - lib/active_matrix.rb
281
+ - lib/active_matrix/account_data_cache.rb
239
282
  - lib/active_matrix/agent_manager.rb
240
283
  - lib/active_matrix/agent_registry.rb
241
284
  - lib/active_matrix/api.rb
242
285
  - lib/active_matrix/bot.rb
243
286
  - lib/active_matrix/bot/base.rb
244
- - lib/active_matrix/bot/main.rb
245
287
  - lib/active_matrix/bot/multi_instance_base.rb
288
+ - lib/active_matrix/cacheable.rb
246
289
  - lib/active_matrix/client.rb
247
290
  - lib/active_matrix/client_pool.rb
248
291
  - lib/active_matrix/errors.rb
249
292
  - lib/active_matrix/event_router.rb
293
+ - lib/active_matrix/events.rb
294
+ - lib/active_matrix/extensions.rb
250
295
  - lib/active_matrix/logging.rb
251
296
  - lib/active_matrix/memory.rb
252
297
  - lib/active_matrix/memory/agent_memory.rb
@@ -263,14 +308,11 @@ files:
263
308
  - lib/active_matrix/response.rb
264
309
  - lib/active_matrix/room.rb
265
310
  - lib/active_matrix/rooms/space.rb
311
+ - lib/active_matrix/state_event_cache.rb
312
+ - lib/active_matrix/uri_module.rb
266
313
  - lib/active_matrix/user.rb
267
- - lib/active_matrix/util/account_data_cache.rb
268
- - lib/active_matrix/util/cacheable.rb
269
- - lib/active_matrix/util/events.rb
270
- - lib/active_matrix/util/extensions.rb
271
- - lib/active_matrix/util/state_event_cache.rb
272
- - lib/active_matrix/util/uri.rb
273
314
  - lib/active_matrix/version.rb
315
+ - lib/activematrix.rb
274
316
  - lib/generators/active_matrix/bot/bot_generator.rb
275
317
  - lib/generators/active_matrix/bot/templates/bot.rb.erb
276
318
  - lib/generators/active_matrix/bot/templates/bot_spec.rb.erb
@@ -304,7 +346,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
304
346
  - !ruby/object:Gem::Version
305
347
  version: '0'
306
348
  requirements: []
307
- rubygems_version: 3.6.7
349
+ rubygems_version: 3.6.9
308
350
  specification_version: 4
309
351
  summary: Rails gem for connecting to Matrix protocol
310
352
  test_files: []
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveMatrix::Bot
4
- PARAMS_CONFIG = {} # rubocop:disable Style/MutableConstant Intended
5
-
6
- require 'optparse'
7
- parser = OptionParser.new do |op|
8
- op.on('-s homeserver', 'Specify homeserver') { |val| PARAMS_CONFIG[:homeserver] = val }
9
-
10
- op.on('-T token', 'Token') { |val| PARAMS_CONFIG[:access_token] = val }
11
- op.on('-U username', 'Username') { |val| PARAMS_CONFIG[:username] = val }
12
- op.on('-P password', 'Password') { |val| PARAMS_CONFIG[:password] = val }
13
-
14
- op.on('-q', 'Disable logging') { PARAMS_CONFIG[:logging] = false }
15
- op.on('-v', 'Enable verbose output') { PARAMS_CONFIG[:logging] = !(PARAMS_CONFIG[:log_level] = :debug).nil? }
16
- end
17
-
18
- begin
19
- parser.parse!(ARGV.dup)
20
- rescue StandardError => e
21
- PARAMS_CONFIG[:optparse_error] = e
22
- end
23
-
24
- ActiveMatrix.logger.appenders.each do |log|
25
- log.layout = Logging::Layouts.pattern(
26
- pattern: "%d|%.1l %c : %m\n"
27
- )
28
- end
29
- ActiveMatrix.debug! if ENV['MATRIX_DEBUG'] == '1'
30
-
31
- class Instance < Base
32
- set :logging, true
33
- set :log_level, :info
34
-
35
- set :app_file, caller_files.first || $PROGRAM_NAME
36
- set(:run) { File.expand_path($PROGRAM_NAME) == File.expand_path(app_file) }
37
-
38
- if run? && ARGV.any?
39
- error = PARAMS_CONFIG.delete(:optparse_error)
40
- raise error if error
41
-
42
- PARAMS_CONFIG.each { |k, v| set k, v }
43
- end
44
- end
45
-
46
- module Delegator
47
- def self.delegate(*methods)
48
- methods.each do |method_name|
49
- define_method(method_name) do |*args, &block|
50
- return super(*args, &block) if respond_to? method_name
51
-
52
- Delegator.target.send(method_name, *args, &block)
53
- end
54
- # ensure keyword argument passing is compatible with ruby >= 2.7
55
- ruby2_keywords(method_name) if respond_to?(:ruby2_keywords, true)
56
- private method_name
57
- end
58
- end
59
-
60
- delegate :command, :client, :event,
61
- :settings,
62
- :set, :enable, :disable
63
-
64
- class << self
65
- attr_accessor :target
66
- end
67
-
68
- self.target = Instance
69
- end
70
-
71
- # Trigger the global instance to run once the main class finishes
72
- at_exit do
73
- remove_const(:PARAMS_CONFIG)
74
- Instance.run! if $!.nil? && Instance.run? # rubocop:disable Style/SpecialGlobalVars Don't want to require into global scope
75
- end
76
- end
77
-
78
- extend ActiveMatrix::Bot::Delegator # rubocop:disable Style/MixinUsage Intended
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveMatrix
4
- module Util
5
- # Provides caching functionality for Matrix objects
6
- # Handles serialization/deserialization to work with Rails.cache
7
- module Cacheable
8
- extend ActiveSupport::Concern
9
-
10
- included do
11
- attr_accessor :cached_at
12
- end
13
-
14
- class_methods do
15
- # Reconstruct object from cached data
16
- def from_cache(client, data)
17
- return nil unless data.is_a?(Hash) && data[:_cache_class] == name
18
-
19
- # Remove cache metadata
20
- attrs = data.except(:_cache_class, :_cached_at)
21
-
22
- # Reconstruct based on class type
23
- case name
24
- when 'ActiveMatrix::User'
25
- new(client, attrs[:id], attrs.except(:id))
26
- when 'ActiveMatrix::Room'
27
- new(client, attrs[:id], attrs.except(:id))
28
- else
29
- new(client, attrs)
30
- end
31
- end
32
- end
33
-
34
- # Convert object to cacheable hash
35
- def to_cache
36
- data = cache_attributes.merge(
37
- _cache_class: self.class.name,
38
- _cached_at: Time.current
39
- )
40
-
41
- # Ensure we only cache serializable data
42
- data.deep_stringify_keys
43
- end
44
-
45
- # Override in each class to specify what to cache
46
- def cache_attributes
47
- if respond_to?(:attributes)
48
- attributes
49
- elsif respond_to?(:to_h)
50
- to_h
51
- else
52
- {}
53
- end
54
- end
55
-
56
- # Generate a cache key for this object
57
- def cache_key(*suffixes)
58
- base_key = "#{self.class.name.underscore}:#{cache_id}"
59
- suffixes.any? ? "#{base_key}:#{suffixes.join(':')}" : base_key
60
- end
61
-
62
- # Override in each class if ID method is different
63
- def cache_id
64
- respond_to?(:id) ? id : object_id
65
- end
66
-
67
- # Check if this object was loaded from cache
68
- def from_cache?
69
- @cached_at.present?
70
- end
71
- end
72
- end
73
- end