sketchucation logo sketchucation
    • Login
    šŸ¤‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Stop processing rest of file?

    Scheduled Pinned Locked Moved Developers' Forum
    14 Posts 3 Posters 465 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • thomthomT Offline
      thomthom
      last edited by

      When Ruby load a file, via require or load, is it possible to stop it from loading the rest if a certain condition occurs?

      I'd like to make a check for TT_Lib2 when my plugins load - and if it's not installed a message is presented to the user and the rest of the plugin should not be loaded as it won't work. I want to avoid raising an error as that makes SU halt the loading and display an error dialog - which also jumbles toolbars etc.
      exit! trashes SketchUp.

      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        Have you been looking over my shoulder ?? šŸ˜†

        I was working on just this thang earlier in the week.
        I think I came to the conclusion, that the best implementation, is to create gloabal methods (an extension to the Kernel mixin module.)

        As far as load() goes.. there's no help, as that is always supposed to load regardless, and does not check the $LOADED_FEATURES array.

        Let me go back and look at what I was doing, and think a bit more.

        One thing I know I must do, is implement a global hash, called $LOAD_ERRORS (and an Array: $LOAD_ERROR_FILES,) that hold information when a script loading error occurs.

        Then we can skip the crappy "Load error" dialog Google made. Wait until the end of the startup cycle, and display a Error GUI dialog, that can go back and look at any of the loading errors that occured. Likely this would be a WebDialog. It'd be nice to click a button a write to a logfile. Hilite a certain error, click a button and create a report to send to the author, or post as an attachemnt in the plugin's thread at SCF, or whereever.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          Regardless of load or require being used - I try to find a method that will stop ruby from processing the rest of the file - without killing SketchUp itself.

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            In my prototype I also have a step that is only a comment at this point:

            a) Display a webdialog here so the user can download the file(s).

            b) call retry if download successful

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              So far I am trying my best to NOT touch or override either require or load themselves.

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • tbdT Offline
                tbd
                last edited by

                have a separate loader.rb that checks if LIB exists and has functions required.
                if yes, require/load rest of your code, if not ...

                SketchUp Ruby Consultant | Podium 1.x developer
                http://plugins.ro

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  Having to have yet ANOTHER file for each and every library or plugin package, is not what I am aiming at...

                  ... besides that does not solve the idiot situation, trying to force a plugin to load, using load(), when require does not work.
                  I'm not sure there is a solution to the idiot syndrome anyway. I (personally,) would not give any support, or waste any time responding, to an idiot who does not let the plugin load the way it's supposed to.

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    ThomThom, for now do something like:

                    module TT_FancyUtil
                    
                      # move require dependancy calls inside namespace
                      require('TT/TT_Lib2') rescue raise(LoadError,'TT_Lib2')
                      require('TT/TT_GUI') rescue raise(LoadError,'TT_GUI')
                    
                      # the rest of the module's code
                    
                    # way down the bottom of the namespace block;
                    rescue LoadError => e
                    
                      case e.message
                      when 'TT_Lib2'
                        puts("LoadError; TT_Lib2 file is not present. .. blah blah blah...")
                      when 'TT_GUI'
                        puts("LoadError; TT_GUI file is not present. .. blah blah blah...")
                      else
                        puts("Error; #<#{e.class.name}; #{e.message}>")
                        puts e.backtrace
                      end
                    
                    end # module
                    

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      @unknownuser said:

                      have a separate loader.rb that checks if LIB exists and has functions required.
                      if yes, require/load rest of your code, if not ...

                      Really don't want to use another file. Many of my plugins are self contained in a single file - apart from that they require TT_Lib. And I don't want to split it up just for this check.

                      Thomas Thomassen — SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by

                        Found a way:

                        <span class="syntaxdefault">requireĀ </span><span class="syntaxstring">'sketchup.rb'<br /></span><span class="syntaxdefault">begin<br />Ā Ā requireĀ </span><span class="syntaxstring">'TT_Lib2/core.rb'<br /></span><span class="syntaxdefault">rescueĀ LoadErrorĀ </span><span class="syntaxkeyword">=></span><span class="syntaxdefault">Ā e<br />Ā Ā </span><span class="syntaxcomment">#Ā meh!<br /></span><span class="syntaxdefault">end<br /><br />moduleĀ MagicPlugin<br />Ā Ā </span><span class="syntaxcomment">#Ā Voodoo<br /></span><span class="syntaxdefault">endĀ ifĀ defined</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Ā TT</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">LibĀ </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">Ā </span>
                        

                        Thomas Thomassen — SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • Dan RathbunD Offline
                          Dan Rathbun
                          last edited by

                          interesting!

                          Hmm ... I see rescue modifiers do not work with require(), for some reason ?

                          You could also do (if you did not want any module defined) ...

                          require 'sketchup.rb'
                          begin
                            require 'TT_Lib2/core.rb'
                          rescue LoadError => e
                            # meh!
                          else
                            module MagicPlugin
                              # Voodoo
                            end
                          end# begin
                           
                          

                          I'm not here much anymore.

                          1 Reply Last reply Reply Quote 0
                          • thomthomT Offline
                            thomthom
                            last edited by

                            @dan rathbun said:

                            Hmm ... I see rescue modifiers do not work with require(), for some reason ?

                            What you mean? What modifiers?

                            Thomas Thomassen — SketchUp Monkey & Coding addict
                            List of my plugins and link to the CookieWare fund

                            1 Reply Last reply Reply Quote 0
                            • tbdT Offline
                              tbd
                              last edited by

                              statement modifiers = conditional statements onto the end of a normal statement

                              ` name = first_name + last_name rescue "John Doe"

                              John Doe # if first_name and last_name are not defined`

                              SketchUp Ruby Consultant | Podium 1.x developer
                              http://plugins.ro

                              1 Reply Last reply Reply Quote 0
                              • Dan RathbunD Offline
                                Dan Rathbun
                                last edited by

                                @thomthom said:

                                @dan rathbun said:

                                Hmm ... I see rescue modifiers do not work with require(), for some reason ?

                                What you mean? What modifiers?

                                The conditional keywords if and unless can be used in block position:
                                [ if| unless] ( expression)
                                statements
                                end

                                or modifier position:
                                statement [ if| unless] ( expression)

                                **rescue** can also be used in modifier poistion, but ... my attempt at this:
                                require('booboo.rb') rescue nil
                                ... does NOT trap the LoadError (because LoadError < ScriptError < Exception, and rescue only traps RuntimeError and subclasses by default.)

                                For a one-liner, we must use semi-colons:
                                begin require('booboo.rb'); rescue Exception; nil; end

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • 1 / 1
                                • First post
                                  Last post
                                Buy SketchPlus
                                Buy SUbD
                                Buy WrapR
                                Buy eBook
                                Buy Modelur
                                Buy Vertex Tools
                                Buy SketchCuisine
                                Buy FormFonts

                                Advertisement