tmp 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +5 -1
- data/VERSION +1 -1
- data/examples/blocks.rb +20 -8
- data/examples/config_exmpl.rb +2 -2
- data/examples/instance.rb +9 -1
- data/examples/path.rb +4 -2
- data/examples/sugar_use.rb +4 -4
- data/examples/tmp_folder/test_file +1 -0
- data/lib/tmp/core.rb +46 -8
- data/lib/tmp/dsl.rb +1 -1
- metadata +3 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: c1bda2dbca58011f1df3c3a20b42576dfec912cf
         | 
| 4 | 
            +
              data.tar.gz: 77dd1029623db6de2a853f3da7b111db0177bd19
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 49f7c44baa417d6db5b256089996c71889c8244c881fde8828beaa7aa305ad94c224ff5558facf2c70dd1e5193ee4b7dbc6fc2a8c1a59ee868e8c772017168a4
         | 
| 7 | 
            +
              data.tar.gz: 67b8dfa1432a925ac6c22e4eb04e69c7bf4848ddfb283e5758c49958172c8e084a5f8299c609708b0bcc00662dc14694e00afc34915f6c09c020b5dfcebb04aa
         | 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -23,7 +23,11 @@ The real object(module) being called all the time is the TMP::DSL | |
| 23 23 | 
             
            The DSL will make easy the job on you but you can use the default methods alike:
         | 
| 24 24 |  | 
| 25 25 | 
             
            * TMP.write( file_name, object )
         | 
| 26 | 
            -
            *  | 
| 26 | 
            +
              * object
         | 
| 27 | 
            +
            * TMP.read( file_name )
         | 
| 28 | 
            +
              * object
         | 
| 29 | 
            +
            * TMP.path( file_name )
         | 
| 30 | 
            +
              * string
         | 
| 27 31 |  | 
| 28 32 | 
             
            ```ruby
         | 
| 29 33 |  | 
    
        data/VERSION
    CHANGED
    
    | @@ -1 +1 @@ | |
| 1 | 
            -
            3. | 
| 1 | 
            +
            3.1.0
         | 
    
        data/examples/blocks.rb
    CHANGED
    
    | @@ -1,17 +1,17 @@ | |
| 1 1 | 
             
            require 'tmp'
         | 
| 2 2 |  | 
| 3 | 
            -
             | 
| 3 | 
            +
            TMP.purge!
         | 
| 4 4 | 
             
            # using blocks is simply as this
         | 
| 5 | 
            -
             | 
| 5 | 
            +
            __tmp__.some_file_name "w+" do |file|
         | 
| 6 6 | 
             
              file.write Random.rand(1000...9999)
         | 
| 7 7 | 
             
            end
         | 
| 8 8 |  | 
| 9 | 
            -
             | 
| 9 | 
            +
            __tmp__.some_file_name do |file|
         | 
| 10 10 | 
             
              puts file.readline #> some random number
         | 
| 11 11 | 
             
            end
         | 
| 12 12 |  | 
| 13 13 | 
             
            # reopen the new file is also simple
         | 
| 14 | 
            -
             | 
| 14 | 
            +
            __tmp__.some_file_name do |file|
         | 
| 15 15 |  | 
| 16 16 | 
             
              while line = file.gets
         | 
| 17 17 | 
             
                #> some random number same as above
         | 
| @@ -21,7 +21,7 @@ tmp.some_file_name do |file| | |
| 21 21 | 
             
            end
         | 
| 22 22 |  | 
| 23 23 | 
             
            # you can set the file mod if you like, by default it's based on file e
         | 
| 24 | 
            -
             | 
| 24 | 
            +
            __tmp__.some_file_name "w+" do |file|
         | 
| 25 25 |  | 
| 26 26 | 
             
              puts file.readlines.inspect #> [] empty array because the file got w+ command
         | 
| 27 27 |  | 
| @@ -29,10 +29,22 @@ tmp.some_file_name "w+" do |file| | |
| 29 29 |  | 
| 30 30 | 
             
            end
         | 
| 31 31 |  | 
| 32 | 
            -
             | 
| 32 | 
            +
            __tmp__.some_file_name do |file|
         | 
| 33 33 | 
             
              puts file.gets.inspect #> "Hello world!"
         | 
| 34 34 | 
             
            end
         | 
| 35 35 |  | 
| 36 | 
            -
            puts  | 
| 36 | 
            +
            puts __tmp__.some_file_name__path__
         | 
| 37 37 |  | 
| 38 | 
            -
            puts  | 
| 38 | 
            +
            puts __tmp__.some_file_name.inspect #> it's a string from the file we made
         | 
| 39 | 
            +
             | 
| 40 | 
            +
             | 
| 41 | 
            +
            TMP.purge!.inspect
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            #> file_with_extension.rb
         | 
| 44 | 
            +
            __tmp__.file_with_extension ext: 'rb' do |file|
         | 
| 45 | 
            +
              file.write "hello world!"
         | 
| 46 | 
            +
            end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
            __tmp__.file_with_extension ext: 'rb' do |file|
         | 
| 49 | 
            +
              puts file.read
         | 
| 50 | 
            +
            end
         | 
    
        data/examples/config_exmpl.rb
    CHANGED
    
    | @@ -16,8 +16,8 @@ puts "","default tmp folder:", | |
| 16 16 | 
             
            TMP.folder_init #> || tmp_folder_init
         | 
| 17 17 |  | 
| 18 18 | 
             
            # or you can use syntax sugar!
         | 
| 19 | 
            -
             | 
| 19 | 
            +
            __tmp__.hello = { hello: 'world'}
         | 
| 20 20 |  | 
| 21 21 | 
             
            # defined variable
         | 
| 22 | 
            -
            puts  | 
| 22 | 
            +
            puts __tmp__.hello #> { hello: 'world'}
         | 
| 23 23 |  | 
    
        data/examples/instance.rb
    CHANGED
    
    | @@ -18,5 +18,13 @@ tmp_instance.tmp_class_instance_object #> return the tmp_instance | |
| 18 18 | 
             
            tmp_instance.tmp_class_instance_object.folder_path
         | 
| 19 19 |  | 
| 20 20 | 
             
            # Remember this instance use different folder thant the main TMP::DSL
         | 
| 21 | 
            -
             | 
| 21 | 
            +
            __tmp__.test.inspect  #> nil, because it was never used before
         | 
| 22 22 | 
             
                              #> the tmp method is same like invoke the TMP::DSL module
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            tmp_instance.test_file do |file|
         | 
| 25 | 
            +
              file.write "hello world!"
         | 
| 26 | 
            +
            end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            tmp_instance.test_file do |file|
         | 
| 29 | 
            +
              file.read
         | 
| 30 | 
            +
            end
         | 
    
        data/examples/path.rb
    CHANGED
    
    | @@ -1,5 +1,7 @@ | |
| 1 1 | 
             
            require 'tmp'
         | 
| 2 2 |  | 
| 3 | 
            -
            puts  | 
| 4 | 
            -
            puts  | 
| 3 | 
            +
            puts __tmp__.random #> nil or empty string because never used or just fresh inited file
         | 
| 4 | 
            +
            puts __tmp__.random__path__ #> path to random named file,
         | 
| 5 5 | 
             
                                        # if not exist, make an empty one
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            puts __tmp__.random__path__ ext: 'rb' #> path to random named file,
         | 
    
        data/examples/sugar_use.rb
    CHANGED
    
    | @@ -4,15 +4,15 @@ TMP.write :test2, {hello: 'world'} | |
| 4 4 | 
             
            puts TMP.read(:test2)
         | 
| 5 5 |  | 
| 6 6 | 
             
            #or you can use syntax sugar!
         | 
| 7 | 
            -
             | 
| 7 | 
            +
            __tmp__.hello= { hello: 'world'}
         | 
| 8 8 |  | 
| 9 9 | 
             
            # defined variable
         | 
| 10 | 
            -
            puts  | 
| 10 | 
            +
            puts __tmp__.hello #> { hello: 'world'}
         | 
| 11 11 |  | 
| 12 12 | 
             
            # undefined variable
         | 
| 13 | 
            -
            puts  | 
| 13 | 
            +
            puts __tmp__.sup #> nil
         | 
| 14 14 |  | 
| 15 15 | 
             
            TMP.purge!
         | 
| 16 16 |  | 
| 17 17 | 
             
            # call after tmp is purged
         | 
| 18 | 
            -
            puts  | 
| 18 | 
            +
            puts __tmp__.hello #> nil
         | 
| @@ -0,0 +1 @@ | |
| 1 | 
            +
            hello world!
         | 
    
        data/lib/tmp/core.rb
    CHANGED
    
    | @@ -75,13 +75,38 @@ module TMP | |
| 75 75 |  | 
| 76 76 | 
             
                def block variable_name, *args, &block_obj
         | 
| 77 77 |  | 
| 78 | 
            -
                   | 
| 79 | 
            -
             | 
| 78 | 
            +
                  tmp_folder_init
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                  options = args.select{|e|(e.class <= ::Hash)}.reduce({},:merge!)
         | 
| 81 | 
            +
                  args.select!{|e|!(e.class <= ::Hash)}
         | 
| 82 | 
            +
             | 
| 83 | 
            +
                  options[:file_extension] ||= options[:extension] || options[:ext]
         | 
| 84 | 
            +
                  file_name = variable_name.to_s
         | 
| 85 | 
            +
                  options[:file_mod] ||= options[:mod] || args[0].class <= ::String ? args[0] : nil
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                  unless options[:file_extension].nil?
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                    while options[:file_extension][0] == '.'
         | 
| 90 | 
            +
                      options[:file_extension][0]= ''
         | 
| 91 | 
            +
                    end
         | 
| 92 | 
            +
             | 
| 93 | 
            +
                    file_name += ".#{options[:file_extension]}"
         | 
| 94 | 
            +
             | 
| 95 | 
            +
                  end
         | 
| 96 | 
            +
             | 
| 97 | 
            +
                  if File.exist?(File.join( tmp_folder_path, file_name ))
         | 
| 98 | 
            +
                    options[:file_mod] ||= "r+"
         | 
| 80 99 | 
             
                  else
         | 
| 81 | 
            -
                     | 
| 100 | 
            +
                    options[:file_mod] ||= "w+"
         | 
| 82 101 | 
             
                  end
         | 
| 83 102 |  | 
| 84 | 
            -
                   | 
| 103 | 
            +
                  begin
         | 
| 104 | 
            +
                    return File.open( File.join( tmp_folder_path, file_name ), options[:file_mod], &block_obj )
         | 
| 105 | 
            +
                  rescue Errno::ENOENT
         | 
| 106 | 
            +
                    var_file= File.new( File.join( tmp_folder_path, file_name ), options[:file_mod])
         | 
| 107 | 
            +
                    block_obj.call(var_file) unless block_obj.nil?
         | 
| 108 | 
            +
                    var_file.close
         | 
| 109 | 
            +
                  end
         | 
| 85 110 |  | 
| 86 111 | 
             
                end
         | 
| 87 112 |  | 
| @@ -104,10 +129,22 @@ module TMP | |
| 104 129 |  | 
| 105 130 | 
             
                end
         | 
| 106 131 |  | 
| 107 | 
            -
                def path variable_name
         | 
| 132 | 
            +
                def path variable_name,*args
         | 
| 133 | 
            +
             | 
| 134 | 
            +
                  options = args.select{|e|(e.class <= ::Hash)}.reduce({},:merge!)
         | 
| 135 | 
            +
                  args.select!{|e|!(e.class <= ::Hash)}
         | 
| 136 | 
            +
             | 
| 137 | 
            +
                  variable_name = variable_name.to_s
         | 
| 138 | 
            +
                  options[:file_extension] ||= options[:extension] || options[:ext]
         | 
| 139 | 
            +
                  unless options[:file_extension].nil?
         | 
| 140 | 
            +
                    while options[:file_extension][0] == '.'
         | 
| 141 | 
            +
                      options[:file_extension][0]= ''
         | 
| 142 | 
            +
                    end
         | 
| 143 | 
            +
                    variable_name += ".#{options[:file_extension]}"
         | 
| 144 | 
            +
                  end
         | 
| 108 145 |  | 
| 109 146 | 
             
                  tmp_folder_init
         | 
| 110 | 
            -
                  path_to_file= File.join(tmp_folder_path,variable_name | 
| 147 | 
            +
                  path_to_file= File.join( tmp_folder_path, variable_name )
         | 
| 111 148 | 
             
                  unless File.exist?(path_to_file)
         | 
| 112 149 | 
             
                    File.open( path_to_file ,"w") {|f| f.write(""); f.close }
         | 
| 113 150 | 
             
                  end
         | 
| @@ -117,11 +154,12 @@ module TMP | |
| 117 154 |  | 
| 118 155 | 
             
                def purge_files
         | 
| 119 156 |  | 
| 120 | 
            -
                  Dir.glob( File.join( tmp_folder_path,'**','*' ) ). | 
| 157 | 
            +
                  Dir.glob( File.join( tmp_folder_path,'**','*' ) ).map do |file_path|
         | 
| 121 158 |  | 
| 122 159 | 
             
                    begin
         | 
| 123 160 | 
             
                      File.delete file_path
         | 
| 124 | 
            -
                    rescue Errno::ENOENT
         | 
| 161 | 
            +
                    rescue Errno::ENOENT => ex
         | 
| 162 | 
            +
                      ex
         | 
| 125 163 | 
             
                    end
         | 
| 126 164 |  | 
| 127 165 | 
             
                  end
         | 
    
        data/lib/tmp/dsl.rb
    CHANGED
    
    | @@ -26,7 +26,7 @@ module TMP | |
| 26 26 | 
             
                    else
         | 
| 27 27 |  | 
| 28 28 | 
             
                      if method =~ /^\w+__path__$/
         | 
| 29 | 
            -
                        return target_obj.__send__ :path, method.to_s.sub!( /__path__$/,"" )
         | 
| 29 | 
            +
                        return target_obj.__send__ :path, method.to_s.sub!( /__path__$/,"" ),*args
         | 
| 30 30 | 
             
                      else
         | 
| 31 31 | 
             
                        return target_obj.__send__ :read, method
         | 
| 32 32 | 
             
                      end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: tmp
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 3. | 
| 4 | 
            +
              version: 3.1.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Adam Luzsi
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2014- | 
| 11 | 
            +
            date: 2014-06-20 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: rake
         | 
| @@ -66,6 +66,7 @@ files: | |
| 66 66 | 
             
            - examples/test.rb
         | 
| 67 67 | 
             
            - examples/tmp_folder/hello
         | 
| 68 68 | 
             
            - examples/tmp_folder/test
         | 
| 69 | 
            +
            - examples/tmp_folder/test_file
         | 
| 69 70 | 
             
            - files.rb
         | 
| 70 71 | 
             
            - lib/tmp.rb
         | 
| 71 72 | 
             
            - lib/tmp/core.rb
         |