Class: Super::Filter::Operator

Inherits:
Object
  • Object
show all
Defined in:
lib/super/filter/operator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, display, &behavior) ⇒ Operator

Returns a new instance of Operator.



31
32
33
34
35
# File 'lib/super/filter/operator.rb', line 31

def initialize(identifier, display, &behavior)
  @identifier = identifier.to_s
  @humanized_operator_name = display
  self.behavior = behavior
end

Instance Attribute Details

#humanized_operator_nameObject (readonly)

Returns the value of attribute humanized_operator_name.



57
58
59
# File 'lib/super/filter/operator.rb', line 57

def humanized_operator_name
  @humanized_operator_name
end

#identifierObject (readonly)

Returns the value of attribute identifier.



55
56
57
# File 'lib/super/filter/operator.rb', line 55

def identifier
  @identifier
end

#query_parameter_keysObject (readonly)

Returns the value of attribute query_parameter_keys.



56
57
58
# File 'lib/super/filter/operator.rb', line 56

def query_parameter_keys
  @query_parameter_keys
end

Class Method Details

.[](key) ⇒ Object



11
12
13
# File 'lib/super/filter/operator.rb', line 11

def [](key)
  registry.fetch(key.to_s)
end

.define(identifier, display, &block) ⇒ Object



24
25
26
27
28
# File 'lib/super/filter/operator.rb', line 24

def define(identifier, display, &block)
  operator = new(identifier, display, &block).freeze
  register(identifier, operator)
  operator
end

.register(identifier, operator) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/super/filter/operator.rb', line 15

def register(identifier, operator)
  identifier = identifier.to_s
  if registry.key?(identifier)
    raise Error::AlreadyRegistered, "Already registered: #{identifier}"
  end

  registry[identifier] = operator
end

.registryObject



7
8
9
# File 'lib/super/filter/operator.rb', line 7

def registry
  @registry ||= {}
end

Instance Method Details

#behavior(&block) ⇒ Object



59
60
61
62
# File 'lib/super/filter/operator.rb', line 59

def behavior(&block)
  self.behavior = block if block_given?
  @behavior
end

#behavior=(behavior) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/super/filter/operator.rb', line 37

def behavior=(behavior)
  behavior_params = behavior.parameters
  if behavior_params.size < 2
    raise Error::ArgumentError, "Operator behavior must include `column_name` and `relation`"
  end
  if behavior_params[0][0] != :req && behavior_params[0][0] != :opt
    raise Error::ArgumentError, "First argument `relation` must be a required, positional argument"
  end
  if behavior_params[1][0] != :req && behavior_params[1][0] != :opt
    raise Error::ArgumentError, "Second argument `column_name` must be a required, positional argument"
  end
  if !behavior_params[2..-1].all? { |(type, _name)| type == :keyreq }
    raise Error::ArgumentError, "All query parameter keys must be required, keyword arguments"
  end
  @behavior = behavior
  @query_parameter_keys = behavior_params[2..-1].map(&:last)
end