43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/riaction/riaction.rb', line 43
def establish_riactionary_class
class << self
def riactionary?
true
end
def riactionless?
@riactionless ||= false
end
def riaction_profile_keys
@riaction_profile_keys ||= {}
end
def riaction_events
@riaction_events ||= {}
end
def riaction_options
@riaction_options ||= ::Riaction::Constants.riaction_options
end
def riaction_use_profile
@riaction_use_profile ||= nil
end
def riactionless(&block)
if block_given?
@riactionless = true
begin
block_value = yield
ensure
@riactionless = false
end
end
end
def reset_riaction
riaction_profile_keys.clear
riaction_events.clear
riaction_options.merge!(::Riaction::Constants.riaction_options)
@riaction_use_profile = nil
end
def add_or_update_riaction_profile(type, opts)
display_name = opts.delete(:display_name) || nil
riaction_check_type(:display_name, display_name, [Symbol, Proc, NilClass])
unless opts.keys.any?{|type| ::Riaction::Constants.supported_identifier_types.include?(type)}
raise ConfigurationError.new("#{self.to_s} defining a riaction profile must use supported IActionable types: #{::Riaction::Constants.supported_identifier_types.map(&:to_s).join(", ")}")
end
riaction_profile_keys.store(type, {
:display_name => display_name,
:identifiers => opts
})
@riaction_use_profile ||= type
end
def add_or_update_riaction_event(name, opts)
trigger = opts.delete(:trigger) || :create
profile = opts.delete(:profile)
profile_type = opts.delete(:profile_type)
params = opts.delete(:params) || {}
guard = opts.delete(:if) || opts.delete(:guard) || true
if profile.nil?
raise ConfigurationError.new("#{self.to_s} defining a riaction event must provide a profile")
end
riaction_check_type(:trigger, trigger, [Symbol])
riaction_check_type(:profile, profile, [Symbol, Proc])
riaction_check_type(:profile_type, profile_type, [Symbol, NilClass])
riaction_check_type(:params, params, [Symbol, Proc, Hash])
riaction_check_type(:guard, guard, [Symbol, Proc, TrueClass])
riaction_events.store(name, {
:trigger => trigger,
:profile => profile,
:profile_type => profile_type,
:params => params,
:guard => guard
})
if ::Riaction::Constants.crud_actions.include? trigger
send "after_#{trigger}".to_sym, ::Riaction::CrudEventCallback.new(name)
else
define_method("trigger_#{trigger}!") do
if self.riaction_log_event?(name) && !self.class.riactionless?
Resque.enqueue(::Riaction::EventPerformer, name, self.class.base_class.to_s, self.id)
end
end
end
end
def riaction_profile_types_defined
riaction_profile_keys.size
end
def riaction_events_defined
riaction_events.size
end
def riaction_check_type(name, value, allowed_types)
unless allowed_types.any?{|type| value.is_a?(type)}
raise ConfigurationError.new("value given for #{name} must be of types: #{allowed_types.map(&:to_s).join(', ')}")
end
end
end
include ::Riaction::Riaction::InstanceMethods
end
|