Module: StateHandler::Mixing
- Defined in:
- lib/state-handler/mixing.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, &block) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/state-handler/mixing.rb', line 60
def method_missing(name, &block)
state = name.to_s.gsub(/\?/, '').to_sym
if name.to_s.end_with?('?')
unless self.class.mapping[state] || self.class.patterns[state]
raise StateHandler::UnexpectedState, "Got: #{state.inspect}"
end
find_mapped(state)
elsif block_given?
@blocks[state] = block
end
end
|
Instance Attribute Details
#response ⇒ Object
Returns the value of attribute response.
11
12
13
|
# File 'lib/state-handler/mixing.rb', line 11
def response
@response
end
|
Class Method Details
.included(base) ⇒ Object
3
4
5
6
7
8
9
|
# File 'lib/state-handler/mixing.rb', line 3
def self.included(base)
base.extend ClassMethods
%w{mapping patterns groups}.each do |attr|
base.class_attribute attr.to_sym
base.send "#{attr}=", {}
end
end
|
Instance Method Details
#at(*args, &block) ⇒ Object
36
37
38
|
# File 'lib/state-handler/mixing.rb', line 36
def at(*args, &block)
args.each { |s| @blocks[s.to_sym] = block }
end
|
#ex(*args, &block) ⇒ Object
40
41
42
|
# File 'lib/state-handler/mixing.rb', line 40
def ex(*args, &block)
args.each { |s| @excludes[s.to_sym] = block }
end
|
#exec {|_self| ... } ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/state-handler/mixing.rb', line 19
def exec(&block)
yield(self)
if state && @blocks[state]
@blocks[state].call
end
if self.class.groups
self.class.groups.each do |group, states|
@blocks[group].call if states.include?(state) && @blocks[group]
end
end
@excludes.each { |s, callback| callback.call unless s == state }
end
|
#find_mapped(state) ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/state-handler/mixing.rb', line 52
def find_mapped(state)
if self.class.mapping[state].kind_of?(Array)
self.class.mapping[state].include?(@response.code.to_i)
else
self.class.mapping[state] == @response.code.to_i
end
end
|
#initialize(response, &block) ⇒ Object
13
14
15
16
17
|
# File 'lib/state-handler/mixing.rb', line 13
def initialize(response, &block)
raise ArgumentError unless response.respond_to?(:code)
@response, @blocks, @excludes = response, {}, {}
exec(&block) if block_given?
end
|
#state ⇒ Object
44
45
46
47
48
49
50
|
# File 'lib/state-handler/mixing.rb', line 44
def state
patterns = self.class.patterns
mapping = self.class.mapping
mapping.keys.each.find { |state| find_mapped(state) } ||
patterns.key(patterns.values.each.find { |regex| @response.code.to_s =~ regex })
end
|