Class: QueryFilter::Utils::UserConditions

Inherits:
Object
  • Object
show all
Defined in:
lib/query_filter/utils/user_conditions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, options = {}) ⇒ UserConditions

Returns a new instance of UserConditions.



8
9
10
11
12
13
14
# File 'lib/query_filter/utils/user_conditions.rb', line 8

def initialize(target, options = {})
  @target = target
  @options = options

  @if = Array(options[:if])
  @unless = Array(options[:unless])
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



6
7
8
# File 'lib/query_filter/utils/user_conditions.rb', line 6

def target
  @target
end

Instance Method Details

#conditions_lambdasObject



31
32
33
34
# File 'lib/query_filter/utils/user_conditions.rb', line 31

def conditions_lambdas
  @if.map { |c| make_lambda c } +
    @unless.map { |c| invert_lambda make_lambda c }
end

#empty?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/query_filter/utils/user_conditions.rb', line 23

def empty?
  @if.empty? && @unless.empty?
end

#passed?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/query_filter/utils/user_conditions.rb', line 16

def passed?
  return true if empty?

  value = target.params
  conditions_lambdas.all? { |c| c.call(target, value) }
end

#present?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/query_filter/utils/user_conditions.rb', line 27

def present?
  !empty?
end