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



137
138
139
140
141
# File 'lib/schedule_attributes/input.rb', line 137

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



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

def end_date
  parse_date_time(@params[:end_date], @params[:start_time]) if @params[:end_date]
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(date_input, @params[:end_time])
end

#ends?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/schedule_attributes/input.rb', line 133

def ends?
  @params[:end_date].present? && @params[:ends] != "never"
end

#start_dateObject



125
126
127
# File 'lib/schedule_attributes/input.rb', line 125

def start_date
  parse_date_time(@params[:start_date]) if @params[:start_date]
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