Module: Searchlogic::Search::MethodMissing

Included in:
Searchlogic::Search
Defined in:
lib/searchlogic/search/method_missing.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/searchlogic/search/method_missing.rb', line 11

def method_missing(name, *args, &block)
  condition_name = condition_name(name)
  scope_name = scope_name(condition_name)

  if setter?(name)
    if scope?(scope_name)
      if args.size == 1
        write_condition(
          condition_name,
          type_cast(
            args.first,
            cast_type(scope_name),
            scope_options(scope_name).respond_to?(:searchlogic_options) ? scope_options(scope_name).searchlogic_options : {}
          )
        )
      else
        write_condition(condition_name, args)
      end
    else
      raise UnknownConditionError.new(condition_name)
    end
  elsif scope?(scope_name) && args.size <= 1
    if args.size == 0
      read_condition(condition_name)
    else
      send("#{condition_name}=", *args)
      self
    end
  else
    scope = conditions_array.inject(klass.scoped(current_scope) || {}) do |scope, condition|
      scope_name, value = condition
      scope_name = normalize_scope_name(scope_name)
      klass.send(scope_name, value) if !klass.respond_to?(scope_name)
      arity = klass.named_scope_arity(scope_name)

      if !arity || arity == 0
        if value == true
          scope.send(scope_name)
        else
          scope
        end
      elsif arity == -1
        scope.send(scope_name, *(value.is_a?(Array) ? value : [value]))
      else
        scope.send(scope_name, value)
      end
    end
    scope.send(name, *args, &block)
  end
end

Instance Method Details

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
# File 'lib/searchlogic/search/method_missing.rb', line 4

def respond_to?(*args)
  super || scope?(normalize_scope_name(args.first))
rescue Searchlogic::NamedScopes::OrConditions::UnknownConditionError
  false
end