Sha256: d237fa098a5b5020cde18e0d804f5e092af601b0fc6864551941f9909f61b90b

Contents?: true

Size: 1.17 KB

Versions: 16

Compression:

Stored size: 1.17 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

      def collection
        collection_class
      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

16 entries across 16 versions & 1 rubygems

Version Path
dao-8.0.1 lib/dao/conducer/collection.rb
dao-8.0.0 lib/dao/conducer/collection.rb
dao-7.0.0 lib/dao/conducer/collection.rb
dao-5.6.1 lib/dao/conducer/collection.rb
dao-5.5.2 lib/dao/conducer/collection.rb
dao-5.5.1 lib/dao/conducer/collection.rb
dao-5.5.0 lib/dao/conducer/collection.rb
dao-5.4.0 lib/dao/conducer/collection.rb
dao-5.3.3 lib/dao/conducer/collection.rb
dao-5.3.2 lib/dao/conducer/collection.rb
dao-5.3.0 lib/dao/conducer/collection.rb
dao-5.2.1 lib/dao/conducer/collection.rb
dao-5.1.2 lib/dao/conducer/collection.rb
dao-5.1.1 lib/dao/conducer/collection.rb
dao-5.0.0 lib/dao/conducer/collection.rb
dao-4.6.4 lib/dao/conducer/collection.rb