Module: ClassMethods

Included in:
ActiveCSV
Defined in:
lib/active_csv/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#allObject



2
3
4
5
6
7
8
# File 'lib/active_csv/class_methods.rb', line 2

def all
	objects = Array.new
	db_file = DbFile.new("db/"+model_name_+".csv")
	attr_array = db_file.csv_content
	objects = ar_to_obj(attr_array)
	objects
end

#ar_to_obj(attr_array) ⇒ Object

turns array of arrays into array of objects



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_csv/class_methods.rb', line 10

def ar_to_obj(attr_array) # turns array of arrays into array of objects
	objects = Array.new
	attr_array.each do |row|
		new_object = eval(self.name).new
		fields_ar = new_object.attr_file.fields(model_name_)
		fields_ar.each_index do |index|
			new_object.send("#{fields_ar[index]}=",row[index])
		end
		objects << new_object
	end
	objects
end

#attr_column(attribute) ⇒ Object



29
30
31
32
# File 'lib/active_csv/class_methods.rb', line 29

def attr_column(attribute)
	object  = eval(self.name).new
	object.attr_file.fields(model_name_).index attribute
end

#check_attr?(attribute) ⇒ Boolean

——- relacionadas ao find———–

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/active_csv/class_methods.rb', line 24

def check_attr?(attribute)
	attr_file = AttrFile.new("config/csv_attributes.yml")	
	attr_file.fields(model_name_).include? attribute
end

#find(*args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/active_csv/class_methods.rb', line 70

def find(*args)
	file = DbFile.new("db/"+model_name_+".csv")
	content = file.csv_content
	if args[0].respond_to? "to_i"
			ids = Array.new
			args.each do |el|
							ids << el.to_i
			end
			found_ids_indexes = find_with_ids(ids,content)
			found_rows = find_rows_by_indexes(found_ids_indexes,content)
	else
		args[0].keys.each do |key|
			if check_attr? key.to_s
				found_rows_indexes = find_with_attr(key,args[0][key],content)
				found_rows = find_rows_by_indexes(found_rows_indexes,content)
			else
				# raise error
			end
		end
	end
	found = ar_to_obj(found_rows)
	if args.length == 1 && args[0].respond_to?("to_i")
		return found.first
	else
		return found 
	end
end

#find_rows_by_indexes(indexes, content) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_csv/class_methods.rb', line 34

def find_rows_by_indexes(indexes,content)
	found_rows = Array.new
	case indexes.size
	when 0
			raise RecordNotFound, "Couldn't find the especified ID"
	else
		indexes.each do |found|
			found_rows << content[found]
		end
	end
	found_rows
end

#find_with_attr(attribute, value, content) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/active_csv/class_methods.rb', line 59

def find_with_attr(attribute,value,content)
	found_cols = Array.new
	col = attr_column attribute.to_s
	content.each_with_index do |row, index|
					if row[col] == value
						found_cols << index
					end
				end
	found_cols #indexes
end

#find_with_ids(ids, content) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_csv/class_methods.rb', line 47

def find_with_ids(ids,content)
	found_ids = Array.new
	content.each_with_index do |row, index|
			ids.each do |id|
				if row[0] == id.to_s
					found_ids << index
				end
			end
	end
	found_ids
end

#model_name_Object

CAREFULL HERE use the method with the same name in ActiveModel::Naming



100
101
102
# File 'lib/active_csv/class_methods.rb', line 100

def model_name_
	self.name.to_s.downcase
end