class Realize::Filter::Inactive

This transformer can take in an array or hash (put in an array) and filters out the objects who have a start date greater than today or end date less than today. If a start or end date is null then it is assumed to be infinity.

Constants

DEFAULT_END_DATE_KEY
DEFAULT_START_DATE_KEY

Attributes

end_date_key[R]
start_date_key[R]

Public Class Methods

new( start_date_key: DEFAULT_START_DATE_KEY, end_date_key: DEFAULT_END_DATE_KEY ) click to toggle source
# File lib/realize/filter/inactive.rb, line 24
def initialize(
  start_date_key: DEFAULT_START_DATE_KEY,
  end_date_key: DEFAULT_END_DATE_KEY
)
  raise ArgumentError, 'start_date_key is required'  if start_date_key.to_s.empty?
  raise ArgumentError, 'end_date_key is required'    if end_date_key.to_s.empty?

  @start_date_key  = start_date_key
  @end_date_key    = end_date_key

  freeze
end

Public Instance Methods

transform(resolver, value, time, _record) click to toggle source
# File lib/realize/filter/inactive.rb, line 37
def transform(resolver, value, time, _record)
  current_time  = time.utc
  records       = array(value)

  records.select do |record|
    start_time  = parse_date(resolver.get(record, start_date_key))
    end_time    = parse_date(resolver.get(record, end_date_key))

    valid?(start_time, end_time, current_time)
  end
end

Private Instance Methods

parse_date(value) click to toggle source
# File lib/realize/filter/inactive.rb, line 63
def parse_date(value)
  value.to_s.empty? ? nil : Time.parse(value.to_s).utc
end
valid?(start_time, end_time, current_time) click to toggle source
# File lib/realize/filter/inactive.rb, line 51
def valid?(start_time, end_time, current_time)
  valid_start?(start_time, current_time) && valid_end?(end_time, current_time)
end
valid_end?(end_time, current_time) click to toggle source
# File lib/realize/filter/inactive.rb, line 59
def valid_end?(end_time, current_time)
  end_time.nil? || end_time >= current_time
end
valid_start?(start_time, current_time) click to toggle source
# File lib/realize/filter/inactive.rb, line 55
def valid_start?(start_time, current_time)
  start_time.nil? || start_time <= current_time
end