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
- .clear(*options) ⇒ Object
- .compile(pattern, options) ⇒ Object
- .compile!(type, pattern, option, &block) ⇒ Object
- .development? ⇒ Boolean
- .disable(*options) ⇒ Object
- .enable(*options) ⇒ Object
-
.event(type, pattern, options, &block) ⇒ Object
def every(pattern, options = {}, &block) event(:cron, pattern, options, &block); end.
- .generate_method(name, &block) ⇒ Object
- .production? ⇒ Boolean
- .quit! ⇒ Object
- .receive(pattern, options = {}, &block) ⇒ Object (also: on)
- .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, block = nil, values = []) ⇒ Object
- #settings ⇒ Object
- #tick(env) ⇒ Object
- #tick!(env) ⇒ Object
Class Attribute Details
.events ⇒ Object (readonly)
Returns the value of attribute events.
135 136 137 |
# File 'lib/mobb/base.rb', line 135 def events @events end |
Class Method Details
.clear(*options) ⇒ Object
214 |
# File 'lib/mobb/base.rb', line 214 def clear(*) .each { |option| set(option, nil) }; end |
.compile(pattern, options) ⇒ Object
163 |
# File 'lib/mobb/base.rb', line 163 def compile(pattern, ) Matcher.new(pattern, ); end |
.compile!(type, pattern, option, &block) ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/mobb/base.rb', line 154 def compile!(type, pattern, option, &block) matcher = compile(pattern, option) unbound_method = generate_method("#{type}", &block) wrapper = block.arity != 0 ? proc { |instance, args| unbound_method.bind(instance).call(*args) } : proc { |instance, args| unbound_method.bind(instance).call } [matcher, wrapper] end |
.development? ⇒ Boolean
172 |
# File 'lib/mobb/base.rb', line 172 def development?; environment == :development; end |
.disable(*options) ⇒ Object
213 |
# File 'lib/mobb/base.rb', line 213 def disable(*) .each { |option| set(option, false) }; end |
.enable(*options) ⇒ Object
212 |
# File 'lib/mobb/base.rb', line 212 def enable(*) .each { |option| set(option, true) }; end |
.event(type, pattern, options, &block) ⇒ Object
def every(pattern, options = {}, &block) event(:cron, pattern, options, &block); end
150 151 152 |
# File 'lib/mobb/base.rb', line 150 def event(type, pattern, , &block) (@events[type] ||= []) << compile!(type, pattern, , &block) end |
.generate_method(name, &block) ⇒ Object
165 166 167 168 169 170 |
# File 'lib/mobb/base.rb', line 165 def generate_method(name, &block) define_method(name, &block) method = instance_method(name) remove_method(name) method end |
.production? ⇒ Boolean
173 |
# File 'lib/mobb/base.rb', line 173 def production?; environment == :production; end |
.quit! ⇒ Object
234 235 236 237 238 239 |
# File 'lib/mobb/base.rb', line 234 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
145 |
# File 'lib/mobb/base.rb', line 145 def receive(pattern, = {}, &block) event(:message, pattern, , &block); end |
.reset! ⇒ Object
137 138 139 |
# File 'lib/mobb/base.rb', line 137 def reset! @events = {} end |
.run!(options = {}, &block) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/mobb/base.rb', line 216 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
241 242 243 |
# File 'lib/mobb/base.rb', line 241 def running? running_service? end |
.set(option, value = (not_set = true), ignore_setter = false, &block) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/mobb/base.rb', line 176 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
141 142 143 |
# File 'lib/mobb/base.rb', line 141 def settings self end |
.test? ⇒ Boolean
174 |
# File 'lib/mobb/base.rb', line 174 def test?; environment == :test; end |
Instance Method Details
#call(env) ⇒ Object
49 50 51 |
# File 'lib/mobb/base.rb', line 49 def call(env) dup.call!(env) end |
#call!(env) ⇒ Object
57 58 59 60 61 |
# File 'lib/mobb/base.rb', line 57 def call!(env) @env = env invoke { dispatch! } [@body, @attachments] end |
#dispatch! ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/mobb/base.rb', line 67 def dispatch! # TODO: encode input messages invoke do # TODO: before filters handle_event end ensure # TODO: after fillters end |
#event_eval ⇒ Object
118 |
# File 'lib/mobb/base.rb', line 118 def event_eval; throw :halt, yield; end |
#handle_event(base = settings, passed_block = nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mobb/base.rb', line 93 def handle_event(base = settings, passed_block = nil) if responds = base.events[@env.event_type] responds.each do |pattern, block| process_event(pattern) do |*args| event_eval { block[*args] } end end end # TODO: Define respond missing if receive reply message nil end |
#invoke ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mobb/base.rb', line 78 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 @attachments = tmp.pop else @attachments = res end nil end |
#process_event(pattern, block = nil, values = []) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mobb/base.rb', line 106 def process_event(pattern, block = nil, values = []) res = pattern.match?(@env.body) case res when ::Mobb::Matcher::Matched yield(self, *(res.matched)) when TrueClass yield self else nil end end |
#settings ⇒ Object
120 121 122 |
# File 'lib/mobb/base.rb', line 120 def settings self.class.settings end |
#tick(env) ⇒ Object
53 54 55 |
# File 'lib/mobb/base.rb', line 53 def tick(env) dup.tick!(env) end |
#tick!(env) ⇒ Object
63 64 65 |
# File 'lib/mobb/base.rb', line 63 def tick!(env) fail # TODO: write logic here end |