Module: ExtendIt::ArrayOf::ArrayMethods

Defined in:
lib/extend_it/array_of.rb

Instance Method Summary collapse

Instance Method Details

#<<(obj) ⇒ Object



104
105
106
107
# File 'lib/extend_it/array_of.rb', line 104

def <<(obj)
  return unless self.class.entity?(obj)
  @array << obj
end

#[](*args) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/extend_it/array_of.rb', line 77

def [](*args)
  if args.size == 1 && args.first.is_a?(Symbol)
    unless self.class.finder.nil?
      return @array.find { |e| self.class.finder.call(e) == args.first }
    end
  else
  end
  @array[*args]
end

#[]=(*args) ⇒ Object



109
110
111
112
# File 'lib/extend_it/array_of.rb', line 109

def []=(*args)
  return unless self.class.entity?(args.last)
  @array.send(:[]=, *args)
end

#fill(*args) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/extend_it/array_of.rb', line 129

def fill(*args)
  if block_given?
    @array.fill(*args) do |index|
      obj = yield index
      self.class.entity?(obj) ? obj : @array[index]
    end
  else
    return self unless self.class.entity?(args.first)
    @array.fill(*args)
  end
  self
end

#initialize(*arr) ⇒ Object



63
64
65
66
# File 'lib/extend_it/array_of.rb', line 63

def initialize(*arr)
  @array = self.class.select(arr.flatten)
  super(@array)
end

#insert(index, *other) ⇒ Object



142
143
144
145
# File 'lib/extend_it/array_of.rb', line 142

def insert(index, *other)
  @array.insert(index, self.class.select(other))
  self
end

#map!(&block) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/extend_it/array_of.rb', line 147

def map!(&block)
  array = @array
  array_class = self.class
  enum = Enumerator.new do |yielder|
    array.replace(array.map do |entity|
      obj = (yielder << entity)
      array_class.entity?(obj) ? obj : nil
    end.compact)
  end
  if block_given?
    enum.each(&block)
    self
  else
    enum
  end
end

#scope(name) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/extend_it/array_of.rb', line 68

def scope(name)
  if name.is_a?(Symbol)
    if self.class.scopes.include?(name)
      self.class.new(@array.select(&self.class.scopes[name]))
    end
  else
  end
end

#to_aObject



114
115
116
# File 'lib/extend_it/array_of.rb', line 114

def to_a
  @array
end

#to_aryObject



118
119
120
# File 'lib/extend_it/array_of.rb', line 118

def to_ary
  @array
end