Class: ActionState::Predicate

Inherits:
Object
  • Object
show all
Defined in:
lib/action_state/predicate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Predicate

Returns a new instance of Predicate.



5
6
7
8
# File 'lib/action_state/predicate.rb', line 5

def initialize(record)
  @record = record
  @result = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **kwargs, &block) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/action_state/predicate.rb', line 55

def method_missing(method_name, *args, **kwargs, &block)
  return self unless @result

  @result = false unless @record.send("#{method_name}?", *args, **kwargs, &block)

  self
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



3
4
5
# File 'lib/action_state/predicate.rb', line 3

def result
  @result
end

Instance Method Details

#excluding(*records) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/action_state/predicate.rb', line 10

def excluding(*records)
  return self unless result

  @result = false if records.include?(@record)

  self
end

#not(**kwargs) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/action_state/predicate.rb', line 35

def not(**kwargs)
  return self unless @result

  @result = false if kwargs.any? do |k, v|
    attribute = @record.send(k)
    next true if attribute.nil? && !v.nil?

    case v
    when ::Range
      v.cover? attribute
    when ::Enumerable
      v.include? attribute
    else
      v == attribute
    end
  end

  self
end

#where(**kwargs) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/action_state/predicate.rb', line 18

def where(**kwargs)
  return self unless @result

  @result = false if kwargs.any? do |k, v|
    case v
    when ::Range
      !v.cover? @record.send(k)
    when ::Enumerable
      !v.include? @record.send(k)
    else
      v != @record.send(k)
    end
  end

  self
end