Sha256: c8a6e353896aaaea9a3331b515c6a4159c807ddb555b5698540bf7a37518a20d

Contents?: true

Size: 1.15 KB

Versions: 14

Compression:

Stored size: 1.15 KB

Contents

class DataList
	private_class_method :new

	attr_accessor :selected_num, :objects

	def initialize(*elems)
		self.objects = elems
		@listeners = []
	end

	def add_listener(listener)
		@listeners << listener
	end

	def remove_listener(listener)
		@listeners.delete(listener)
	end

	def notify
		@listeners.each { |lst| lst.on_datalist_changed(data_table) }
	end

	def select_element(number)
		self.selected_num = number < objects.size ? number : nil
	end

	def selected_id
		objects[selected_num].id
	end

	def get_names
		self.objects.first.instance_variables.map{|v| v.to_s[1..-1]}
	end

	def table_fields(_obj)
		[]
	end

	def data_table
		result = []
		counter = 0
		objects.each do |obj|
			row = []
			row << counter
			row.push(*table_fields(obj))
			result << row
			counter += 1
		end
		DataTable.new(result)
	end

	def append(new_data)
		self.objects.append(new_data)
	end

	def replace_objects(objects)
		self.objects = objects.dup
		notify
	end

	private
	def instance_variables_wout_id(object)
		object.instance_variables.reject{|v| v.to_sym ==:@id}.map{|v| object.instance_variable_get(v)}
	end

end

Version data entries

14 entries across 14 versions & 5 rubygems

Version Path
mc_gem3-1.0.2 lib/source/Data_list.rb
mc_gem3-1.0.0 lib/source/Data_list.rb
mc_gem2-1.1.5 lib/source/Data_list.rb
mc_gem2-1.1.4 lib/source/Data_list.rb
mvcStudentXD-1.2.3 lib/source/containers/Data_list.rb
mc_gem-0.0.1 mc_gem/lib/source/containers/Data_list.rb
mvcStudentXD-1.2.2 lib/source/containers/Data_list.rb
mvcStudentXD-1.2.1 mvcStudentXD/lib/source/containers/Data_list.rb
mvcStudentXD-1.2.0 lib/source/containers/Data_list.rb
mvcStudentXD-1.1.3 lib/source/containers/Data_list.rb
mvcStudentXD-1.1.2 lib/containers/Data_list.rb
mvcStudentXD-1.1.1 lib/containers/Data_list.rb
mvcStudentXD-1.0.1 lib/Data_list.rb
model_XD-0.1.0 lib/Data_list.rb