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
- #find_by(attrs) ⇒ Object
- #find_or_initialize_by(attrs) ⇒ 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
- #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 |
# File 'lib/nested_record/collection.rb', line 43 def build(attributes = {}) record_class.new(attributes).tap do |obj| self << obj end end |
#clear ⇒ Object
57 58 59 60 |
# File 'lib/nested_record/collection.rb', line 57 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(&proc) end |
#empty? ⇒ Boolean
53 54 55 |
# File 'lib/nested_record/collection.rb', line 53 def empty? @ary.empty? end |
#find_by(attrs) ⇒ Object
94 95 96 97 |
# File 'lib/nested_record/collection.rb', line 94 def find_by(attrs) attrs = attrs.stringify_keys find { |obj| obj.match?(attrs) } end |
#find_or_initialize_by(attrs) ⇒ Object
99 100 101 102 |
# File 'lib/nested_record/collection.rb', line 99 def find_or_initialize_by(attrs) attrs = attrs.stringify_keys find_by(attrs) || build(attrs) 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
49 50 51 |
# File 'lib/nested_record/collection.rb', line 49 def inspect @ary.inspect end |
#length ⇒ Object
62 63 64 |
# File 'lib/nested_record/collection.rb', line 62 def length @ary.length end |
#reject! ⇒ Object
76 77 78 79 80 |
# File 'lib/nested_record/collection.rb', line 76 def reject! return to_enum(:reject!) unless block_given? @ary.reject!(&proc) self end |
#reject_by!(attrs) ⇒ Object
88 89 90 91 92 |
# File 'lib/nested_record/collection.rb', line 88 def reject_by!(attrs) return to_enum(:reject_by!) unless block_given? attrs = attrs.stringify_keys reject! { |obj| obj.match?(attrs) } end |
#select! ⇒ Object
70 71 72 73 74 |
# File 'lib/nested_record/collection.rb', line 70 def select! return to_enum(:select!) unless block_given? @ary.select!(&proc) self end |
#size ⇒ Object
66 67 68 |
# File 'lib/nested_record/collection.rb', line 66 def size @ary.size end |
#sort_by! ⇒ Object
82 83 84 85 86 |
# File 'lib/nested_record/collection.rb', line 82 def sort_by! return to_enum(:sort_by!) unless block_given? @ary.sort_by!(&proc) self end |
#to_ary ⇒ Object
27 28 29 |
# File 'lib/nested_record/collection.rb', line 27 def to_ary @ary.dup end |