Class: SpotFeel::DateTimeLiteral

Inherits:
Node
  • Object
show all
Defined in:
lib/spot_feel/nodes.rb

Overview

  1. date time literal = ( “date” | “time” | “date and time” | “duration” ) , “(” , string literal , “)” ;

Instance Method Summary collapse

Methods inherited from Node

#qualified_names_in_context, #raise_evaluation_error

Instance Method Details

#duration_range(start_date, end_date) ⇒ Object



680
681
682
# File 'lib/spot_feel/nodes.rb', line 680

def duration_range(start_date, end_date)
  (Date.parse(end_date) - Date.parse(start_date)).to_i
end

#eval(context = {}) ⇒ Object



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/spot_feel/nodes.rb', line 659

def eval(context = {})
  head_val = head.eval(context)
  return nil if head_val.nil?
  return head_val if head_val.is_a?(ActiveSupport::Duration) || head_val.is_a?(DateTime) || head_val.is_a?(Date) || head_val.is_a?(Time)

  case keyword.text_value
  when "date and time"
    DateTime.parse(head_val)
  when "date"
    Date.parse(head_val)
  when "time"
    Time.parse(head_val)
  when "duration"
    if defined?(tail) && tail.present?
      (Date.parse(tail.text_value) - Date.parse(head.text_value)).to_i.days
    else
      ActiveSupport::Duration.parse(head_val)
    end
  end
end