Class: Audrey::Node::Array

Inherits:
Collection show all
Defined in:
lib/audrey.rb

Overview

Audrey::Node::Array

Instance Attribute Summary

Attributes inherited from Audrey::Node

#db, #pk

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Collection

#any?, #changed, #changed=, #child_pks, #clear, create_object, #length

Methods inherited from Audrey::Node

#alive?, #ancestors, #delete_object, #fco, #initialize, #isolate, #row

Constructor Details

This class inherits a constructor from Audrey::Node

Class Method Details

.new_objectObject


new_object



1161
1162
1163
# File 'lib/audrey.rb', line 1161

def self.new_object
	return([])
end

Instance Method Details

#[](idx) ⇒ Object




1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
# File 'lib/audrey.rb', line 1172

def [](idx)
	read_check()
	row = @engine.row_by_array_index(@pk, idx)
	
	# if we got a hash
	if row
		return @db.object_from_row(row)
		
	# else return nil
	else
		return nil
	end
end

#[]=(key, child) ⇒ Object


[]=



1193
1194
# File 'lib/audrey.rb', line 1193

def []=(key, child)
end

#attach_to(arr) ⇒ Object


attach_to



1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
# File 'lib/audrey.rb', line 1303

def attach_to(arr)
	# hold on to original values
	orgs = arr.clone
	
	# audrey()
	def arr.audrey(p_node = nil)
		if p_node
			@audrey_node = p_node
		end
		
		# return
		return @audrey_node
	end
	
	# NOTE: There must be a simpler way to delegate all these hash methods
	# to @audrey_node
	
	# []
	def arr.[](idx)
		return @audrey_node[idx]
	end
	
	# push
	def arr.push(val)
		return @audrey_node.push(val)
	end
	
	# each
	def arr.each()
		if block_given?
			@audrey_node.each do |k, v|
				yield k, v
			end
		else
			return @audrey_node.each
		end
	end
	
	# each_with_index
	def arr.each_with_index()
		@audrey_node.each_with_index do |el, idx|
			yield el, idx
		end
	end
	
	# length
	def arr.length()
		return @audrey_node.length
	end
	
	# any?
	def arr.any?()
		return @audrey_node.any?
	end
	
	# clear
	def arr.clear()
		return @audrey_node.clear()
	end
	
	# join
	def arr.join(sep)
		rv = []
		
		@audrey_node.each do |el|
			rv.push el
		end
		
		return rv.join(sep)
	end
	
	# include?
	def arr.include?(val)
		return @audrey_node.include?(val)
	end
	
	# initial call to audrey object
	arr.audrey self
	
	# save child elements
	orgs.each do |v|
		arr.push v
	end
end

#eachObject


each



1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
# File 'lib/audrey.rb', line 1216

def each
	# $tm.hrm
	read_check()
	rv = nil
	
	# loop through rows
	@engine.array_each(@pk) do |row|
		obj = @db.object_from_row(row)
		
		if block_given?
			yield obj
		else
			rv ||= []
			rv.push obj
		end
	end
	
	# return
	return rv
end

#each_with_indexObject



1237
1238
1239
1240
1241
1242
1243
1244
# File 'lib/audrey.rb', line 1237

def each_with_index
	idx = 0
	
	each do |el|
		yield el, idx
		idx += 1
	end
end

#include?(val) ⇒ Boolean


include? NOTE: This routine could probably be better implemented by the database engine. For now just doing it the brute force way.

Returns:

  • (Boolean)


1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
# File 'lib/audrey.rb', line 1272

def include?(val)
	# $tm.hrm
	
	# if val has a audrey object
	if val.respond_to?('audrey')
		each do |object|
			if object.respond_to?('audrey')
				if val.audrey.pk == object.audrey.pk
					return true
				end
			end
		end
	else
		each do |object|
			if val === object
				return true
			end
		end
	end
	
	# didn't find it
	return false
end

#push(child) ⇒ Object


push



1203
1204
1205
1206
1207
# File 'lib/audrey.rb', line 1203

def push(child)
	write_check()
	save_child child
	return child
end

#to_arrayObject


to_array



1253
1254
1255
1256
1257
1258
1259
1260
1261
# File 'lib/audrey.rb', line 1253

def to_array
	rv = []
	
	each do |el|
		rv.push el
	end
	
	return rv
end