Class: NestedRecord::Collection
- Inherits:
-
Object
- Object
- NestedRecord::Collection
- Includes:
- Enumerable
- Defined in:
- lib/nested_record/collection.rb
Class Attribute Summary collapse
-
.record_class ⇒ Object
readonly
Returns the value of attribute record_class.
Instance Method Summary collapse
- #<<(obj) ⇒ Object
- #==(other) ⇒ Object
- #as_json ⇒ Object
- #build(attributes = {}) ⇒ Object
- #clear ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #exists?(attrs) ⇒ Boolean
- #find_by(attrs) ⇒ Object
- #find_or_initialize_by(attrs, &block) ⇒ Object
-
#initialize ⇒ Collection
constructor
A new instance of Collection.
- #initialize_dup(orig) ⇒ Object
- #inspect ⇒ Object
- #length ⇒ Object
- #reject! ⇒ Object
- #reject_by!(attrs) ⇒ Object
- #select ⇒ Object
- #select! ⇒ Object
- #size ⇒ Object
- #sort_by! ⇒ Object
- #to_ary ⇒ Object
Constructor Details
#initialize ⇒ Collection
Returns a new instance of Collection.
10 11 12 |
# File 'lib/nested_record/collection.rb', line 10 def initialize @ary = [] end |
Class Attribute Details
.record_class ⇒ Object (readonly)
Returns the value of attribute record_class.
5 6 7 |
# File 'lib/nested_record/collection.rb', line 5 def record_class @record_class end |
Instance Method Details
#<<(obj) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/nested_record/collection.rb', line 35 def <<(obj) unless obj.kind_of?(record_class) raise NestedRecord::TypeMismatchError, "#{obj.inspect} should be a #{record_class}" end @ary << obj self end |
#==(other) ⇒ Object
31 32 33 |
# File 'lib/nested_record/collection.rb', line 31 def ==(other) @ary == other.to_ary end |
#as_json ⇒ Object
18 19 20 |
# File 'lib/nested_record/collection.rb', line 18 def as_json @ary.as_json end |
#build(attributes = {}) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/nested_record/collection.rb', line 43 def build(attributes = {}) record_class.new(attributes).tap do |obj| yield obj if block_given? self << obj end end |
#clear ⇒ Object
58 59 60 61 |
# File 'lib/nested_record/collection.rb', line 58 def clear @ary.clear self end |
#each ⇒ Object
22 23 24 25 |
# File 'lib/nested_record/collection.rb', line 22 def each return to_enum(:each) unless block_given? @ary.each { |obj| yield obj } end |
#empty? ⇒ Boolean
54 55 56 |
# File 'lib/nested_record/collection.rb', line 54 def empty? @ary.empty? end |
#exists?(attrs) ⇒ Boolean
114 115 116 117 |
# File 'lib/nested_record/collection.rb', line 114 def exists?(attrs) attrs = attrs.stringify_keys any? { |obj| obj.match?(attrs) } end |
#find_by(attrs) ⇒ Object
119 120 121 122 |
# File 'lib/nested_record/collection.rb', line 119 def find_by(attrs) attrs = attrs.stringify_keys find { |obj| obj.match?(attrs) } end |
#find_or_initialize_by(attrs, &block) ⇒ Object
124 125 126 127 |
# File 'lib/nested_record/collection.rb', line 124 def find_or_initialize_by(attrs, &block) attrs = attrs.stringify_keys find_by(attrs) || build(attrs, &block) end |
#initialize_dup(orig) ⇒ Object
14 15 16 |
# File 'lib/nested_record/collection.rb', line 14 def initialize_dup(orig) @ary = orig.to_ary end |
#inspect ⇒ Object
50 51 52 |
# File 'lib/nested_record/collection.rb', line 50 def inspect @ary.inspect end |
#length ⇒ Object
63 64 65 |
# File 'lib/nested_record/collection.rb', line 63 def length @ary.length end |
#reject! ⇒ Object
77 78 79 80 81 |
# File 'lib/nested_record/collection.rb', line 77 def reject! return to_enum(:reject!) unless block_given? @ary.reject! { |obj| yield obj } self end |
#reject_by!(attrs) ⇒ Object
89 90 91 92 |
# File 'lib/nested_record/collection.rb', line 89 def reject_by!(attrs) attrs = attrs.stringify_keys reject! { |obj| obj.match?(attrs) } end |
#select ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/nested_record/collection.rb', line 94 def select if block_given? dup.select! { |obj| yield obj } else to_enum(:select) end end |
#select! ⇒ Object
71 72 73 74 75 |
# File 'lib/nested_record/collection.rb', line 71 def select! return to_enum(:select!) unless block_given? @ary.select! { |obj| yield obj } self end |
#size ⇒ Object
67 68 69 |
# File 'lib/nested_record/collection.rb', line 67 def size @ary.size end |
#sort_by! ⇒ Object
83 84 85 86 87 |
# File 'lib/nested_record/collection.rb', line 83 def sort_by! return to_enum(:sort_by!) unless block_given? @ary.sort_by! { |obj| yield obj } self end |
#to_ary ⇒ Object
27 28 29 |
# File 'lib/nested_record/collection.rb', line 27 def to_ary @ary.dup end |