Class: NestedRecord::Collection

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

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollection

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_classObject (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_jsonObject



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

#clearObject



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

def clear
  @ary.clear
  self
end

#eachObject



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

Returns:

  • (Boolean)


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

def empty?
  @ary.empty?
end

#exists?(attrs) ⇒ Boolean

Returns:

  • (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

#inspectObject



50
51
52
# File 'lib/nested_record/collection.rb', line 50

def inspect
  @ary.inspect
end

#lengthObject



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

#selectObject



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

#sizeObject



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_aryObject



27
28
29
# File 'lib/nested_record/collection.rb', line 27

def to_ary
  @ary.dup
end