module WCC::Media::ActiveRecordShim def self.included(base) base.public_send :include, InstanceMethods base.extend ClassMethods end module InstanceMethods def attributes raw end end module ClassMethods def active_record_shim(&block) builder = ShimBuilder.new builder.instance_eval(&block) builder.apply(self) self end def find(id) WCC::Media::Client.default.public_send(endpoint).find(id) end def find_all(**filters) WCC::Media::Client.default.public_send(endpoint).list(filters) end def find_by(**filters) raise ArgumentError, "You must provide at least one filter" if filters.empty? find_all(filters).first end def model_name name end def table_name endpoint end def unscoped yield end def find_in_batches(options, &block) options = options ? options.dup : {} batch_size = options.delete(:batch_size) || 1000 filter = { limit: batch_size, offset: options.delete(:start) || 0 } find_all(filter).each_slice(batch_size, &block) end end class ShimBuilder def apply(klass) filters = (@filters || []).freeze endpoint = @endpoint || klass.name.split('::').last.downcase name = @name || klass.name.split('::').last.downcase klass.instance_eval do define_singleton_method('filters') { filters } define_singleton_method('endpoint') { endpoint } define_singleton_method('name') { name } end end [:filters, :endpoint, :name].each do |att| class_eval("def #{att}(value); @#{att} = value; end", __FILE__, __LINE__) end end end