Module: ArrayFields
- Defined in:
- lib/arrayfields.rb
Overview
The ArrayFields module implements methods which allow an Array to be indexed by String or Symbol. It is not required to manually use this module to extend Arrays - they are auto-extended on a per-object basis when Array#fields= is called
Defined Under Namespace
Classes: FieldSet
Class Method Summary collapse
Instance Method Summary collapse
-
#[](idx, *args) ⇒ Object
methods redefined to work with fields as well as numeric indexes.
- #[]=(idx, *args) ⇒ Object
- #at(idx) ⇒ Object
- #clone ⇒ Object
- #deepcopy ⇒ Object
- #delete_at(idx) ⇒ Object
- #dup ⇒ Object
- #each_key ⇒ Object
-
#each_pair ⇒ Object
methods which give a hash-like interface.
- #each_value(*args, &block) ⇒ Object
- #each_with_field ⇒ Object
- #fetch(key) ⇒ Object
- #fill(obj, *args) ⇒ Object
- #has_key?(key) ⇒ Boolean
- #has_value?(value) ⇒ Boolean
- #indexes(*idxs) ⇒ Object
- #indices(*idxs) ⇒ Object
- #invert ⇒ Object
- #key?(key) ⇒ Boolean
- #keys ⇒ Object
- #member?(key) ⇒ Boolean
- #replace(other) ⇒ Object
- #slice(idx, *args) ⇒ Object
- #slice!(*args) ⇒ Object
- #store(key, value) ⇒ Object
- #to_h ⇒ Object
- #to_hash ⇒ Object
- #to_pairs ⇒ Object (also: #pairs)
- #update(other) ⇒ Object
- #value?(value) ⇒ Boolean
- #values ⇒ Object
- #values_at(*idxs) ⇒ Object
Class Method Details
.description ⇒ Object
11 12 13 |
# File 'lib/arrayfields.rb', line 11 def ArrayFields.description 'string/symbol keyword access to arrays' end |
.index(list) ⇒ Object
16 17 18 19 20 |
# File 'lib/arrayfields.rb', line 16 def index(list) index = list.dup ensure index.fields = list end |
.version ⇒ Object
9 |
# File 'lib/arrayfields.rb', line 9 def self.version() Arrayfields::VERSION end |
Instance Method Details
#[](idx, *args) ⇒ Object
methods redefined to work with fields as well as numeric indexes
67 68 69 70 71 72 73 74 75 |
# File 'lib/arrayfields.rb', line 67 def [] idx, *args if @fieldset and (String === idx or Symbol === idx) pos = @fieldset.pos idx return nil unless pos super(pos, *args) else super end end |
#[]=(idx, *args) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/arrayfields.rb', line 86 def []=(idx, *args) if @fieldset and (String === idx or Symbol === idx) pos = @fieldset.pos idx unless pos @fieldset.fields << idx @fieldset.fieldpos[idx] = pos = size end super(pos, *args) else super end end |
#at(idx) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/arrayfields.rb', line 98 def at idx if @fieldset and (String === idx or Symbol === idx) pos = @fieldset.pos idx return nil unless pos super pos else super end end |
#clone ⇒ Object
268 269 270 271 272 |
# File 'lib/arrayfields.rb', line 268 def clone clone = super ensure clone.fields = fields.clone end |
#deepcopy ⇒ Object
280 281 282 283 284 |
# File 'lib/arrayfields.rb', line 280 def deepcopy cp = Marshal.load(Marshal.dump(self)) cp.fields = Marshal.load(Marshal.dump(self.fields)) cp end |
#delete_at(idx) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/arrayfields.rb', line 107 def delete_at idx if @fieldset and (String === idx or Symbol === idx) pos = @fieldset.pos idx return nil unless pos new_fields = fields.dup new_fields.delete_at(pos) self.fields = new_fields super pos else super end end |
#dup ⇒ Object
274 275 276 277 278 |
# File 'lib/arrayfields.rb', line 274 def dup dup = super ensure dup.fields = fields.dup end |
#each_key ⇒ Object
170 171 172 |
# File 'lib/arrayfields.rb', line 170 def each_key @fieldset.each{|field| yield field} end |
#each_pair ⇒ Object
methods which give a hash-like interface
165 166 167 168 169 |
# File 'lib/arrayfields.rb', line 165 def each_pair each_with_index do |elem, i| yield @fieldset.fields[i], elem end end |
#each_value(*args, &block) ⇒ Object
173 174 175 |
# File 'lib/arrayfields.rb', line 173 def each_value *args, &block each(*args, &block) end |
#each_with_field ⇒ Object
157 158 159 160 161 |
# File 'lib/arrayfields.rb', line 157 def each_with_field each_with_index do |elem, i| yield elem, @fieldset.fields[i] end end |
#fetch(key) ⇒ Object
176 177 178 |
# File 'lib/arrayfields.rb', line 176 def fetch key self[key] or raise IndexError, 'key not found' end |
#fill(obj, *args) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/arrayfields.rb', line 119 def fill(obj, *args) idx = args.first if idx and @fieldset and (String === idx or Symbol === idx) idx = args.shift pos = @fieldset.pos idx super(obj, pos, *args) else super end end |
#has_key?(key) ⇒ Boolean
180 181 182 |
# File 'lib/arrayfields.rb', line 180 def has_key? key @fieldset.fields.include? key end |
#has_value?(value) ⇒ Boolean
190 191 192 193 194 195 196 197 198 |
# File 'lib/arrayfields.rb', line 190 def has_value? value if respond_to? 'include?' self.include? value else a = [] each{|val| a << val} a.include? value end end |
#indexes(*idxs) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/arrayfields.rb', line 144 def indexes(*idxs) idxs.flatten! if @fieldset idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i} end super(*idxs) end |
#indices(*idxs) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/arrayfields.rb', line 137 def indices(*idxs) idxs.flatten! if @fieldset idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i} end super(*idxs) end |
#invert ⇒ Object
259 260 261 |
# File 'lib/arrayfields.rb', line 259 def invert to_hash.invert end |
#key?(key) ⇒ Boolean
186 187 188 |
# File 'lib/arrayfields.rb', line 186 def key? key @fieldset.fields.include? key end |
#keys ⇒ Object
209 210 211 |
# File 'lib/arrayfields.rb', line 209 def keys fields end |
#member?(key) ⇒ Boolean
183 184 185 |
# File 'lib/arrayfields.rb', line 183 def member? key @fieldset.fields.include? key end |
#replace(other) ⇒ Object
256 257 258 |
# File 'lib/arrayfields.rb', line 256 def replace other Hash === other ? update(other) : super end |
#slice(idx, *args) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/arrayfields.rb', line 76 def slice idx, *args if @fieldset and (String === idx or Symbol === idx) pos = @fieldset.pos idx return nil unless pos super(pos, *args) else super end end |
#slice!(*args) ⇒ Object
152 153 154 155 156 |
# File 'lib/arrayfields.rb', line 152 def slice!(*args) ret = self[*args] self[*args] = nil ret end |
#store(key, value) ⇒ Object
212 213 214 |
# File 'lib/arrayfields.rb', line 212 def store key, value self[key] = value end |
#to_h ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/arrayfields.rb', line 238 def to_h if respond_to? 'to_ary' h = {} @fieldset.fields.zip(to_ary){|f,e| h[f] = e} h else a = [] each{|val| a << val} h = {} @fieldset.fields.zip(a){|f,e| h[f] = e} h end end |
#to_hash ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/arrayfields.rb', line 225 def to_hash if respond_to? 'to_ary' h = {} @fieldset.fields.zip(to_ary){|f,e| h[f] = e} h else a = [] each{|val| a << val} h = {} @fieldset.fields.zip(a){|f,e| h[f] = e} h end end |
#to_pairs ⇒ Object Also known as: pairs
263 264 265 |
# File 'lib/arrayfields.rb', line 263 def to_pairs fields.zip values end |
#update(other) ⇒ Object
252 253 254 255 |
# File 'lib/arrayfields.rb', line 252 def update other other.each{|k,v| self[k] = v} to_hash end |
#value?(value) ⇒ Boolean
199 200 201 202 203 204 205 206 207 |
# File 'lib/arrayfields.rb', line 199 def value? value if respond_to? 'include?' self.include? value else a = [] each{|val| a << val} a.include? value end end |
#values ⇒ Object
215 216 217 218 219 220 221 222 223 |
# File 'lib/arrayfields.rb', line 215 def values if respond_to? 'to_ary' self.to_ary else a = [] each{|val| a << val} a end end |
#values_at(*idxs) ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/arrayfields.rb', line 130 def values_at(*idxs) idxs.flatten! if @fieldset idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i} end super(*idxs) end |