Class: Event

Inherits:
Object
  • Object
show all
Includes:
Timeable
Defined in:
lib/dbc_today/models/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Event

Returns a new instance of Event.



8
9
10
11
12
13
14
15
# File 'lib/dbc_today/models/event.rb', line 8

def initialize(attributes)
  @phase = attributes.fetch(:phase)
  @week = attributes.fetch(:week)
  @day = attributes.fetch(:day)
  @description = attributes.fetch(:description)
  @start_time = attributes.fetch(:start_time)
  @end_time = attributes.fetch(:end_time)
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



6
7
8
# File 'lib/dbc_today/models/event.rb', line 6

def day
  @day
end

#descriptionObject (readonly)

Returns the value of attribute description.



6
7
8
# File 'lib/dbc_today/models/event.rb', line 6

def description
  @description
end

#end_timeObject (readonly)

Returns the value of attribute end_time.



6
7
8
# File 'lib/dbc_today/models/event.rb', line 6

def end_time
  @end_time
end

#phaseObject (readonly)

Returns the value of attribute phase.



6
7
8
# File 'lib/dbc_today/models/event.rb', line 6

def phase
  @phase
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



6
7
8
# File 'lib/dbc_today/models/event.rb', line 6

def start_time
  @start_time
end

#weekObject (readonly)

Returns the value of attribute week.



6
7
8
# File 'lib/dbc_today/models/event.rb', line 6

def week
  @week
end

Class Method Details

.allObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dbc_today/models/event.rb', line 32

def self.all
  list = []
  
  CSV.foreach(
    "#{current_directory}/../data/schedule.csv",
    headers: true,
    header_converters: :symbol,
    skip_blanks: true
  ) do |row|
    list << Event.new(row)
  end

  list
end

.current_directoryObject



74
75
76
# File 'lib/dbc_today/models/event.rb', line 74

def self.current_directory
  File.expand_path File.dirname(__FILE__)    
end

.nil_or_match(attribute_value, value_to_match) ⇒ Object



69
70
71
72
# File 'lib/dbc_today/models/event.rb', line 69

def self.nil_or_match(attribute_value, value_to_match)
  attribute_value.nil? ||
    attribute_value.include?(value_to_match)
end

.where(args) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/dbc_today/models/event.rb', line 47

def self.where(args)
  all.select { |event|
    nil_or_match(event.phase, args[:phase]) &&
      nil_or_match(event.week, args[:week]) &&
      nil_or_match(event.day, args[:day])
  }.sort_by { |e| e.start_time_military }
end

Instance Method Details

#all_day?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/dbc_today/models/event.rb', line 17

def all_day?
  start_time.downcase == 'all day' ||
    (starts_at_day_start? &&
    ends_at_day_end?)
end

#moment?Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/dbc_today/models/event.rb', line 23

def moment?
  start_time == end_time ||
    end_time.nil?
end

#start_time_militaryObject



28
29
30
# File 'lib/dbc_today/models/event.rb', line 28

def start_time_military
  in_military(start_time)
end