Sha256: 1ade857508c6d81a7618ff4a61643a97229ba989d0e19372c1ba24fd2f24b91a
Contents?: true
Size: 1.79 KB
Versions: 1
Compression:
Stored size: 1.79 KB
Contents
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) client.public_send(endpoint).find(id) end def find_all(**filters) client.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 } define_singleton_method('client=') { |client| @client = client } define_singleton_method('client') { @client || WCC::Media::Client.default } end end [:filters, :endpoint, :name].each do |att| class_eval("def #{att}(value); @#{att} = value; end", __FILE__, __LINE__) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
wcc-media-client-0.2.0 | lib/wcc/media/active_record_shim.rb |