Class: ScheduleAttributes::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/schedule_attributes/input.rb

Defined Under Namespace

Modules: RepeatingDates, SingleDates

Constant Summary collapse

NEGATIVES =
[false, "false", 0, "0", "f", "F", "no", "none"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Input

Returns a new instance of Input.



96
97
98
99
100
101
102
103
104
105
# File 'lib/schedule_attributes/input.rb', line 96

def initialize(params)
  raise ArgumentError "expecting a Hash" unless params.is_a? Hash
  @params = params.symbolize_keys.delete_if { |v| v.blank? }
  date_methods = if NEGATIVES.none? { |v| params[:repeat] == v }
                   RepeatingDates
                 else
                   SingleDates
                 end
  (class << self; self end).send :include, date_methods
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



107
108
109
# File 'lib/schedule_attributes/input.rb', line 107

def params
  @params
end

Instance Method Details

#datesObject



162
163
164
165
166
# File 'lib/schedule_attributes/input.rb', line 162

def dates
  dates = (@params[:dates] || [@params[:date]]).compact
  time = start_time.strftime('%H:%M') if @params[:start_time]
  dates.map { |d| parse_date_time(d, time) }
end

#durationObject



109
110
111
112
# File 'lib/schedule_attributes/input.rb', line 109

def duration
  return nil unless end_time
  end_time - start_time
end

#end_dateObject



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/schedule_attributes/input.rb', line 145

def end_date
  if @params[:end_date]
    parse_date_time(@params[:end_date], @params[:end_time] || @params[:start_time])
  elsif @params[:end_time] && !time_only?(@params[:end_time])
    parse_date_time(@params[:end_time])
  elsif end_time
    end_time
  else
    parse_date_time(@params[:end_time])
  end
end

#end_timeObject



119
120
121
122
123
# File 'lib/schedule_attributes/input.rb', line 119

def end_time
  return nil if @params[:all_day]
  return nil unless @params[:end_time].present?
  parse_date_time(end_time_date, @params[:end_time])
end

#end_time_dateObject

if end_time < start_time, the schedule occurs over night



126
127
128
129
130
131
# File 'lib/schedule_attributes/input.rb', line 126

def end_time_date
  return date_input unless @params[:start_time].present?
  return nil if @params[:all_day]
  return nil unless @params[:end_time].present?
  parse_date_time(date_input, @params[:end_time]) <= start_time ? (Time.parse(date_input) + 1.day).strftime('%Y-%m-%d') : date_input
end

#ends?Boolean

Returns:

  • (Boolean)


157
158
159
160
# File 'lib/schedule_attributes/input.rb', line 157

def ends?
  return false if @params[:ends] == "never"
  @params[:end_date].present? || @params[:end_time].present?
end

#start_dateObject



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/schedule_attributes/input.rb', line 133

def start_date
  if @params[:start_date]
    parse_date_time(@params[:start_date], @params[:start_time])
  elsif @params[:start_time] && !time_only?(@params[:start_time])
    parse_date_time(@params[:start_time])
  elsif start_time
    start_time
  else
    parse_date_time(@params[:start_time])
  end
end

#start_timeObject



114
115
116
117
# File 'lib/schedule_attributes/input.rb', line 114

def start_time
  time = @params[:start_time] unless @params[:all_day]
  parse_date_time(date_input, time)
end