class Collection
Attributes
klass[R]
options[R]
Public Class Methods
new(klass, *args)
click to toggle source
# File lib/collection_of/collection.rb, line 17 def initialize(klass, *args) @options = args.extract_options! @klass = klass.is_a?(Class) ? klass : klass.class @collection = [args.first].compact.flatten end
Public Instance Methods
<<(obj)
click to toggle source
# File lib/collection_of/collection.rb, line 48 def <<(obj) checker = @options.fetch(:allow_subclasses, true) ? :is_a? : :instance_of? raise ArgumentError, "can only add #{@klass.name} objects" unless obj.send(checker, @klass) @collection << obj end
==(other)
click to toggle source
Calls superclass method
# File lib/collection_of/collection.rb, line 83 def ==(other) return other == @collection if other.is_a?(self.class) return @collection == other if other.is_a?(Array) super end
[](item)
click to toggle source
# File lib/collection_of/collection.rb, line 33 def [](item) return nil if empty? if item.is_a?(0.class) @collection[item] else item = item.to_sym detect{ |i| i.name.to_sym == item } end end
delete(*items)
click to toggle source
# File lib/collection_of/collection.rb, line 79 def delete(*items) @collection = reject{ |i| items.include?(i.name) } end
except(*items)
click to toggle source
# File lib/collection_of/collection.rb, line 69 def except(*items) items.map!(&:to_sym) self.class.new(klass, reject{ |i| items.include?(i.name.to_sym) }) end
include?(item)
click to toggle source
# File lib/collection_of/collection.rb, line 63 def include?(item) return true if @collection.include?(item) return keys.include?(item.to_sym) if item.respond_to?(:to_sym) return false end
initialize_clone(*)
click to toggle source
Calls superclass method
# File lib/collection_of/collection.rb, line 23 def initialize_clone(*) super # Clone each item in the collection @collection = @collection.inject([]) do |ary, item| ary << (item.duplicable? ? item.clone : item) ary end end
key?(key)
click to toggle source
# File lib/collection_of/collection.rb, line 58 def key?(key) keys.include?(key.to_sym) end
Also aliased as: has_key?
keys()
click to toggle source
# File lib/collection_of/collection.rb, line 54 def keys @collection.map{ |i| i.name.to_sym } end
new(*args, &block)
click to toggle source
# File lib/collection_of/collection.rb, line 44 def new(*args, &block) @klass.new(*args, &block).tap{ |obj| @collection << obj } end
slice(*items)
click to toggle source
# File lib/collection_of/collection.rb, line 74 def slice(*items) items.map!(&:to_sym) self.class.new(klass, select{ |i| items.include?(i.name.to_sym) }) end