Sha256: a1537cd221d5e71b1ad9d837167967f16e08f8738d0a3b2a2916e1f028677cae

Contents?: true

Size: 936 Bytes

Versions: 5

Compression:

Stored size: 936 Bytes

Contents

module EndecaOnDemand
  class Collection < EndecaOnDemand::Proxy

    attr_reader :klass, :target

    def initialize(klass, target, mapping = nil)
      @klass, @target = klass, target
      @target = target.map { |object| @klass.new(mapping, object) } if mapping.present?
      extend klass.collection_class if klass.respond_to?(:collection_class)
    end

    ## override proxy ##

    def class
      EndecaOnDemand::Collection
    end

    def inspect
      target.to_a.inspect
    end

    ##

    def where(conditions = {})
      target.select do |object|
        conditions.all? do |key,value|
          value.is_a?(Regexp) ? object.send(key) =~ value : object.send(key) == value
        end
      end
    end

    protected

    def wrap_collection(collection)
      EndecaOnDemand::Collection.new(klass, collection)
    end

    def method_missing(name, *args, &block)
      target.send(name, *args, &block)
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
endeca_on_demand-1.3.4 lib/endeca_on_demand/collection.rb
endeca_on_demand-1.3.3 lib/endeca_on_demand/collection.rb
endeca_on_demand-1.3.2 lib/endeca_on_demand/collection.rb
endeca_on_demand-1.3.1 lib/endeca_on_demand/collection.rb
endeca_on_demand-1.3.0 lib/endeca_on_demand/collection.rb