Class: ActiveFields::Finders::BaseFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fields/finders/base_finder.rb

Direct Known Subclasses

ArrayFinder, SingularFinder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(active_field:) ⇒ BaseFinder

Returns a new instance of BaseFinder.



47
48
49
# File 'lib/active_fields/finders/base_finder.rb', line 47

def initialize(active_field:)
  @active_field = active_field
end

Instance Attribute Details

#active_fieldObject (readonly)

Returns the value of attribute active_field.



45
46
47
# File 'lib/active_fields/finders/base_finder.rb', line 45

def active_field
  @active_field
end

Class Method Details

.__operations__Object

Storage for defined operations. Private.



35
36
37
# File 'lib/active_fields/finders/base_finder.rb', line 35

def __operations__
  @__operations__ ||= {}
end

.__operators__Object

Index for finding operation by operator. Private.



40
41
42
# File 'lib/active_fields/finders/base_finder.rb', line 40

def __operators__
  @__operators__ ||= {}
end

.operation(name, operator:, &block) ⇒ Object

Define search operation



8
9
10
11
12
13
14
15
16
17
# File 'lib/active_fields/finders/base_finder.rb', line 8

def operation(name, operator:, &block)
  name = name.to_sym
  operator = operator.to_sym

  __operations__[name] = {
    operator: operator.to_sym,
    block: block,
  }
  __operators__[operator] = name
end

.operation_for(operator) ⇒ Object

Returns search operation name for provided operator



25
26
27
# File 'lib/active_fields/finders/base_finder.rb', line 25

def operation_for(operator)
  __operators__[operator.to_sym]
end

.operationsObject

Returns all defined search operations names



30
31
32
# File 'lib/active_fields/finders/base_finder.rb', line 30

def operations
  __operations__.keys
end

.operator_for(operation_name) ⇒ Object

Returns operator for provided search operation name



20
21
22
# File 'lib/active_fields/finders/base_finder.rb', line 20

def operator_for(operation_name)
  __operations__.dig(operation_name.to_sym, :operator)
end

Instance Method Details

#search(op:, value:) ⇒ Object

Perform query operation

Parameters:

  • op (String, Symbol)

    Operation name or operator

  • value (Any)

    The value to search for



54
55
56
57
58
59
60
# File 'lib/active_fields/finders/base_finder.rb', line 54

def search(op:, value:)
  op = op.to_sym if op.respond_to?(:to_sym)
  operation = self.class.__operations__.key?(op) ? op : self.class.__operators__[op]
  return if operation.nil?

  instance_exec(value, &self.class.__operations__[operation][:block])
end