Sha256: ac7f528d3e783499ddf08519b23c972546e3c50ffb8cbd5d7c43dd913a83fd7a

Contents?: true

Size: 1003 Bytes

Versions: 6

Compression:

Stored size: 1003 Bytes

Contents

require 'delegate'

module DBViewCTI
  module Model

    class CollectionDelegator < SimpleDelegator
      def initialize(object, target_class_name)
        super(object)
        @target_class_name = target_class_name
      end
      
      def <<(object, *args, &block)
        __getobj__.send('<<', object.try(:convert_to, @target_class_name), *args, &block)
      end

      def []=(*args, &block)
        object = args.last.convert_to(@target_class_name)
        __getobj__.send('[]=', *(args[0..-2]), object, &block)
      end

      def delete(*args, &block)
        objects = args.map do |obj|
          obj.respond_to?(:convert_to) ? obj.convert_to(@target_class_name) : obj
        end
        __getobj__.send('delete', *objects, &block)
      end

      def destroy(*args, &block)
        objects = args.map do |obj|
          obj.respond_to?(:convert_to) ? obj.convert_to(@target_class_name) : obj
        end
        __getobj__.send('delete', *objects, &block)
      end

    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dbview_cti-0.2.3 lib/db_view_cti/model/collection_delegator.rb
dbview_cti-0.2.2 lib/db_view_cti/model/collection_delegator.rb
dbview_cti-0.2.1 lib/db_view_cti/model/collection_delegator.rb
dbview_cti-0.2.0 lib/db_view_cti/model/collection_delegator.rb
dbview_cti-0.1.5 lib/db_view_cti/model/collection_delegator.rb
dbview_cti-0.1.4 lib/db_view_cti/model/collection_delegator.rb