-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_handler.rb
72 lines (56 loc) · 1.19 KB
/
event_handler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def event(name, date, &block)
Event.create(name, date).instance_eval(&block)
end
class Event
def self.create(name, date)
@events ||= {}
@events[[name,date]] = new(name, date)
end
#to save a new event from irb, make a function to dump them in a yaml file
def initialize(name, date)
@name = name
@date = date
end
def self.all
(@events ||= {}).values
end
def self.on_date(date)
all.select { |e| e.date == date }
end
def self.class_def(name, &block)
(class << self; self; end).send(:define_method, name, &block)
end
[:name, :date, :desc, :place, :time].each do |attrib|
attr_accessor attrib
class_def "#{attrib}s" do
all.map { |e| e.send(attrib) }
end
class_def "find_by_#{attrib}" do |value|
all.select { |e| e.send(attrib) =~ value }
end
end
def description(desc=nil)
if desc
@desc = desc
else
@desc
end
end
def place(where=nil)
if where
@where = where
else
@where
end
end
def time(time=nil)
if time
@time = time
else
@time
end
end
def to_s
"\nWhat: #{@name}\nWhen: #{@date} at #{@time}\nWhere: #{@where}\nDetails: #{@desc}\n\n"
end
end