Module: ExtendIt::ArrayOf::ArrayMethods
- Defined in:
- lib/extend_it/array_of.rb
Instance Method Summary collapse
- #<<(obj) ⇒ Object
- #[](*args) ⇒ Object
- #[]=(*args) ⇒ Object
- #fill(*args) ⇒ Object
- #initialize(*arr) ⇒ Object
- #insert(index, *other) ⇒ Object
- #map!(&block) ⇒ Object
- #scope(name) ⇒ Object
- #to_a ⇒ Object
- #to_ary ⇒ Object
Instance Method Details
#<<(obj) ⇒ Object
102 103 104 105 |
# File 'lib/extend_it/array_of.rb', line 102 def <<(obj) return unless self.class.entity?(obj) @array << obj end |
#[](*args) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/extend_it/array_of.rb', line 75 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
107 108 109 110 |
# File 'lib/extend_it/array_of.rb', line 107 def []=(*args) return unless self.class.entity?(args.last) @array.send(:[]=, *args) end |
#fill(*args) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/extend_it/array_of.rb', line 127 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
61 62 63 64 |
# File 'lib/extend_it/array_of.rb', line 61 def initialize(*arr) @array = self.class.select(arr.flatten) super(@array) end |
#insert(index, *other) ⇒ Object
140 141 142 143 |
# File 'lib/extend_it/array_of.rb', line 140 def insert(index, *other) @array.insert(index, self.class.select(other)) self end |
#map!(&block) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/extend_it/array_of.rb', line 145 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
66 67 68 69 70 71 72 73 |
# File 'lib/extend_it/array_of.rb', line 66 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_a ⇒ Object
112 113 114 |
# File 'lib/extend_it/array_of.rb', line 112 def to_a @array end |
#to_ary ⇒ Object
116 117 118 |
# File 'lib/extend_it/array_of.rb', line 116 def to_ary @array end |