Class: Array

Inherits:
Object
  • Object
show all
Includes:
ForceArray
Defined in:
lib/liquidoc.rb

Instance Method Summary collapse

Methods included from ForceArray

#force_array, #force_array!

Instance Method Details

#concatenate_property_instances(property = String) ⇒ Object



1312
1313
1314
1315
1316
1317
1318
1319
# File 'lib/liquidoc.rb', line 1312

def concatenate_property_instances property=String
  # flattens the values of instances of a given property throughout an array of Hashes
  all_arrays = []
  self.each do |i|
    all_arrays << i[property]
  end
  return all_arrays.flatten
end

#repeated_property_values(property = String) ⇒ Object



1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
# File 'lib/liquidoc.rb', line 1321

def repeated_property_values property=String
  # testing for uniqueness globally among all values in subarrays (list-formatted values) of all instances of the property across all nodes in the parent array
  # returns an array of duplicate items among all the tested arrays
  #
  # Example:
  #   array_of_hashes[0]['cue'] = ['one','two','three']
  #   array_of_hashes[1]['cue'] = ['three','four','five']
  #   array_of_hashes.duplicate_property_values('cue')
  #   #=> ['three']
  #   Due to the apperance of 'three' in both instances of cue.
  firsts = []
  dupes = []
  self.each do |node|
    return ['non-array property value present'] unless node[property].is_a? Array
    node[property].each do |i|
      dupes << i if firsts.include? i
      firsts << i
    end
  end
  return dupes
end

#to_hashObject



1292
1293
1294
1295
1296
1297
1298
# File 'lib/liquidoc.rb', line 1292

def to_hash
  struct = {}
  self.each do |p|
    struct.merge!p if p.is_a? Hash
  end
  return struct
end

#unique_property_values(property = nil) ⇒ Object

Get all unique values for each item in an array, or each unique value of a desigated

parameter in an array of hashes.


1305
1306
1307
1308
1309
1310
# File 'lib/liquidoc.rb', line 1305

def unique_property_values property=nil
  return self.uniq unless property
  new_ary = self.uniq { |i| i[property] }
  out = new_ary.map { |i| i[property] }.compact
  out
end