class NestedRecord::Collection

Attributes

record_class[R]

Public Class Methods

new() click to toggle source
# File lib/nested_record/collection.rb, line 10
def initialize
  @ary = []
end

Public Instance Methods

<<(obj) click to toggle source
# 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) click to toggle source
# File lib/nested_record/collection.rb, line 31
def ==(other)
  @ary == other.to_ary
end
as_json() click to toggle source
# File lib/nested_record/collection.rb, line 18
def as_json
  @ary.as_json
end
build(attributes = {}) { |obj| ... } click to toggle source
# 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() click to toggle source
# File lib/nested_record/collection.rb, line 58
def clear
  @ary.clear
  self
end
each() { |obj| ... } click to toggle source
# File lib/nested_record/collection.rb, line 22
def each
  return to_enum(:each) unless block_given?
  @ary.each { |obj| yield obj }
end
empty?() click to toggle source
# File lib/nested_record/collection.rb, line 54
def empty?
  @ary.empty?
end
exists?(attrs) click to toggle source
# File lib/nested_record/collection.rb, line 114
def exists?(attrs)
  attrs = attrs.stringify_keys
  any? { |obj| obj.match?(attrs) }
end
find_by(attrs) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/nested_record/collection.rb, line 14
def initialize_dup(orig)
  @ary = orig.to_ary
end
inspect() click to toggle source
# File lib/nested_record/collection.rb, line 50
def inspect
  @ary.inspect
end
length() click to toggle source
# File lib/nested_record/collection.rb, line 63
def length
  @ary.length
end
reject!() { |obj| ... } click to toggle source
# 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) click to toggle source
# File lib/nested_record/collection.rb, line 89
def reject_by!(attrs)
  attrs = attrs.stringify_keys
  reject! { |obj| obj.match?(attrs) }
end
select() { |obj| ... } click to toggle source
# 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!() { |obj| ... } click to toggle source
# 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() click to toggle source
# File lib/nested_record/collection.rb, line 67
def size
  @ary.size
end
sort_by!() { |obj| ... } click to toggle source
# 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() click to toggle source
# File lib/nested_record/collection.rb, line 27
def to_ary
  @ary.dup
end

Private Instance Methods

record_class() click to toggle source
# File lib/nested_record/collection.rb, line 131
def record_class
  self.class.record_class
end