Module: Enumerable

Included in:
Hashay
Defined in:
lib/kiss/ext/core.rb

Instance Method Summary collapse

Instance Method Details

#conjoin(conjunction = 'and') ⇒ Object Also known as: join_with_conjunction



261
262
263
264
265
266
267
268
269
# File 'lib/kiss/ext/core.rb', line 261

def conjoin(conjunction = 'and')
  size = self.size
  return join if size < 2
  return join(" #{conjunction} ") if size == 2

  array = clone
  last = array.pop
  "#{array.join(', ')}, #{conjunction} #{last}"
end

#hash_arrays_by_idObject



234
235
236
# File 'lib/kiss/ext/core.rb', line 234

def hash_arrays_by_id
  hash_arrays_by_key(:id)
end

#hash_arrays_by_key(key) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/kiss/ext/core.rb', line 213

def hash_arrays_by_key(key)
  hash = {}
  each do |item|
    (hash[item[key]] ||= []) << item
  end
  hash
end

#hash_arrays_by_keys(*keys) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/kiss/ext/core.rb', line 221

def hash_arrays_by_keys(*keys)
  last_key = keys.pop
  hash = {}
  each do |item|
    h = hash
    keys.each do |key|
      h = (h[item[key]] ||= {})
    end
    (h[item[last_key]] ||= []) << item
  end
  hash
end

#hash_by_idObject



186
187
188
# File 'lib/kiss/ext/core.rb', line 186

def hash_by_id
  hash_by_key(:id)
end

#hash_by_key(key) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/kiss/ext/core.rb', line 163

def hash_by_key(key)
  hash = {}
  each do |item|
    hash[item[key]] = item
  end
  hash
end

#hash_by_keys(*keys) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/kiss/ext/core.rb', line 171

def hash_by_keys(*keys)
  last_key = keys.pop
  raise 'no keys given' unless last_key
  
  hash = {}
  each do |item|
    h = hash
    keys.each do |key|
      h = (h[item[key]] ||= {})
    end
    h[item[last_key]] = item
  end
  hash
end

#hash_pairsObject



249
250
251
# File 'lib/kiss/ext/core.rb', line 249

def hash_pairs
  Hash[*self]
end

#hash_values_by_key(value_key, hash_key) ⇒ Object



190
191
192
193
194
195
196
# File 'lib/kiss/ext/core.rb', line 190

def hash_values_by_key(value_key, hash_key)
  hash = {}
  each do |item|
    hash[item[hash_key]] = item[value_key]
  end
  hash
end

#hash_values_by_keys(value_key, *hash_keys) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/kiss/ext/core.rb', line 198

def hash_values_by_keys(value_key, *hash_keys)
  last_key = hash_keys.pop
  raise 'no keys given' unless last_key
  
  hash = {}
  each do |item|
    h = hash
    hash_keys.each do |key|
      h = (h[item[key]] ||= {})
    end
    h[item[hash_key]] = item[value_key]
  end
  hash
end

#hash_with_keys(*args) ⇒ Object



238
239
240
241
242
243
244
245
246
247
# File 'lib/kiss/ext/core.rb', line 238

def hash_with_keys(*args)
  first_arg = args.first
  args = first_arg if first_arg.is_a?(Array)
  
  hash = {}
  (0...length).each do |i|
    hash[args[i]] = self[i]
  end
  hash
end

#idsObject



253
254
255
# File 'lib/kiss/ext/core.rb', line 253

def ids
  map {|item| item.id }
end

#join_unless_empty(delimiter, none = 'None') ⇒ Object



257
258
259
# File 'lib/kiss/ext/core.rb', line 257

def join_unless_empty(delimiter, none = 'None')
  self.empty? ? none : self.join(delimiter)
end

#to_table(*columns) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/kiss/ext/core.rb', line 272

def to_table(*columns)
  options = columns.last.is_a?(Hash) ? columns.pop : {}
  raise 'no columns specified' if columns.empty?
  
  column_delimiter = options[:column_delimiter] || "\t"
  line_delimiter = options[:line_delimiter] || "\r\n"
  
  [
    columns.map do |column|
      column = column[0] if column.is_a?(Array)
      column.is_a?(String) ? column : column.to_s.titlecase
    end.flatten.join(column_delimiter),
  
    *(map do |object|
      columns.map do |column|
        column = column[1] if column.is_a?(Array)
        column.is_a?(Proc) ? column.call(object) : object[column]
      end.flatten.join(column_delimiter)
    end + [nil])
  ].join(line_delimiter)
end