Class: Model
- Inherits:
-
Object
- Object
- Model
- Defined in:
- lib/Model.rb
Overview
The model class processes data from user input and csv files and interacts with the controller
Constant Summary collapse
- @@activities =
[]
Class Method Summary collapse
-
.add_activity(user, type, distance, duration, date) ⇒ Object
adds a new activity append it to the users file.
-
.add_user(user) ⇒ Object
create a file for a new user.
-
.calculate_totals(user, type, month) ⇒ Object
calculates users total distance and time for a particular activity for a month (or all months) returns an array to send to display class.
-
.delete_activity(user, activity) ⇒ Object
deletes an activity based on user selection updates the csv file to remove the deleted activity.
-
.find_longest(user, type, month) ⇒ Object
finds the users longest activities by time and distance returns an array with longest distance and longest time to send to display.
-
.get_activities(user) ⇒ Object
parses activity data from users file uses to create new activity objects and add to activities array.
-
.search_activities(user, date) ⇒ Object
search for activities by date returns array of activities that match date input.
-
.update_activities(user) ⇒ Object
iterates over activities array overwrites file with updated activities.
Class Method Details
.add_activity(user, type, distance, duration, date) ⇒ Object
adds a new activity append it to the users file
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/Model.rb', line 49 def self.add_activity(user, type, distance, duration, date) activity = Activity.new(type, distance, duration, date) if is_in_past?(activity.date) activity.completed = true #checks activity completed if in the past end @@activities << activity #appends to file CSV.open("users/#{user}.csv", "a") do |file| file << [activity.type, activity.distance, activity.duration, activity.date, activity.completed] end return @@activities end |
.add_user(user) ⇒ Object
create a file for a new user
12 13 14 15 |
# File 'lib/Model.rb', line 12 def self.add_user(user) csv = CSV.open("users/#{user}.csv", 'wb') do |csv| end end |
.calculate_totals(user, type, month) ⇒ Object
calculates users total distance and time for a particular activity for a month (or all months) returns an array to send to display class
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/Model.rb', line 108 def self.calculate_totals(user, type, month) distance = 0 duration = 0 activities = self.get_activities(user) activities.each do |activity| month_num = activity.date.to_s.split('-')[1].to_i if activity.type == type && ( month == 'All' || month == Date::MONTHNAMES[month_num] ) distance += activity.distance.to_f duration += activity.duration.to_i end end minutes = duration % 60 hours = (duration - minutes) / 60 return [distance, "#{hours} hours #{minutes} mins"] end |
.delete_activity(user, activity) ⇒ Object
deletes an activity based on user selection updates the csv file to remove the deleted activity
76 77 78 79 80 81 82 |
# File 'lib/Model.rb', line 76 def self.delete_activity(user, activity) activities = self.get_activities(user) @@activities = activities.select do |a| a.type != activity.type || a.distance != activity.distance || a.duration != activity.duration end self.update_activities(user) end |
.find_longest(user, type, month) ⇒ Object
finds the users longest activities by time and distance returns an array with longest distance and longest time to send to display
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/Model.rb', line 86 def self.find_longest(user, type, month) longest_distance = 0 longest_duration = 0 activities = self.get_activities(user) activities.each do |activity| month_num = activity.date.to_s.split('-')[1].to_i if month == 'All' || month == Date::MONTHNAMES[month_num] if activity.distance.to_f > longest_distance longest_distance = activity.distance.to_f end if activity.duration.to_i > longest_duration longest_duration = activity.duration.to_i end end end minutes = longest_duration % 60 hours = (longest_duration - minutes) / 60 return [longest_distance, "#{hours} hours #{minutes} mins"] end |
.get_activities(user) ⇒ Object
parses activity data from users file uses to create new activity objects and add to activities array
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/Model.rb', line 19 def self.get_activities(user) activities = File.open("users/#{user}.csv", "r").read.split("\n") activities = activities.map do |activity| activity.split(",") end @@activities = [] activities.each do |activity| new_activity = Activity.new(activity[0], activity[1], activity[2], activity[3]) new_activity.completed = activity[4] @@activities << new_activity end # sort activities by date @@activities = @@activities.sort_by {|obj| obj.date.to_s.split('-').join('').to_i } return @@activities.reverse # sort from newest to oldest end |
.search_activities(user, date) ⇒ Object
search for activities by date returns array of activities that match date input
64 65 66 67 68 69 70 71 72 |
# File 'lib/Model.rb', line 64 def self.search_activities(user, date) matched = [] @@activities.each do |activity| if activity.date == date && activity.completed == 'false' matched << activity end end return matched end |
.update_activities(user) ⇒ Object
iterates over activities array overwrites file with updated activities
39 40 41 42 43 44 45 |
# File 'lib/Model.rb', line 39 def self.update_activities(user) CSV.open("users/#{user}.csv", "wb") do |file| @@activities.each do |activity| file << [activity.type, activity.distance, activity.duration, activity.date, activity.completed] end end end |