# Adhearsion, open source technology integrator # Copyright 2006 Jay Phillips # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. require 'active_support' class Object # For dealing with data that may be more difficult to manage than it should be. # Strings with simplify() called on them return integers if they appear to be # integers. Arrays with only one item in it return ary.first.simplify. def simplify if is_a? String return Integer(self) if self =~ /^\d+$/ elsif is_a? Array return first.simplify if size == 1 end self end def is_a_group? # TODO: check if it responds to the conventions. end def is_a_user? # TODO: check if it responds to the conventions. end def is_local_number? to_s =~ LOCAL_NUMBER end def is_national_number? to_s =~ national_number end def mutex() @mutex ||= Mutex.new end def synchronize() mutex.synchronize { yield self } end private $BEFORE_CALL, $BEFORE_CALL_HIGH, $BEFORE_CALL_LOW = [],[],[] def before_call priority=:normal, &block name = priority == :normal ? "BEFORE_CALL" : "BEFORE_CALL_#{priority.to_s.upcase}" eval("$#{name}") << block end $AFTER_CALL, $AFTER_CALL_HIGH, $AFTER_CALL_LOW = [],[],[] def after_call priority=:normal, &block name = priority == :normal ? "AFTER_CALL" : "AFTER_CALL_#{priority.to_s.upcase}" eval("$#{name}") << block end end class String def String.random_char case r = rand(62) when 0...10 then r.to_s when 10...36 then (r+55).chr when 36...62 then (r+61).chr end end # Handy way to generate random strings of an arbitrary lengths. def String.random len=8 str = '' len.times do str << String.random_char end str end def nameify() downcase.gsub(/[^\w]/, '') end def nameify!() replace nameify end end class Proc def +@ Thread.current[:container].run_inside(&self) true # Allows "and" operator end def ~@ raise ControlPassingException.new(self) end end # A ControlPassingException is used internally to stop execution of one # dialplan context and begin execution of another proc instead. It is # most notably used by the ~@ unary operator that can be called on a # context name within a dialplan to transfer control entirely to that # particular context. The serve() method in the servlet_container actually # rescues these exceptions specifically and then does +e.target to execute # that code. class ControlPassingException < Exception def initialize target @target = target super end attr_reader :target end #class Regexp # alias replaced_triple_equals === # # Allows regular expressions to be compared against numbers in case statements. # def === other # # This feature is so valuable to the dialplan DSL that a little bit # # of hackery is justified. # # if caller.find {|i| i.ends_with? ":in `run_inside'" } # Test if running in the DSL # return replaced_triple_equals(other.to_s) if other.kind_of? Numeric # end # replaced_triple_equals other # end #end class Module # One ring is approximately six seconds. def rings() self*6 end alias second seconds alias digit seconds alias digits seconds def =~ other to_s =~ other end def am() self end def pm() self+12 end # Used by __case__ statements. For Adhearsion's purposes, passing # a Fixnum to a case statement will convert both the Fixnum operand # and other operand to Strings and then return their === equivalency. def === other to_s === other.to_s end end class Time def weekday?() (1..5).include? wday end def weekend?() !weekday? end def sunday?() wday == 0 end def monday?() wday == 1 end def tuesday?() wday == 2 end def wednesday?() wday == 3 end def thursday?() wday == 4 end def friday?() wday == 5 end def saturday?() wday == 6 end end class Hash def method_missing name, *args if name.to_s[-1] == ?= then self[name.to_s.chop.to_sym] = args.first else self[name] || self[name.to_s] end end end class Array def first=(value) self[0] = value end def last=(value) self[-1] = value end end