Module: ArrayFields

Included in:
RQ::Job
Defined in:
lib/rq/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

Class Method Details

.versionObject



9
# File 'lib/rq/arrayfields.rb', line 9

def self.version() VERSION end

Instance Method Details

#[](idx, *args) ⇒ Object

methods redefined to work with fields as well as numeric indexes



54
55
56
57
58
59
60
61
62
# File 'lib/rq/arrayfields.rb', line 54

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



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rq/arrayfields.rb', line 73

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



85
86
87
88
89
90
91
92
93
# File 'lib/rq/arrayfields.rb', line 85

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

#delete_at(idx) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/rq/arrayfields.rb', line 94

def delete_at idx
  if @fieldset and (String === idx or Symbol === idx)
    pos = @fieldset.pos idx
    return nil unless pos
    super pos
  else
    super
  end
end

#each_keyObject



154
155
156
# File 'lib/rq/arrayfields.rb', line 154

def each_key
  @fieldset.each{|field| yield field}
end

#each_pairObject

methods which give a hash-like interface



149
150
151
152
153
# File 'lib/rq/arrayfields.rb', line 149

def each_pair
  each_with_index do |elem, i|
    yield @fieldset.fields[i], elem
  end
end

#each_value(*args, &block) ⇒ Object



157
158
159
# File 'lib/rq/arrayfields.rb', line 157

def each_value(*args, &block)
  each(*args, &block)
end

#each_with_fieldObject



141
142
143
144
145
# File 'lib/rq/arrayfields.rb', line 141

def each_with_field
  each_with_index do |elem, i|
    yield elem, @fieldset.fields[i]
  end
end

#fetch(key) ⇒ Object



160
161
162
# File 'lib/rq/arrayfields.rb', line 160

def fetch key
  self[key] or raise IndexError, 'key not found'
end

#fill(obj, *args) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/rq/arrayfields.rb', line 103

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

Returns:

  • (Boolean)


164
165
166
# File 'lib/rq/arrayfields.rb', line 164

def has_key? key
  @fieldset.fields.include? key
end

#has_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
177
178
179
180
181
182
# File 'lib/rq/arrayfields.rb', line 174

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



128
129
130
131
132
133
134
# File 'lib/rq/arrayfields.rb', line 128

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



121
122
123
124
125
126
127
# File 'lib/rq/arrayfields.rb', line 121

def indices(*idxs)
  idxs.flatten!
  if @fieldset
    idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i}
  end
  super(*idxs)
end

#invertObject



243
244
245
# File 'lib/rq/arrayfields.rb', line 243

def invert
  to_hash.invert
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/rq/arrayfields.rb', line 170

def key? key
  @fieldset.fields.include? key
end

#keysObject



193
194
195
# File 'lib/rq/arrayfields.rb', line 193

def keys
  fields
end

#member?(key) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/rq/arrayfields.rb', line 167

def member? key
  @fieldset.fields.include? key
end

#replace(other) ⇒ Object



240
241
242
# File 'lib/rq/arrayfields.rb', line 240

def replace other
  Hash === other ? update(other) : super
end

#slice(idx, *args) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/rq/arrayfields.rb', line 63

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



136
137
138
139
140
# File 'lib/rq/arrayfields.rb', line 136

def slice!(*args)
  ret = self[*args]
  self[*args] = nil
  ret
end

#store(key, value) ⇒ Object



196
197
198
# File 'lib/rq/arrayfields.rb', line 196

def store key, value
  self[key] = value
end

#to_hObject



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rq/arrayfields.rb', line 222

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_hashObject



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rq/arrayfields.rb', line 209

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

#update(other) ⇒ Object



236
237
238
239
# File 'lib/rq/arrayfields.rb', line 236

def update other
  other.each{|k,v| self[k] = v}
  to_hash
end

#value?(value) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
187
188
189
190
191
# File 'lib/rq/arrayfields.rb', line 183

def value? value
  if respond_to? 'include?'
    self.include? value
  else
    a = []
    each{|val| a << val}
    a.include? value
  end
end

#valuesObject



199
200
201
202
203
204
205
206
207
# File 'lib/rq/arrayfields.rb', line 199

def values
  if respond_to? 'to_ary'
    self.to_ary
  else
    a = []
    each{|val| a << val}
    a
  end
end

#values_at(*idxs) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/rq/arrayfields.rb', line 114

def values_at(*idxs)
  idxs.flatten!
  if @fieldset
    idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i}
  end
  super(*idxs)
end