Class: ActiveCSV

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ClassMethods
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/active_csv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

all, ar_to_obj, attr_column, check_attr?, find, find_rows_by_indexes, find_with_attr, find_with_ids, model_name_

Constructor Details

#initialize(*attributes) ⇒ ActiveCSV

Returns a new instance of ActiveCSV.



19
20
21
22
23
24
25
# File 'lib/active_csv.rb', line 19

def initialize(*attributes)	
	if attributes[0].is_a? Hash || attributes[0].is_a?(ActiveSupport::HashWithIndifferentAccess)
		hash_to_obj(attributes[0])
	end
	self.attr_file = AttrFile.new("config/csv_attributes.yml")
	self.db_file = DbFile.new("db/"+model_name+".csv")
end

Instance Attribute Details

#attr_fileObject

Returns the value of attribute attr_file.



17
18
19
# File 'lib/active_csv.rb', line 17

def attr_file
  @attr_file
end

#db_fileObject

Returns the value of attribute db_file.



17
18
19
# File 'lib/active_csv.rb', line 17

def db_file
  @db_file
end

#idObject

Returns the value of attribute id.



17
18
19
# File 'lib/active_csv.rb', line 17

def id
  @id
end

Instance Method Details

#destroyObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/active_csv.rb', line 89

def destroy	
	csv_rows = db_file.csv_content
	target_index = ''
	unless self.id.nil?
		ids = [self.id]
		target_index = ActiveCSV.find_with_ids(ids,csv_rows).first
		unless target_index.nil?
			csv_rows.delete_at(target_index)
			CSV.open(db_file.name,"wb") do |csv|		
				csv_rows.each do |line|
					csv << line
				end
			end
		else
			return false
		end
	end
	self
end

#field_valuesObject



31
32
33
34
35
36
37
# File 'lib/active_csv.rb', line 31

def field_values
	attributes = Array.new
	attr_file.fields(model_name).each do |field|
		 attributes << eval(field)
	end
	attributes #String
end

#hash_to_obj(hash) ⇒ Object



109
110
111
112
113
114
# File 'lib/active_csv.rb', line 109

def hash_to_obj(hash)
	hash.each do |key, value|
		key = key.to_s
			send("#{key}=", value)
	end
end

#last_idObject



39
40
41
42
43
44
45
46
# File 'lib/active_csv.rb', line 39

def last_id
	attr_array = self.db_file.csv_content
	unless attr_array.count == 0
		attr_array.last[0]
	else
		0
	end
end

#model_nameObject



27
28
29
# File 'lib/active_csv.rb', line 27

def model_name
	self.class.to_s.downcase
end

#next_idObject



48
49
50
# File 'lib/active_csv.rb', line 48

def next_id
	last_id.to_i+1
end

#persist_existingObject



52
53
54
55
# File 'lib/active_csv.rb', line 52

def persist_existing
	destroy
	persist_new_obj
end

#persist_new_objObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/active_csv.rb', line 57

def persist_new_obj
	a = db_file.csv_content
	new_row = field_values
	CSV.open(db_file.name,"wb") do |csv|		
			a.each do |line|
				csv << line
			end
			csv << new_row
	end
end

#persisted?Boolean

Also used by form_for in the views on Rails

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/active_csv.rb', line 69

def persisted?
   unless self.id.nil?
		true
	else
		false
	end
end

#saveObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_csv.rb', line 77

def save
	if valid?
		unless persisted?		
			self.id = next_id 
		end
		persist_existing
		return true
	else
		return false
	end
end

#update_attributes(hash_attr) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/active_csv.rb', line 116

def update_attributes(hash_attr)
	hash_to_obj(hash_attr)
	if valid?
		persist_existing
		true
	else
		false
	end
end