Class: Habits::Habit
- Inherits:
-
Object
- Object
- Habits::Habit
- Defined in:
- lib/habits/habit.rb,
lib/habits/mock_data_store.rb
Overview
a module to in Habit to mock data storing
Constant Summary collapse
- HABITS_DIR =
File.join(ENV['HOME'], '.habits')
- YELLOW_ZONE =
16 hours before deadline,
16*60*60
- RED_ZONE =
6 -“-
6*60*60
- DAYS =
Time::RFC2822_DAY_NAME
Instance Attribute Summary collapse
-
#days ⇒ Object
readonly
Returns the value of attribute days.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#red_zone ⇒ Object
Returns the value of attribute red_zone.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#yellow_zone ⇒ Object
Returns the value of attribute yellow_zone.
Class Method Summary collapse
- .all ⇒ Object
- .all=(habits) ⇒ Object
- .all_not_on_hold ⇒ Object
- .all_on_hold ⇒ Object
- .find(title) ⇒ Object
-
.join_all(title) ⇒ Object
Join all habits with title ‘title’:something.
- .missed_count ⇒ Object
Instance Method Summary collapse
- #activities_on_week(week, day = nil) ⇒ Object
- #add_event(event, time = Time.now) ⇒ Object
- #destroy ⇒ Object
- #file_path(title = @title) ⇒ Object
- #hold ⇒ Object
-
#initialize(title, days = ['Mon'], yellow_zone = YELLOW_ZONE, red_zone = RED_ZONE, events = []) ⇒ Habit
constructor
A new instance of Habit.
- #join(habit) ⇒ Object
- #join!(habit) ⇒ Object
- #save ⇒ Object
- #set_days(days) ⇒ Object
- #set_title(title) ⇒ Object
- #split ⇒ Object
- #split! ⇒ Object
- #unhold ⇒ Object
- #update_status ⇒ Object
- #validate_zones ⇒ Object
Constructor Details
#initialize(title, days = ['Mon'], yellow_zone = YELLOW_ZONE, red_zone = RED_ZONE, events = []) ⇒ Habit
Returns a new instance of Habit.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/habits/habit.rb', line 56 def initialize(title, days = ['Mon'], yellow_zone = YELLOW_ZONE, red_zone = RED_ZONE, events = []) set_title(title) set_days(days) @yellow_zone, @red_zone = yellow_zone, red_zone @status = Status.green @created_at = Time.now @events = events end |
Instance Attribute Details
#days ⇒ Object (readonly)
Returns the value of attribute days.
53 54 55 |
# File 'lib/habits/habit.rb', line 53 def days @days end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
53 54 55 |
# File 'lib/habits/habit.rb', line 53 def events @events end |
#red_zone ⇒ Object
Returns the value of attribute red_zone.
54 55 56 |
# File 'lib/habits/habit.rb', line 54 def red_zone @red_zone end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
53 54 55 |
# File 'lib/habits/habit.rb', line 53 def status @status end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
53 54 55 |
# File 'lib/habits/habit.rb', line 53 def title @title end |
#yellow_zone ⇒ Object
Returns the value of attribute yellow_zone.
54 55 56 |
# File 'lib/habits/habit.rb', line 54 def yellow_zone @yellow_zone end |
Class Method Details
.all ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/habits/habit.rb', line 16 def self.all @@all ||= begin habits = [] Dir.glob(File.join(HABITS_DIR, '*.habit')).each do |habit_file| habits << YAML.load(File.read(habit_file)) end habits end end |
.all=(habits) ⇒ Object
11 12 13 |
# File 'lib/habits/mock_data_store.rb', line 11 def self.all=(habits) @@all = habits end |
.all_not_on_hold ⇒ Object
26 27 28 |
# File 'lib/habits/habit.rb', line 26 def self.all_not_on_hold all.select{|habit| habit.status != Status.on_hold} end |
.all_on_hold ⇒ Object
30 31 32 |
# File 'lib/habits/habit.rb', line 30 def self.all_on_hold all.select{|habit| habit.status == Status.on_hold} end |
.find(title) ⇒ Object
38 39 40 41 42 |
# File 'lib/habits/habit.rb', line 38 def self.find(title) h = Habit.all.detect {|h| h.title == title.strip} raise "No such habit found." unless h h end |
.join_all(title) ⇒ Object
Join all habits with title ‘title’:something.
45 46 47 48 49 50 51 |
# File 'lib/habits/habit.rb', line 45 def self.join_all(title) joinables = Habit.all.select {|habit| habit.title =~ /^#{title}:.+/} raise "Habits not found." if joinables.size == 0 habit = Habit.find(title) rescue nil habit ||= Habit.new(title, []) joinables.each{|joinable| habit.join!(joinable)} end |
.missed_count ⇒ Object
34 35 36 |
# File 'lib/habits/habit.rb', line 34 def self.missed_count all.select{|habit| habit.status == Status.missed}.size end |
Instance Method Details
#activities_on_week(week, day = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/habits/habit.rb', line 99 def activities_on_week(week, day=nil) activities = @events.select do |e| e.is_a?(Events::Activity) and e.applied_at.year == Date.today.year and Date.new(e.applied_at.year, e.applied_at.month, e.applied_at.day).cweek == week end activities = activities.select{|a| a.applied_at.strftime('%a') == day} if day activities end |
#add_event(event, time = Time.now) ⇒ Object
68 69 70 71 72 |
# File 'lib/habits/habit.rb', line 68 def add_event(event, time=Time.now) event.apply(self, time) @events << event save end |
#destroy ⇒ Object
110 111 112 113 |
# File 'lib/habits/habit.rb', line 110 def destroy FileUtils.rm_f file_path @@all = nil end |
#file_path(title = @title) ⇒ Object
74 75 76 |
# File 'lib/habits/habit.rb', line 74 def file_path(title=@title) File.join(HABITS_DIR, title.downcase + '.habit') end |
#hold ⇒ Object
156 157 158 159 |
# File 'lib/habits/habit.rb', line 156 def hold @status = Status.on_hold save end |
#join(habit) ⇒ Object
143 144 145 146 147 148 |
# File 'lib/habits/habit.rb', line 143 def join(habit) @events += habit.events @days += habit.days @days.uniq! @days.sort!{|a,b| DAYS.index(a) <=> DAYS.index(b)} end |
#join!(habit) ⇒ Object
150 151 152 153 154 |
# File 'lib/habits/habit.rb', line 150 def join!(habit) join(habit) save habit.destroy end |
#save ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/habits/habit.rb', line 78 def save validate_zones @@all = nil FileUtils.rm_f(file_path(@old_title)) if @old_title FileUtils.mkdir_p HABITS_DIR File.open(file_path, 'w') do |file| file.write self.to_yaml end self end |
#set_days(days) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/habits/habit.rb', line 121 def set_days(days) if days.detect{|d| DAYS.index(d).nil?} raise "Valid days are #{DAYS.join(',')}" else @days = days end end |
#set_title(title) ⇒ Object
115 116 117 118 119 |
# File 'lib/habits/habit.rb', line 115 def set_title(title) raise "No spaces or commas allowed in habit title" if title =~ /[\s,,]+/ @old_title = @title @title = title end |
#split ⇒ Object
129 130 131 132 133 134 |
# File 'lib/habits/habit.rb', line 129 def split @days.map do |day| events = @events.select{|event| event.applied_at.strftime('%a') == day} Habit.new("#{@title}:#{day}", [day], YELLOW_ZONE, RED_ZONE, events) end end |
#split! ⇒ Object
136 137 138 139 140 141 |
# File 'lib/habits/habit.rb', line 136 def split! habits = split habits.each{|habit| habit.save} destroy habits end |
#unhold ⇒ Object
161 162 163 164 165 |
# File 'lib/habits/habit.rb', line 161 def unhold @status = Status.green @status = Status.resolve(self) save end |
#update_status ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/habits/habit.rb', line 89 def update_status old_status = @status @status = Status.resolve(self) if old_status != @status yield if block_given? save end end |
#validate_zones ⇒ Object
167 168 169 |
# File 'lib/habits/habit.rb', line 167 def validate_zones raise "Yellow zone must be before red zone." if self.yellow_zone <= self.red_zone end |