Class: Mobb::Base
- Inherits:
-
Object
- Object
- Mobb::Base
- Defined in:
- lib/mobb/base.rb
Direct Known Subclasses
Constant Summary collapse
- CALLERS_TO_IGNORE =
[ /\/mobb(\/(base|main|show_exceptions))?\.rb$/, # all sinatra code /^\(.*\)$/, # generated code /rubygems\/(custom|core_ext\/kernel)_require\.rb$/, # rubygems require hacks /active_support/, # active_support require hacks /bundler(\/runtime)?\.rb/, # bundler require hacks /<internal:/, # internal in ruby >= 1.9.2 /src\/kernel\/bootstrap\/[A-Z]/ # maglev kernel files ]
Class Attribute Summary collapse
-
.events ⇒ Object
readonly
Returns the value of attribute events.
Class Method Summary collapse
- .after_condition(name = "#{caller.first[/`.*'/]} condition", &block) ⇒ Object
- .clear(*options) ⇒ Object
- .compile(pattern, options) ⇒ Object
- .compile!(type, pattern, options, &block) ⇒ Object
- .compile_cron(time, at) ⇒ Object
- .condition(name = "#{caller.first[/`.*'/]} condition", &block) ⇒ Object (also: before_condition)
- .cron(pattern, options = {}, &block) ⇒ Object
- .dest_to(channel) ⇒ Object
- .development? ⇒ Boolean
- .disable(*options) ⇒ Object
- .enable(*options) ⇒ Object
- .event(type, pattern, options, &block) ⇒ Object
- .every(pattern, options = {}, &block) ⇒ Object
- .generate_method(name, &block) ⇒ Object
- .helpers(*extensions, &block) ⇒ Object
- .ignore_bot(cond) ⇒ Object
- .production? ⇒ Boolean
- .quit! ⇒ Object
- .receive(pattern, options = {}, &block) ⇒ Object (also: on)
- .reply_to_me(cond) ⇒ Object
- .reset! ⇒ Object
- .run!(options = {}, &block) ⇒ Object
- .running? ⇒ Boolean
- .set(option, value = (not_set = true), ignore_setter = false, &block) ⇒ Object
- .settings ⇒ Object
- .test? ⇒ Boolean
Instance Method Summary collapse
- #call(env) ⇒ Object
- #call!(env) ⇒ Object
- #dispatch! ⇒ Object
- #event_eval ⇒ Object
- #handle_event(base = settings, passed_block = nil) ⇒ Object
- #invoke ⇒ Object
- #process_event(pattern, conditions, block = nil, values = []) ⇒ Object
- #settings ⇒ Object
Class Attribute Details
.events ⇒ Object (readonly)
Returns the value of attribute events.
145 146 147 |
# File 'lib/mobb/base.rb', line 145 def events @events end |
Class Method Details
.after_condition(name = "#{caller.first[/`.*'/]} condition", &block) ⇒ Object
255 256 257 258 259 260 261 262 263 |
# File 'lib/mobb/base.rb', line 255 def after_condition(name = "#{caller.first[/`.*'/]} condition", &block) @after_conditions << generate_method(name) do |res| if String === res res = [res, {}] end block.call(res) res end end |
.clear(*options) ⇒ Object
285 |
# File 'lib/mobb/base.rb', line 285 def clear(*) .each { |option| set(option, nil) }; end |
.compile(pattern, options) ⇒ Object
188 |
# File 'lib/mobb/base.rb', line 188 def compile(pattern, ) Matcher.new(pattern, ); end |
.compile!(type, pattern, options, &block) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/mobb/base.rb', line 168 def compile!(type, pattern, , &block) at = .delete(:at) .each_pair { |option, args| send(option, *args) } matcher = case type when :message compile(pattern, ) when :ticker compile_cron(pattern, at) end unbound_method = generate_method("#{type}", &block) before_conditions, @before_conditions = @before_conditions, [] after_conditions, @after_conditions = @after_conditions, [] wrapper = block.arity != 0 ? proc { |instance, args| unbound_method.bind(instance).call(*args) } : proc { |instance, args| unbound_method.bind(instance).call } [ matcher, wrapper, before_conditions, after_conditions ] end |
.compile_cron(time, at) ⇒ Object
190 191 192 193 194 195 196 |
# File 'lib/mobb/base.rb', line 190 def compile_cron(time, at) if String === time Matcher.new(CronParser.new(time)) else Matcher.new(CronParser.new(Whenever::Output::Cron.new(time, nil, at).time_in_cron_syntax)) end end |
.condition(name = "#{caller.first[/`.*'/]} condition", &block) ⇒ Object Also known as: before_condition
250 251 252 |
# File 'lib/mobb/base.rb', line 250 def condition(name = "#{caller.first[/`.*'/]} condition", &block) @before_conditions << generate_method(name, &block) end |
.cron(pattern, options = {}, &block) ⇒ Object
162 |
# File 'lib/mobb/base.rb', line 162 def cron(pattern, = {}, &block) event(:ticker, pattern, , &block); end |
.dest_to(channel) ⇒ Object
277 278 279 280 281 |
# File 'lib/mobb/base.rb', line 277 def dest_to(channel) after_condition do |res| res.last[:dest_channel] = channel end end |
.development? ⇒ Boolean
210 |
# File 'lib/mobb/base.rb', line 210 def development?; environment == :development; end |
.disable(*options) ⇒ Object
284 |
# File 'lib/mobb/base.rb', line 284 def disable(*) .each { |option| set(option, false) }; end |
.enable(*options) ⇒ Object
283 |
# File 'lib/mobb/base.rb', line 283 def enable(*) .each { |option| set(option, true) }; end |
.event(type, pattern, options, &block) ⇒ Object
164 165 166 |
# File 'lib/mobb/base.rb', line 164 def event(type, pattern, , &block) (@events[type] ||= []) << compile!(type, pattern, , &block) end |
.every(pattern, options = {}, &block) ⇒ Object
160 |
# File 'lib/mobb/base.rb', line 160 def every(pattern, = {}, &block) event(:ticker, pattern, , &block); end |
.generate_method(name, &block) ⇒ Object
198 199 200 201 202 203 |
# File 'lib/mobb/base.rb', line 198 def generate_method(name, &block) define_method(name, &block) method = instance_method(name) remove_method(name) method end |
.helpers(*extensions, &block) ⇒ Object
205 206 207 208 |
# File 'lib/mobb/base.rb', line 205 def helpers(*extensions, &block) class_eval(&block) if block_given? include(*extensions) if extensions.any? end |
.ignore_bot(cond) ⇒ Object
265 266 267 268 269 |
# File 'lib/mobb/base.rb', line 265 def ignore_bot(cond) condition do @env.bot? != cond end end |
.production? ⇒ Boolean
211 |
# File 'lib/mobb/base.rb', line 211 def production?; environment == :production; end |
.quit! ⇒ Object
305 306 307 308 309 310 |
# File 'lib/mobb/base.rb', line 305 def quit! return unless running? running_service.respond_to?(:stop!) ? running_service.stop! : running_service.stop $stderr.puts "== Great sound Mobb, thank you so much" clear :running_service, :handler_name end |
.receive(pattern, options = {}, &block) ⇒ Object Also known as: on
157 |
# File 'lib/mobb/base.rb', line 157 def receive(pattern, = {}, &block) event(:message, pattern, , &block); end |
.reply_to_me(cond) ⇒ Object
271 272 273 274 275 |
# File 'lib/mobb/base.rb', line 271 def reply_to_me(cond) condition do @env.reply_to.include?(settings.name) == cond end end |
.reset! ⇒ Object
147 148 149 150 151 |
# File 'lib/mobb/base.rb', line 147 def reset! @events = {} @before_conditions = [] @after_conditions = [] end |
.run!(options = {}, &block) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/mobb/base.rb', line 287 def run!( = {}, &block) return if running? set handler = detect_repp_handler handler_name = handler.name.gsub(/.*::/, '') service_settings = settings.respond_to?(:service_settings) ? settings.service_settings : {} begin start_service(handler, service_settings, handler_name, &block) rescue => e $stderr.puts e. $stderr.puts e.backtrace ensure quit! end end |
.running? ⇒ Boolean
312 313 314 |
# File 'lib/mobb/base.rb', line 312 def running? running_service? end |
.set(option, value = (not_set = true), ignore_setter = false, &block) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/mobb/base.rb', line 214 def set(option, value = (not_set = true), ignore_setter = false, &block) raise ArgumentError if block && !not_set value, not_set = block, false if block if not_set raise ArgumentError unless option.respond_to?(:each) option.each { |k,v| set(k,v) } return self end setter_name = "#{option}=" if respond_to?(setter_name) && ! ignore_setter return __send__(setter_name, value) end setter = proc { |val| set(option, val, true) } getter = proc { value } case value when Proc getter = value when Symbol, Integer, FalseClass, TrueClass, NilClass getter = value.inspect when Hash setter = proc do |val| val = value.merge(val) if Hash === val set(option, val, true) end end define_singleton(setter_name, setter) define_singleton(option, getter) define_singleton("#{option}?", "!!#{option}") unless method_defined?("#{option}?") self end |
.settings ⇒ Object
153 154 155 |
# File 'lib/mobb/base.rb', line 153 def settings self end |
.test? ⇒ Boolean
212 |
# File 'lib/mobb/base.rb', line 212 def test?; environment == :test; end |
Instance Method Details
#call(env) ⇒ Object
60 61 62 |
# File 'lib/mobb/base.rb', line 60 def call(env) dup.call!(env) end |
#call!(env) ⇒ Object
64 65 66 67 68 |
# File 'lib/mobb/base.rb', line 64 def call!(env) @env = env invoke { dispatch! } [@body, ] end |
#dispatch! ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mobb/base.rb', line 70 def dispatch! # TODO: encode input messages invoke do # TODO: before filters handle_event end ensure # TODO: after fillters end |
#event_eval ⇒ Object
128 |
# File 'lib/mobb/base.rb', line 128 def event_eval; throw :halt, yield; end |
#handle_event(base = settings, passed_block = nil) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mobb/base.rb', line 96 def handle_event(base = settings, passed_block = nil) if responds = base.events[@env.event_type] responds.each do |pattern, block, before_conditions, after_conditions| process_event(pattern, before_conditions) do |*args| event_eval do res = block[*args] after_conditions.inject(res) { |acc, c| c.bind(self).call(acc) } end end end end # TODO: Define respond missing if receive reply message nil end |
#invoke ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mobb/base.rb', line 81 def invoke res = catch(:halt) { yield } return if res.nil? res = [res] if String === res if Array === res && String === res.first tmp = res.dup @body = tmp.shift = tmp.pop else = res end nil end |
#process_event(pattern, conditions, block = nil, values = []) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/mobb/base.rb', line 112 def process_event(pattern, conditions, block = nil, values = []) res = pattern.match?(@env.body) catch(:pass) do conditions.each { |c| throw :pass unless c.bind(self).call } case res when ::Mobb::Matcher::Matched yield(self, *(res.matched)) when TrueClass yield self else nil end end end |
#settings ⇒ Object
130 131 132 |
# File 'lib/mobb/base.rb', line 130 def settings self.class.settings end |