Sha256: 17cd42f2fb1c8c1838a3d8b11a5465e260d6d22036bf9a4b36944c92b84d6227

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

module WebConsole
  class View < Window

    def do_javascript_function(function, arguments = nil)
      javascript = self.class.javascript_function(function, arguments)
      result = do_javascript(javascript)
      result.chomp!

      if result.is_integer?
        return result.to_i
      end

      if result.is_float?
        return result.to_f
      end

      return result
    end

    def self.javascript_function(function, arguments = nil)
      function = function.dup
      function << '('

      if arguments
        arguments.each { |argument|
          if argument
            function << argument.javascript_argument          
          else
            function << "null"
          end
          function << ', '
        }
        function = function[0...-2]
      end

      function << ');'

      return function
    end

    private

    class ::String
      def javascript_argument
        return "'#{self.javascript_escape}'"
      end

      def javascript_escape
        self.gsub('\\', "\\\\\\\\").gsub("\n", "\\\\n").gsub("'", "\\\\'")
      end

      def javascript_escape!
        replace(self.javascript_escape)
      end

      def is_float?
        !!Float(self) rescue false
      end

      def is_integer?
        self.to_i.to_s == self
      end
    end

    class ::Float
      def javascript_argument
        return self.to_s
      end
    end

    class ::Integer
      def javascript_argument
        return self.to_s
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
webconsole-0.1.16 lib/webconsole/lib/view/javascript.rb
webconsole-0.1.15 lib/webconsole/lib/view/javascript.rb
webconsole-0.1.14 lib/webconsole/lib/view/javascript.rb
webconsole-0.1.13 lib/webconsole/lib/view/javascript.rb