Class: MetaStates::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/meta_states/callback.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_type, conditions, block) ⇒ Callback

Returns a new instance of Callback.



8
9
10
11
12
13
14
# File 'lib/meta_states/callback.rb', line 8

def initialize(state_type, conditions, block)
  @state_type = state_type
  @conditions = conditions
  @block = block
  @max_executions = conditions.delete(:times)
  @execution_count = 0
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



5
6
7
# File 'lib/meta_states/callback.rb', line 5

def block
  @block
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



5
6
7
# File 'lib/meta_states/callback.rb', line 5

def conditions
  @conditions
end

#execution_countObject

Returns the value of attribute execution_count.



6
7
8
# File 'lib/meta_states/callback.rb', line 6

def execution_count
  @execution_count
end

#max_executionsObject (readonly)

Returns the value of attribute max_executions.



5
6
7
# File 'lib/meta_states/callback.rb', line 5

def max_executions
  @max_executions
end

#state_typeObject (readonly)

Returns the value of attribute state_type.



5
6
7
# File 'lib/meta_states/callback.rb', line 5

def state_type
  @state_type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



45
46
47
48
49
50
# File 'lib/meta_states/callback.rb', line 45

def ==(other)
  other.is_a?(self.class) &&
    other.state_type == state_type &&
    other.conditions == conditions &&
    other.block == block
end

#call(state) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/meta_states/callback.rb', line 31

def call(state)
  result = block.call(state)
  @execution_count += 1

  # Remove self from configuration if this was the last execution
  MetaStates.configuration.off(self) if expired?

  result
end

#expired?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/meta_states/callback.rb', line 41

def expired?
  max_executions && execution_count >= max_executions
end

#hashObject



54
55
56
# File 'lib/meta_states/callback.rb', line 54

def hash
  [state_type, conditions, block].hash
end

#matches?(state) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/meta_states/callback.rb', line 16

def matches?(state)
  return false unless state.state_type == state_type.to_s

  conditions.all? do |key, expected_value|
    case key
    when :to
      state.status == expected_value
    when :from
      state.status_before_last_save == expected_value
    else
      state.public_send(key) == expected_value
    end
  end
end