save_file 1.0.31

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.

Potentially problematic release.


This version of save_file might be problematic. Click here for more details.

@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a3140e65c5970b71373c653d85535670de98b162388e808608b9aef056224fb5
4
+ data.tar.gz: b9f53fe2cb3e875b6eb29a4b13254814f7f227fb96fb0136c41b201245ccbbf8
5
+ SHA512:
6
+ metadata.gz: 0c6e818c0a048aab6d1d8afd819adff69015397508b71ad240781290a7f3f5a0d1cebf052bec59ee920bc4df1b1ff2c922689febe4ec42de9afb9bfb02c060ed
7
+ data.tar.gz: de58cd838dbe9a0f0a10c614d724a140ace81ebc9d971505baf0f10be97bda154a291852afacc3bdbd1966bef016393fd2bb28b3e921f5d45a0e78b728e44224
@@ -0,0 +1,25 @@
1
+ This gem provides a save_file() method functionality.
2
+
3
+ In other words, you can save a file with this easily.
4
+
5
+ I often needed a save_file() functionality among my
6
+ different projects, so that is why I turned this into
7
+ a standalone gem, rather than writing a new save_file()
8
+ functionality every time anew for my projects.
9
+
10
+ That functionality is bundled into a module called
11
+ SaveFile. Additionally, convenience methods called
12
+ save_file() and append_what_into() are provided,
13
+ and of course several aliases to these.
14
+
15
+ You can pass the permission bits as third argument
16
+ to the two main methods of this gem:
17
+
18
+ - save_file()
19
+ - append_what_into()
20
+
21
+ Also note that:
22
+
23
+ - write_what_into()
24
+
25
+ is an alias to save_file().
@@ -0,0 +1,5 @@
1
+ #!/System/Index/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ require 'save_file/requires/requires.rb'
@@ -0,0 +1,30 @@
1
+ #!/System/Index/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ require 'save_file/module/save_file.rb' # This will also pull in constants.rb
6
+
7
+ # =========================================================================== #
8
+ # === append_what_into
9
+ #
10
+ # A wrapper over the save functionality above. Currently we do not
11
+ # make use of the permission bits, but this may change at a later
12
+ # time.
13
+ #
14
+ # For this to work, we need to require the SaveFile module.
15
+ #
16
+ # Usage example:
17
+ # append_what_into()
18
+ # =========================================================================== #
19
+ def append_what_into(
20
+ what = "Testing.\n",
21
+ into_where = 'default.txt',
22
+ permission_to_use = SaveFile::DEFAULT_PERMISSION_TO_USE
23
+ )
24
+ ::SaveFile.append_what_into(what, into_where, permission_to_use)
25
+ end; alias append_what_to append_what_into # === append_what_to
26
+ alias append_into append_what_into # === append_into
27
+ alias append_file append_what_into # === append_file
28
+ alias append_into_file append_what_into # === append_into_file
29
+ alias append_what_into_file append_what_into # === append_what_into_file
30
+ alias append_what_to_this_file append_what_into # === append_what_to_this_file
@@ -0,0 +1,2 @@
1
+ require 'save_file' # Add the default save_file require here.
2
+ include SaveFile
@@ -0,0 +1,46 @@
1
+ #!/System/Index/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # This is the class-variant from which you can subclass, if you need
6
+ # a specialized class that already has this functionality.
7
+ # =========================================================================== #
8
+ # require 'save_file/base/base.rb'
9
+ # =========================================================================== #
10
+ module SaveFile
11
+
12
+ class SaveFile
13
+
14
+ # ========================================================================= #
15
+ # === initialize
16
+ # ========================================================================= #
17
+ def initialize
18
+ reset
19
+ end
20
+
21
+ # ========================================================================= #
22
+ # === reset
23
+ # ========================================================================= #
24
+ def reset
25
+ end
26
+
27
+ # ========================================================================= #
28
+ # === write_what_into
29
+ # ========================================================================= #
30
+ def write_what_into(
31
+ what = "Hello world!\n",
32
+ into = 'default_test_file.md'
33
+ )
34
+ ::SaveFile.write_what_into(what, into)
35
+ end
36
+
37
+ end
38
+
39
+ # =========================================================================== #
40
+ # === SaveFile.new
41
+ # =========================================================================== #
42
+ def self.new
43
+ ::SaveFile::SaveFile.new
44
+ end
45
+
46
+ end
@@ -0,0 +1,22 @@
1
+
2
+
3
+
4
+
5
+
6
+ # =========================================================================== #
7
+ # This file holds the constants that we will use in the save_file gem.
8
+ # =========================================================================== #
9
+ module SaveFile # require 'save_file/constants.rb'
10
+
11
+ # ========================================================================= #
12
+ # Specify the file mode to be used here.
13
+ # ========================================================================= #
14
+ FILE_MODE_TO_BE_USED = 'w+'
15
+
16
+ # ========================================================================= #
17
+ # === SAVE_FILE_WHERE_TO
18
+ # ========================================================================= #
19
+ SAVE_FILE_WHERE_TO = 'default.txt'
20
+ DEFAULT_PERMISSION_TO_USE = 0755
21
+
22
+ end
@@ -0,0 +1,3 @@
1
+ require 'save_file/append/append_what_into.rb' # Pull in the module.
2
+ # We also need to ensure that append_what_into() works
3
+ # as well when requiring that file.
@@ -0,0 +1,130 @@
1
+ #!/System/Index/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # This file holds the main module of SaveFile. It requires the file
6
+ # constants.rb.
7
+ # =========================================================================== #
8
+ # require 'save_file/module/save_file.rb'
9
+ # =========================================================================== #
10
+ require 'save_file/constants.rb' # Pull in 3 constants from that file.
11
+
12
+ module SaveFile
13
+
14
+ alias e puts
15
+
16
+ # ========================================================================= #
17
+ # === save_file
18
+ #
19
+ # Use this method to save a file in your ruby projects.
20
+ #
21
+ # The arguments to this method are the following:
22
+ #
23
+ # (1) what
24
+ # (2) into_where
25
+ # (3) the permission to use for the newly created file
26
+ # (4) file mode to use
27
+ #
28
+ # The second argument, `into_where`, is the target file that will
29
+ # be created or stored-into. For instance, 'test.txt'
30
+ #
31
+ # As of Dec 2013, the third argument is the permission bit.
32
+ #
33
+ # The fourth argument is the file mode to be used, which we
34
+ # default to the 'w+' mode. If you require append-functionality,
35
+ # it is recommended to use the append_into() or append_file()
36
+ # method, which can be found in this file as well, below the
37
+ # save_file() method.
38
+ # ========================================================================= #
39
+ def save_file(
40
+ what = "Testing.\n", # What we will save.
41
+ into_where = SAVE_FILE_WHERE_TO, # Save into this file here.
42
+ permission_to_use = DEFAULT_PERMISSION_TO_USE, # With these permissions.
43
+ file_mode_to_be_used = FILE_MODE_TO_BE_USED # The file mode to be used. Can also be a Symbol such as :overwrite
44
+ )
45
+
46
+ into_where = into_where.to_s
47
+ if file_mode_to_be_used == :overwrite
48
+ file_mode_to_be_used = 'w+'
49
+ elsif file_mode_to_be_used == :append
50
+ file_mode_to_be_used = 'a+'
51
+ end
52
+ # ======================================================================= #
53
+ # Next handle Array input. Unsure whether we should join via \n or
54
+ # not. Dec 2013 I decided that we join via '', and not via "\n".
55
+ # ======================================================================= #
56
+ if what.is_a? Array
57
+ what = what.join('') # ("\n")
58
+ end
59
+ if File.directory? into_where
60
+ into_where = sfile(into_where) if respond_to? :sfile
61
+ e 'Sorry, can not store into '+into_where
62
+ e 'as it is a directory.'
63
+ end
64
+ if permission_to_use.is_a? String # File.open() does not like Strings, hence convert them.
65
+ case permission_to_use
66
+ when 'w' # We assume the user made a mistake here.
67
+ file_mode_to_be_used = 'w'
68
+ permission_to_use = DEFAULT_PERMISSION_TO_USE
69
+ else # Else we treat it like an Octal.
70
+ permission_to_use = permission_to_use.to_i(8)
71
+ end
72
+ elsif permission_to_use.nil?
73
+ permission_to_use = DEFAULT_PERMISSION_TO_USE
74
+ end
75
+ if permission_to_use == :append
76
+ permission_to_use = DEFAULT_PERMISSION_TO_USE
77
+ elsif permission_to_use.is_a? Symbol # If that is the case then we assume an error happened.
78
+ permission_to_use = DEFAULT_PERMISSION_TO_USE
79
+ end
80
+ file_mode_to_be_used = file_mode_to_be_used.to_s
81
+ begin
82
+ Signal.trap('SIGINT') { exit }
83
+ # ===================================================================== #
84
+ # If we are appending then we are effectively doing this:
85
+ # File.open('myfile.out','a') {|f| f.puts "Hello, world." }
86
+ # ===================================================================== #
87
+ File.open(into_where, file_mode_to_be_used, permission_to_use) {|file|
88
+ file.write(what) # Write the new file here.
89
+ } # The aliases to it here.
90
+ rescue Errno::EINVAL
91
+ if into_where.include? '-' # Then try again.
92
+ into_where = into_where.split('-').first
93
+ end
94
+ File.open(into_where, file_mode_to_be_used, permission_to_use) {|file|
95
+ file.write(what) # Write the new file here.
96
+ }
97
+ rescue Errno::EACCES
98
+ into_where = sfile(into_where) if respond_to? :sfile
99
+ error_string = 'You do not have sufficient permission '+
100
+ 'to write into '+into_where+'.'
101
+ e error_string
102
+ end
103
+ end; alias save_what_into save_file # === save_what_into
104
+ alias save_what_to save_file # === save_what_to
105
+ alias save_what_where save_file # === save_what_where
106
+ alias write_what_into save_file # === write_what_into
107
+ alias write_what_to save_file # === write_what_to
108
+ alias save_to save_file # === save_to
109
+ alias save save_file # === save
110
+ alias save_file save_file # === save_file
111
+
112
+ # ========================================================================= #
113
+ # === SaveFile.append_what_into
114
+ # ========================================================================= #
115
+ def self.append_what_into(
116
+ what = "Testing.\n",
117
+ into_where = 'default.txt',
118
+ permission_to_use = ::SaveFile::DEFAULT_PERMISSION_TO_USE
119
+ )
120
+ ::SaveFile.save_what_into(
121
+ what,
122
+ into_where,
123
+ permission_to_use,
124
+ 'a+'
125
+ )
126
+ end; self.instance_eval { alias append_file append_what_into } # === SaveFile.append_file
127
+
128
+ extend self # Make SaveFile.save_file() available.
129
+
130
+ end
@@ -0,0 +1,2 @@
1
+ require 'save_file/append/append_what_into.rb'
2
+ require 'save_file/base/base.rb'
@@ -0,0 +1,12 @@
1
+ #!/System/Index/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ module SaveFile
6
+
7
+ # ========================================================================= #
8
+ # === SaveFile::VERSION
9
+ # ========================================================================= #
10
+ VERSION = '1.0.31'
11
+
12
+ end
@@ -0,0 +1,94 @@
1
+ # =========================================================================== #
2
+ # Gemspec for Project SaveFile.
3
+ # =========================================================================== #
4
+ require 'save_file/version/version.rb'
5
+
6
+ Gem::Specification.new { |s|
7
+
8
+ s.name = 'save_file'
9
+ s.version = SaveFile::VERSION
10
+ s.date = Time.now.strftime('%Y-%m-%d')
11
+
12
+ DESCRIPTION = <<-EOF
13
+
14
+ This class can be used to save a file in a convenient way,
15
+ by giving you a very simple save-file functionality.
16
+
17
+ I liked the name 'save_file()' a lot, and I found that I
18
+ was needing such a functionality in many of my projects,
19
+ so I made a tiny gem that would do exactly this. Note
20
+ that the recommended methods are write_what_into() and
21
+ append_what_into() - at the least for me, this is easy
22
+ to remember.
23
+
24
+ Of course you can use File.open() or File.new() already
25
+ as-is in block variant, so there is no need to use a
26
+ gem like this one here - but I did not want to have to
27
+ deal with file permissions or missing files or similar
28
+ all on my own, via the File API that I do not as easily
29
+ remember. So I just wrote an API that is simple enough
30
+ for my use cases.
31
+
32
+ Usage examples:
33
+
34
+ require 'save_file'
35
+ save_what_into('test', '/tmp/test.txt')
36
+
37
+ You can also use:
38
+
39
+ SaveFile.write_what_into(what, into)
40
+ SaveFile.append_what_into(what, into)
41
+
42
+ The main module can be included into a class,
43
+ which then will have these methods available.
44
+
45
+ This project has no external dependencies, deliberately
46
+ so. It has to remain stand-alone.
47
+
48
+ Version 1.0.3 made it more clear when to use save_file()
49
+ and when to use append_into_file().
50
+
51
+ If you have specific suggestions to make this gem more
52
+ useful for others, please drop me an email at:
53
+
54
+ shevegen@gmail.com
55
+
56
+ Thank you.
57
+
58
+ EOF
59
+
60
+ s.summary = DESCRIPTION
61
+ s.description = DESCRIPTION
62
+
63
+ s.extra_rdoc_files = %w()
64
+
65
+ s.authors = ['Robert A. Heiler']
66
+ s.email = 'shevegen@gmail.com'
67
+ these_files = Dir['**/*']
68
+ s.files = these_files
69
+ s.files << 'README.md'
70
+ s.license = 'GPL-2.0'
71
+
72
+ # ========================================================================= #
73
+ # Show this when a user installs this project.
74
+ # ========================================================================= #
75
+ s.post_install_message = <<-EOF
76
+
77
+ This gem, called "save_file", can be used to quickly save content
78
+ into a file. This is easily possible in ruby with File.open {} but
79
+ I wanted to have this available as a method on module SaveFile.
80
+
81
+ Usage example:
82
+
83
+ require 'save_file'
84
+ SaveFile.write_what_into("Hello world!", 'foobar.txt')
85
+
86
+ EOF
87
+
88
+ s.homepage = 'http://rubygems.org/gems/save_file'
89
+
90
+ s.required_ruby_version = '>= '+RUBY_VERSION
91
+ s.required_rubygems_version = '>= '+Gem::VERSION
92
+ s.rubygems_version = '>= '+Gem::VERSION
93
+
94
+ }
@@ -0,0 +1,32 @@
1
+ # =========================================================================== #
2
+ # Code to test the save_file functionality.
3
+ # =========================================================================== #
4
+ require 'colours/autoinclude'
5
+ require 'save_file/autoinclude'
6
+ N = "\n"
7
+ save_into = '/Depot/Temp/OK'
8
+ if File.exist? save_into
9
+ File.delete(save_into)
10
+ end
11
+ e 'We will test the functionality of the save_file gem next.'
12
+ e 'We will save our test dataset into the '+
13
+ 'directory: '+sdir(save_into)+N+N
14
+ e '(1) First, we test the save_file() functionality:'
15
+ e 'For this, we will save the string "yo" '+
16
+ 'into `'+sfile(save_into)+'`.'
17
+ save_file('yo', save_into)
18
+
19
+ e
20
+ e '(2) Second, we test the append_what_into() '+
21
+ 'functionality:'
22
+ e 'This will append the string "there" into '+
23
+ 'the same file.'
24
+ append_what_into(' there', save_into)
25
+ append_what_into(' OMG!'+N+N+N+'CAN THIS REALLY BE?',
26
+ save_into)
27
+ e 'Done!'
28
+ e
29
+ e 'View the file at:'
30
+ e
31
+ e ' cat '+sfile(save_into)
32
+ e
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: save_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.31
5
+ platform: ruby
6
+ authors:
7
+ - Robert A. Heiler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: "\nThis class can be used to save a file in a convenient way,\nby giving
14
+ you a very simple save-file functionality.\n\nI liked the name 'save_file()' a lot,
15
+ and I found that I\nwas needing such a functionality in many of my projects,\nso
16
+ I made a tiny gem that would do exactly this. Note\nthat the recommended methods
17
+ are write_what_into() and\nappend_what_into() - at the least for me, this is easy\nto
18
+ remember.\n\nOf course you can use File.open() or File.new() already\nas-is in block
19
+ variant, so there is no need to use a \ngem like this one here - but I did not want
20
+ to have to\ndeal with file permissions or missing files or similar\nall on my own,
21
+ via the File API that I do not as easily\nremember. So I just wrote an API that
22
+ is simple enough\nfor my use cases. \n\nUsage examples:\n\n require 'save_file'\n
23
+ \ save_what_into('test', '/tmp/test.txt')\n\nYou can also use:\n\n SaveFile.write_what_into(what,
24
+ into)\n SaveFile.append_what_into(what, into)\n\nThe main module can be included
25
+ into a class,\nwhich then will have these methods available.\n\nThis project has
26
+ no external dependencies, deliberately \nso. It has to remain stand-alone.\n\nVersion
27
+ 1.0.3 made it more clear when to use save_file()\nand when to use append_into_file().\n
28
+ \ \nIf you have specific suggestions to make this gem more\nuseful for others, please
29
+ drop me an email at:\n\n shevegen@gmail.com\n\nThank you.\n\n"
30
+ email: shevegen@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - README.md
36
+ - lib/save_file.rb
37
+ - lib/save_file/append/append_what_into.rb
38
+ - lib/save_file/autoinclude.rb
39
+ - lib/save_file/base/base.rb
40
+ - lib/save_file/constants.rb
41
+ - lib/save_file/module.rb
42
+ - lib/save_file/module/save_file.rb
43
+ - lib/save_file/requires/requires.rb
44
+ - lib/save_file/version/version.rb
45
+ - save_file.gemspec
46
+ - test/testing_save_file.rb
47
+ homepage: http://rubygems.org/gems/save_file
48
+ licenses:
49
+ - GPL-2.0
50
+ metadata: {}
51
+ post_install_message: |2+
52
+
53
+ This gem, called "save_file", can be used to quickly save content
54
+ into a file. This is easily possible in ruby with File.open {} but
55
+ I wanted to have this available as a method on module SaveFile.
56
+
57
+ Usage example:
58
+
59
+ require 'save_file'
60
+ SaveFile.write_what_into("Hello world!", 'foobar.txt')
61
+
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 2.6.3
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.0.3
75
+ requirements: []
76
+ rubygems_version: 3.0.3
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: 'This class can be used to save a file in a convenient way, by giving you
80
+ a very simple save-file functionality. I liked the name ''save_file()'' a lot,
81
+ and I found that I was needing such a functionality in many of my projects, so
82
+ I made a tiny gem that would do exactly this. Note that the recommended methods
83
+ are write_what_into() and append_what_into() - at the least for me, this is easy
84
+ to remember. Of course you can use File.open() or File.new() already as-is in block
85
+ variant, so there is no need to use a gem like this one here - but I did not want
86
+ to have to deal with file permissions or missing files or similar all on my own,
87
+ via the File API that I do not as easily remember. So I just wrote an API that is
88
+ simple enough for my use cases. Usage examples: require ''save_file'' save_what_into(''test'',
89
+ ''/tmp/test.txt'') You can also use: SaveFile.write_what_into(what, into) SaveFile.append_what_into(what,
90
+ into) The main module can be included into a class, which then will have these
91
+ methods available. This project has no external dependencies, deliberately so.
92
+ It has to remain stand-alone. Version 1.0.3 made it more clear when to use save_file()
93
+ and when to use append_into_file(). If you have specific suggestions to make this
94
+ gem more useful for others, please drop me an email at: shevegen@gmail.com Thank
95
+ you.'
96
+ test_files: []