J
josh
I would like to get your feedback on the initial design of my app.
The goal is to email a user when an event he cares about change it's time.
i have User class, Event Class and an Email module.
some questions:
1. should Email be a class instead of a module?
2. should Email be refactored into a method of the User class?
3. I read a bit about DCI Design Pattern (http://www.clean-ruby.com/ and http://saturnflyer.com/blog/jim/201...what-your-system-is-vs-what-your-system-does/) and I am curious if there is a way to use it here.
In DCI I believe I'll need a context module that will describe the main use cases of my app,
and it each use case i'll add roles to some objects. in my case the user object will be extended with Email module (the Email will turn into a role, being able to send emails).
I pasted my code below but it's nicer here: http://pastebin.com/XA6ez8FY
# email a user when an event's time was changed.
#
# features
# 1. user can add events (each event has a known url).
# 2. every morning if any event's time was changed,
# the user gets email with the changed events.
# 3. on Monday morning, user gets email of all his events
# regardless of changes in times.
# holds a user and it's events
class User
attr_accessor :events, :name
def initialize name
@name = name
@events = nil
@updated = false
end
# ask each event to get it's current time and
# mark the user as 'updated' so later on we
# will email him/her
def get_time
@updated = @events.any?{|e| e.get_time; e.updated?}
end
def updated?
@updated
end
end
# holds info about a event
class Event
attr_accessor :name, :url, :base_time
def initialize name, url, base_time
@name = name
@url = url
@base_time = base_time
@current_time = nil
@updated = false
end
# scrape event from the website and mark it
# as 'updated' if it's time was modified
def get_time
current_time = fetch_current_time
@current_current_time = current_time if current_time
if current_time != @base_time
@updated = true
end
end
def updated?
@updated
end
private
# get current event time from the website
def fetch_current_time
123124234324
end
end
# send email to a user
module Email
module_function
def send user
# if it's Monday send email with all events
# else send email with only the updated events
puts "sending email to #{user.name}"
end
end
user = User.new('josh')
party = Event.new('party', 'http://events.com/events/123', 1234234)
movie = Event.new('movie', 'http://events/events/564', 1243133)
user.events = [party, movie]
user.get_time
if user.updated? or Date.now.monday?
Email.send(user)
end
The goal is to email a user when an event he cares about change it's time.
i have User class, Event Class and an Email module.
some questions:
1. should Email be a class instead of a module?
2. should Email be refactored into a method of the User class?
3. I read a bit about DCI Design Pattern (http://www.clean-ruby.com/ and http://saturnflyer.com/blog/jim/201...what-your-system-is-vs-what-your-system-does/) and I am curious if there is a way to use it here.
In DCI I believe I'll need a context module that will describe the main use cases of my app,
and it each use case i'll add roles to some objects. in my case the user object will be extended with Email module (the Email will turn into a role, being able to send emails).
I pasted my code below but it's nicer here: http://pastebin.com/XA6ez8FY
# email a user when an event's time was changed.
#
# features
# 1. user can add events (each event has a known url).
# 2. every morning if any event's time was changed,
# the user gets email with the changed events.
# 3. on Monday morning, user gets email of all his events
# regardless of changes in times.
# holds a user and it's events
class User
attr_accessor :events, :name
def initialize name
@name = name
@events = nil
@updated = false
end
# ask each event to get it's current time and
# mark the user as 'updated' so later on we
# will email him/her
def get_time
@updated = @events.any?{|e| e.get_time; e.updated?}
end
def updated?
@updated
end
end
# holds info about a event
class Event
attr_accessor :name, :url, :base_time
def initialize name, url, base_time
@name = name
@url = url
@base_time = base_time
@current_time = nil
@updated = false
end
# scrape event from the website and mark it
# as 'updated' if it's time was modified
def get_time
current_time = fetch_current_time
@current_current_time = current_time if current_time
if current_time != @base_time
@updated = true
end
end
def updated?
@updated
end
private
# get current event time from the website
def fetch_current_time
123124234324
end
end
# send email to a user
module Email
module_function
def send user
# if it's Monday send email with all events
# else send email with only the updated events
puts "sending email to #{user.name}"
end
end
user = User.new('josh')
party = Event.new('party', 'http://events.com/events/123', 1234234)
movie = Event.new('movie', 'http://events/events/564', 1243133)
user.events = [party, movie]
user.get_time
if user.updated? or Date.now.monday?
Email.send(user)
end