Class: Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/collection_of/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, *args) ⇒ Collection

Returns a new instance of Collection.



17
18
19
20
21
# 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

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/collection_of/collection.rb', line 8

def klass
  @klass
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/collection_of/collection.rb', line 8

def options
  @options
end

Class Method Details

.[]Object



14
# File 'lib/collection_of/collection.rb', line 14

alias_method :[], :new

.ofObject



13
# File 'lib/collection_of/collection.rb', line 13

alias_method :of, :new

Instance Method Details

#<<(obj) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
# 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) ⇒ Object



83
84
85
86
87
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/collection_of/collection.rb', line 33

def [](item)
  return nil if empty?

  if item.is_a?(Fixnum)
    @collection[item]
  else
    item = item.to_sym
    detect{ |i| i.name.to_sym == item }
  end
end

#delete(*items) ⇒ Object



79
80
81
# File 'lib/collection_of/collection.rb', line 79

def delete(*items)
  @collection = reject{ |i| items.include?(i.name) }
end

#except(*items) ⇒ Object



69
70
71
72
# 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) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
# 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_cloneObject



23
24
25
26
27
28
29
30
31
# 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) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


58
59
60
# File 'lib/collection_of/collection.rb', line 58

def key?(key)
  keys.include?(key.to_sym)
end

#keysObject



54
55
56
# File 'lib/collection_of/collection.rb', line 54

def keys
  @collection.map{ |i| i.name.to_sym }
end

#new(*args, &block) ⇒ Object



44
45
46
# File 'lib/collection_of/collection.rb', line 44

def new(*args, &block)
  @klass.new(*args, &block).tap{ |obj| @collection << obj }
end

#slice(*items) ⇒ Object



74
75
76
77
# 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