Class: Collection
- Inherits:
-
Object
- Object
- Collection
- Includes:
- Enumerable
- Defined in:
- lib/collection_of/collection.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(obj) ⇒ Object
- #==(other) ⇒ Object
- #[](item) ⇒ Object
- #delete(*items) ⇒ Object
- #except(*items) ⇒ Object
- #include?(item) ⇒ Boolean
-
#initialize(klass, *args) ⇒ Collection
constructor
A new instance of Collection.
- #initialize_clone ⇒ Object
- #key?(key) ⇒ Boolean (also: #has_key?)
- #keys ⇒ Object
- #new(*args, &block) ⇒ Object
- #slice(*items) ⇒ Object
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. @klass = klass.is_a?(Class) ? klass : klass.class @collection = [args.first].compact.flatten end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
8 9 10 |
# File 'lib/collection_of/collection.rb', line 8 def klass @klass end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/collection_of/collection.rb', line 8 def @options end |
Class Method Details
.[] ⇒ Object
14 |
# File 'lib/collection_of/collection.rb', line 14 alias_method :[], :new |
.of ⇒ Object
13 |
# File 'lib/collection_of/collection.rb', line 13 alias_method :of, :new |
Instance Method Details
#<<(obj) ⇒ Object
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
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_clone ⇒ Object
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?
58 59 60 |
# File 'lib/collection_of/collection.rb', line 58 def key?(key) keys.include?(key.to_sym) end |
#keys ⇒ Object
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 |