lib/sunspot/dsl/scope.rb in outoftime-sunspot-0.0.2 vs lib/sunspot/dsl/scope.rb in outoftime-sunspot-0.7.0

- old
+ new

@@ -1,35 +1,25 @@ module Sunspot module DSL - class Scope - def initialize(query) - @query = query + # + # This class presents an API for building restrictions in the query DSL. The + # methods exposed are the snake-cased names of the classes defined in the + # Restriction module, with the exception of Base and SameAs. All methods + # take a single argument, which is the value to be applied to the + # restriction. + # + class Restriction + def initialize(field_name, query, negative) + @field_name, @query, @negative = field_name, query, negative end - def method_missing(field_name, *args) - if args.length == 0 then RestrictionBuilder.new(field_name, @query) - elsif args.length == 1 then @query.add_scope @query.build_condition(field_name, ::Sunspot::Restriction::EqualTo, args.first) - else super(field_name.to_sym, *args) - end - end - - class RestrictionBuilder - def initialize(field_name, query) - @field_name, @query = field_name, query - end - - def method_missing(condition_name, *args) - clazz = begin - ::Sunspot::Restriction.const_get(condition_name.to_s.camel_case) - rescue(NameError) - super(condition_name.to_sym, *args) - end - if value = args.first - @query.add_scope @query.build_condition(@field_name, clazz, args.first) - else - @query.interpret_condition @field_name, clazz + Sunspot::Restriction.names.each do |class_name| + method_name = Util.snake_case(class_name) + module_eval(<<-RUBY, __FILE__, __LINE__ + 1) + def #{method_name}(value) + @query.add_restriction(@field_name, Sunspot::Restriction::#{class_name}, value, @negative) end - end + RUBY end end end end