Sha256: 53f5fa004433bb5de748ec581bd6a18ad74cae2f0d762c17cdbe411e847d83e6

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

module Dao
  class Conducer
    class << Conducer
      fattr(:collection_class)

      def build_collection_class!
        conducer_class = self
        collection_class = const_set(:Collection, Class.new(Collection){})
        collection_class.conducer_class = conducer_class
        conducer_class.collection_class = collection_class
      end

      def collection_for(models, *args, &block)
        collection_class.load(models, *args, &block)
      end
    end

    class Collection < ::Array
      class << Collection
        fattr(:conducer_class)

        def load(*args, &block)
          new.tap{|collection| collection.load(*args, &block)}
        end
      end

      fattr(:models)

      def conducer_class
        self.class.conducer_class
      end

      def load(models, *args, &block)
        block ||= proc{|model| conducer_class.new(model, *args) }
        (self.models = models).each{|model| self << block.call(model, *args)}
        self
      end

      def method_missing(method, *args, &block)
        return(models.send(method, *args, &block)) if models.respond_to?(method)
        super
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dao-4.4.4 lib/dao/conducer/collection.rb
dao-4.4.2 lib/dao/conducer/collection.rb