• Extending the initial example to make a clipboard text tool

    1.  The store program prepared above is a solution in search of a purpose.

    2.  It can become a more useful program if the text that is stored is written to the clipboard when recalled.

    3.  This is done by making a new clipStore class that extends the store class.

    4. clipStore.roo

        -- clipStore.roo
        
        shared clipboard
        
        clipStore  :  class extends store
        
        initialize  :  method
          clipboard = ^^ clipboard
          return ''
        
        remember  :  method
        
          select 
          when arg() = 2 then -- ADD an association
            return 'base' ~ remember( arg(1), arg(2) )
        
          when arg() = 1 then do -- RECALL association
            v = 'base' ~ remember( arg(1) )
            if v <> '' then
              clipboard ~ setText( v )
            return v
            end 
        
          otherwise 
            return 'base' ~ remember()
        
          end 
        


    5.  Now the store.rooProgram needs to create an instance of a clipStore class instead of a store class.

    6.  No other changes are required.

    7. store2.rooProgram

        -- store2.rooProgram
        
        store = ^^ clipStore -- prepare the 'clipStore' object
        
        call showUsage
        
        signal on halt
        
        do forever
        
          parse linein request +1 key '=' value
        
          key = strip( key )
          value = strip( value )
        
          select 
            when request = '' then
              nop 
            when request = '+' then
              store ~ remember( key, value )
            when request = '?' then
              say store ~ remember( key )
            when request = '-' then
              store ~ forget( key )
            when request = 'k' then
              say store ~ listKeys
            when request = '!' then
              say store ~ listAll
            when translate( request ) = 'Q' then
              exit 0
            otherwise 
              call showUsage
            end 
        
          end 
        
        halt :
        
        exit 0
        
        showUsage  :  procedure
          say 'Usage:'
          say '  + key = value -- adds a value'
          say '  ? key         -- locates value associated with key'
          say '  - key         -- forgets value associated with key'
          say '  k             -- lists all keys'
          say '  !             -- lists all associations'
          say '  Q             -- terminates'
          return 
        

    Proceed...