Class: Habits::Habit

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#daysObject (readonly)

Returns the value of attribute days.



53
54
55
# File 'lib/habits/habit.rb', line 53

def days
  @days
end

#eventsObject (readonly)

Returns the value of attribute events.



53
54
55
# File 'lib/habits/habit.rb', line 53

def events
  @events
end

#red_zoneObject

Returns the value of attribute red_zone.



54
55
56
# File 'lib/habits/habit.rb', line 54

def red_zone
  @red_zone
end

#statusObject (readonly)

Returns the value of attribute status.



53
54
55
# File 'lib/habits/habit.rb', line 53

def status
  @status
end

#titleObject (readonly)

Returns the value of attribute title.



53
54
55
# File 'lib/habits/habit.rb', line 53

def title
  @title
end

#yellow_zoneObject

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

.allObject



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_holdObject



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_holdObject



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_countObject



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



98
99
100
101
102
103
104
105
106
# File 'lib/habits/habit.rb', line 98

def activities_on_week(week, day=nil)
  activities = @events.select do |e| 
    e.is_a?(Events::Activity) 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

#destroyObject



108
109
110
111
# File 'lib/habits/habit.rb', line 108

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

#holdObject



154
155
156
157
# File 'lib/habits/habit.rb', line 154

def hold
  @status = Status.on_hold
  save
end

#join(habit) ⇒ Object



141
142
143
144
145
146
# File 'lib/habits/habit.rb', line 141

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



148
149
150
151
152
# File 'lib/habits/habit.rb', line 148

def join!(habit)
  join(habit)
  save
  habit.destroy
end

#saveObject



78
79
80
81
82
83
84
85
86
# File 'lib/habits/habit.rb', line 78

def save
  @@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



119
120
121
122
123
124
125
# File 'lib/habits/habit.rb', line 119

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



113
114
115
116
117
# File 'lib/habits/habit.rb', line 113

def set_title(title)
  raise "No spaces or commas allowed in habit title" if title =~ /[\s,,]+/
  @old_title = @title
  @title = title
end

#splitObject



127
128
129
130
131
132
# File 'lib/habits/habit.rb', line 127

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



134
135
136
137
138
139
# File 'lib/habits/habit.rb', line 134

def split!
  habits = split
  habits.each{|habit| habit.save}
  destroy
  habits
end

#unholdObject



159
160
161
162
163
# File 'lib/habits/habit.rb', line 159

def unhold
  @status = Status.green
  @status = Status.resolve(self)
  save
end

#update_statusObject



88
89
90
91
92
93
94
95
96
# File 'lib/habits/habit.rb', line 88

def update_status
  old_status = @status
  @status = Status.resolve(self)
  
  if old_status != @status
    yield if block_given?
    save
  end
end