Class: SimpleMapper::Collection::Array

Inherits:
Array
  • Object
show all
Includes:
CommonMethods
Defined in:
lib/simple_mapper/collection.rb

Instance Attribute Summary

Attributes included from CommonMethods

#attribute, #change_tracking

Instance Method Summary collapse

Methods included from CommonMethods

#[]=, #build, #changed_members, #member_changed!, #simple_mapper_changes

Constructor Details

#initialize(array = []) ⇒ Array

Returns a new instance of Array.



82
83
84
# File 'lib/simple_mapper/collection.rb', line 82

def initialize(array=[])
  super(array)
end

Instance Method Details

#<<(value) ⇒ Object



99
100
101
102
# File 'lib/simple_mapper/collection.rb', line 99

def <<(value)
  member_changed!(size, value)
  super(value)
end

#delete_at(index) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/simple_mapper/collection.rb', line 134

def delete_at(index)
  original_size = size
  result = _delete(index)
  if size != original_size
    (index..original_size - 1).each {|idx| member_changed!(idx, self[idx])}
  end
  result
end

#delete_ifObject



163
164
165
166
# File 'lib/simple_mapper/collection.rb', line 163

def delete_if
  reject! {|x| yield(x)}
  self
end

#inject(*args) ⇒ Object



95
96
97
# File 'lib/simple_mapper/collection.rb', line 95

def inject(*args)
  (0..size - 1).inject(*args) {|accum, key| yield(accum, [key, self[key]])}
end

#is_member?(key) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/simple_mapper/collection.rb', line 86

def is_member?(key)
  key = key.to_i
  key >= 0 and key < size
end

#keysObject



91
92
93
# File 'lib/simple_mapper/collection.rb', line 91

def keys
  (0..size - 1).to_a
end

#push(*values) ⇒ Object



104
105
106
107
# File 'lib/simple_mapper/collection.rb', line 104

def push(*values)
  values.each {|val| self << val }
  self
end

#reject!Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/simple_mapper/collection.rb', line 143

def reject!
  first = nil
  last = size - 1
  index = 0
  while index < size
    if yield(self[index])
      first ||= index
      _delete(index)
    else
      index += 1
    end
  end
  if first
    (first..last).each {|idx| member_changed!(idx, self[idx])}
    self
  else
    nil 
  end
end

#slice!(start_or_range, length = 1) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/simple_mapper/collection.rb', line 109

def slice!(start_or_range, length=1)
  result = nil
  original_size = size
  case start_or_range
    when Range
      result = super(start_or_range)
      if result
        change_min = start_or_range.min
      end
    else
      result = super(start_or_range, length)
      if result
        change_min = start_or_range < 0 ? original_size + start_or_range : start_or_range
      end
  end
  if result
    change_min = 0 if change_min < 0
    (change_min..original_size - 1).each {|index| member_changed!(index, self[index]) }
  end
  result
end