require 'time_constraint' require 'location_constraint' require 'or_constraint' require 'dsl' class MovieFinderContext include Dsl def initialize self.constraints = [] end def self.evaluate(str) # Massage times so they look like a method (will be caught in method_missing str.gsub!(/(\d{1,2}(:\d{2})?(pm|am)?)\b/, "time_\\1 ").gsub!(":", "_") super end terminators "find", "and" modifier "finishing", :attribute, "finish_time" modifier "starting", :attribute, "start_time" modifier "before", :operator, :< modifier "after", :operator, :> bubble "at" combinator OrConstraint, "or" nouns LocationConstraint, "village", "readings" # Time nouns - tmies aren't valid method names so we need to prefix them and catch them here def method_missing(method, *args) if method.to_s =~ /^time_/ time = method.to_s[5..-1] add_constraint(args[0]) return TimeConstraint.new(time.gsub("_", ":")) else super end end end