d: utils_response, rely on module (remove class) extend module
authorRalph Amissah <ralph@amissah.com>
Mon, 12 Jan 2015 03:15:54 +0000 (22:15 -0500)
committerRalph Amissah <ralph@amissah.com>
Tue, 20 Jan 2015 04:46:16 +0000 (23:46 -0500)
* update a couple of affected files

data/doc/sisu/CHANGELOG_v6
lib/sisu/current/utils_response.rb
lib/sisu/develop/db_drop.rb
lib/sisu/develop/sst_convert_markup.rb
lib/sisu/develop/utils_response.rb

index 4a385a72557cad1269bc5f5e8db351fc4a668d2e..9bc34a1d3d7936be85eb28846011b28789cfb996 100644 (file)
@@ -94,7 +94,9 @@ http://www.jus.uio.no/sisu/pkg/src/sisu_6.4.0.orig.tar.xz
 
   * utils_composite, composite documents (.ssm) extract the list of insert files
 
-  * utils_response, ease selection options
+  * utils_response,
+    * rely on module (remove class) extend module
+    * ease selection options
 
   * SiSU_is switched to a method (instead of a hash) (bin and lib)
 
index 6f30437c2396ee6e8968c219813c2bc2a8eb86c7..c47fab2e2d5c7a4216f7df8445e5d1855da017d4 100644 (file)
@@ -69,5 +69,45 @@ module SiSU_Response
       ans
     end
   end
+  def query
+    def selections_available_(selections=:strict)
+      short_options=(selections == :strict) ? '' : '; [ynqx]'
+      %{'yes', 'no', 'quit' or 'exit'#{short_options}}
+    end
+    def selection_options
+      def response_strict(resp)
+        case resp
+        when /^(?:yes)$/          then true
+        when /^(?:no)$/           then false
+        when /^(?:quit|exit)$/    then exit
+        else
+          puts %{response was: #{resp}}
+          puts %{[please type to select: #{selections_available_(:strict)}]}
+          answer?('',:strict)
+        end
+      end
+      def response_short(resp)
+        case resp
+        when /^(?:y|yes)$/          then true
+        when /^(?:n|no)$/           then false
+        when /^(?:[qx]|quit|exit)$/ then exit
+        else
+          puts %{response was: #{resp}}
+          puts %{[please type to select: #{selections_available_(:short)}]}
+          answer?('',:short)
+        end
+      end
+      self
+    end
+    def answer?(ask,selections=:strict)
+      resp='redo'
+      print ask + %{PROCEED? [#{selections_available_(selections)}]: }
+      resp=File.new('/dev/tty').gets.strip
+      (selections==:strict) \
+      ? selection_options.response_strict(resp)
+      : selection_options.response_short(resp)
+    end
+    self
+  end
 end
 __END__
index a65f1e753258b4ab653440445649e1c81473614c..f2dd642e55af7e3cfd0dba90ecceb77c0ad64a0d 100644 (file)
 
 =end
 module SiSU_DbDrop
+  require_relative 'utils_response'                   # utils_response.rb
   class Drop
-    require_relative 'utils_response'                   # utils_response.rb
+    include SiSU_Response
     def initialize(opt,conn,db_info,sql_type)
       @opt,@conn,@db_info,@sql_type=opt,conn,db_info,sql_type
-      @ans=SiSU_Response::Response.new
       case @sql_type
       when :sqlite
         cascade=''
@@ -82,7 +82,7 @@ module SiSU_DbDrop
           case @sql_type
           when :sqlite
             puts msg_sqlite
-            ans=@ans.response?('remove sql database?')
+            ans=response?('remove sql database?')
             if ans \
             and File.exist?(@db_info.sqlite.db)
               @conn.close
@@ -133,7 +133,7 @@ module SiSU_DbDrop
         rescue
           case @sql_type
           when :sqlite
-            ans=@ans.response?('remove sql database?')
+            ans=response?('remove sql database?')
             if ans and File.exist?(@db_info.sqlite.db); File.unlink(@db_info.sqlite.db)
             end
           else
index b2e233cda1eeaa50a84d8a4db924ff36ed211263..8a63636fbffb2891d15e6d9c357df9da24f034db 100644 (file)
@@ -61,11 +61,10 @@ module SiSU_Modify
   require_relative 'sst_from_xml'                       # sst_from_xml.rb
   require_relative 'utils_response'                     # utils_response.rb
   class ConvertMarkup
+    include SiSU_Response
     def initialize(opt)
       @opt=opt
       @description='This is a script that contains canned text conversions for reuse'
-      @response=SiSU_Response::Response.new
-      @ask=SiSU_Response::Response.new
       @warn='WARNING, PROCEED AT YOUR OWN RISK, will make file changes.'
     end
     def current_match_and_replace
@@ -75,7 +74,7 @@ module SiSU_Modify
       response=''
       unless @opt.cmd=~/QQ/ \
       or @opt.act[:quiet][:set]==:on
-        response=@ask.response?(%{#{  text}\nProceed? })
+        response=response?(%{#{  text}\nProceed? })
       end
     end
     def help
index 4603646eeeb1669287abad85edf893bf07f753b0..a29f2fcd5dbfba5352ae219cc8dbc711f16e64b3 100644 (file)
 
 =end
 module SiSU_Response
-  class Response
-    def available_selections_
-      %{'yes', 'no', 'quit' or 'exit'; [ynqx]}
+  def available_selections_
+    %{'yes', 'no', 'quit' or 'exit'; [ynqx]}
+  end
+  def response?(ask)
+    response='redo'
+    print ask + %{ [#{available_selections_}]: }
+    response=File.new('/dev/tty').gets.strip
+    case response
+    when /^(?:y|yes)$/          then true
+    when /^(?:n|no)$/           then false
+    when /^(?:[qx]|quit|exit)$/ then exit
+    else puts %{[please type: #{available_selections_}]}
+      response?(ask)
+    end
+  end
+  def query
+    def selections_available_(selections=:strict)
+      short_options=(selections == :strict) ? '' : '; [ynqx]'
+      %{'yes', 'no', 'quit' or 'exit'#{short_options}}
     end
-    def response?(ask)
-      response='redo'
-      print ask + %{ [#{available_selections_}]: }
-      response=File.new('/dev/tty').gets.strip
-      case response
-      when /^(?:y|yes)$/          then true
-      when /^(?:n|no)$/           then false
-      when /^(?:[qx]|quit|exit)$/ then exit
-      else puts %{[please type: #{available_selections_}]}
-        response?(ask)
+    def selection_options
+      def response_strict(resp)
+        case resp
+        when /^(?:yes)$/          then true
+        when /^(?:no)$/           then false
+        when /^(?:quit|exit)$/    then exit
+        else
+          puts %{response was: #{resp}}
+          puts %{[please type to select: #{selections_available_(:strict)}]}
+          answer?('',:strict)
+        end
       end
+      def response_short(resp)
+        case resp
+        when /^(?:y|yes)$/          then true
+        when /^(?:n|no)$/           then false
+        when /^(?:[qx]|quit|exit)$/ then exit
+        else
+          puts %{response was: #{resp}}
+          puts %{[please type to select: #{selections_available_(:short)}]}
+          answer?('',:short)
+        end
+      end
+      self
+    end
+    def answer?(ask,selections=:strict)
+      resp='redo'
+      print ask + %{PROCEED? [#{selections_available_(selections)}]: }
+      resp=File.new('/dev/tty').gets.strip
+      (selections==:strict) \
+      ? selection_options.response_strict(resp)
+      : selection_options.response_short(resp)
     end
+    self
   end
 end
 __END__