Sha256: daf8a96676304497235c821ae1fdd6e80f6dffddb5d0db106fdea9d7c7a854cc

Contents?: true

Size: 625 Bytes

Versions: 2

Compression:

Stored size: 625 Bytes

Contents

module Cubbyhole
  class Collection < Array

    def last(atts = nil)
      if atts
        all(atts).last
      else
        super()
      end
    end

    def first(atts = nil)
      if atts
        all(atts).first
      else
        super()
      end
    end

    def all(atts = nil)
      if atts
        Collection.new(where(atts))
      else
        self
      end
    end

  private

    def where(atts)
      select do |item|
        matches = true
        atts.each do |k, v|
          if item.send(k).to_s != v.to_s
            matches = false
          end
        end
        matches
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cubbyhole-0.2.1 lib/cubbyhole/collection.rb
cubbyhole-0.2.0 lib/cubbyhole/collection.rb