save_file 1.0.34

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fc78eddefce9f749a9e6e996bc4958c8aa343bb8dc0dd2a8c8be2aecf0c79f52
4
+ data.tar.gz: 0c34e0af5b8b6f546f745e8596c6223a239d2ada17a31a49137257512a6cb817
5
+ SHA512:
6
+ metadata.gz: ba40495683e416e3f9774f72e2089bf57ed7dff73a1a1108572ad79d3207a4cd96485656f9718e16cbd3f4f999cf57315b70e3d1402384d9f65711fdc68f3776
7
+ data.tar.gz: 59ea539111364707f60ddd1f42adf591908d3573ef8efa8233b3bbd4df04ff7b2582cda6a9629a6609520bdf9fe1b5ebf13c2125166e241fb2c170294156c3a3
data/README.md ADDED
@@ -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,30 @@
1
+ #!/usr/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
+ #!/usr/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,134 @@
1
+ #!/usr/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
+ module SaveFile
11
+
12
+ require 'save_file/constants.rb' # Pull in 3 constants from that file.
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
+ case permission_to_use
65
+ when :default
66
+ permission_to_use = DEFAULT_PERMISSION_TO_USE
67
+ end
68
+ if permission_to_use.is_a? String # File.open() does not like Strings, hence convert them.
69
+ case permission_to_use
70
+ when 'w' # We assume the user made a mistake here.
71
+ file_mode_to_be_used = 'w'
72
+ permission_to_use = DEFAULT_PERMISSION_TO_USE
73
+ else # Else we treat it like an Octal.
74
+ permission_to_use = permission_to_use.to_i(8)
75
+ end
76
+ elsif permission_to_use.nil?
77
+ permission_to_use = DEFAULT_PERMISSION_TO_USE
78
+ end
79
+ if permission_to_use == :append
80
+ permission_to_use = DEFAULT_PERMISSION_TO_USE
81
+ elsif permission_to_use.is_a? Symbol # If that is the case then we assume an error happened.
82
+ permission_to_use = DEFAULT_PERMISSION_TO_USE
83
+ end
84
+ file_mode_to_be_used = file_mode_to_be_used.to_s
85
+ begin
86
+ Signal.trap('SIGINT') { exit }
87
+ # ===================================================================== #
88
+ # If we are appending then we are effectively doing this:
89
+ # File.open('myfile.out','a') {|f| f.puts "Hello, world." }
90
+ # ===================================================================== #
91
+ File.open(into_where, file_mode_to_be_used, permission_to_use) {|file|
92
+ file.write(what) # Write the new file here.
93
+ } # The aliases to it here.
94
+ rescue Errno::EINVAL
95
+ if into_where.include? '-' # Then try again.
96
+ into_where = into_where.split('-').first
97
+ end
98
+ File.open(into_where, file_mode_to_be_used, permission_to_use) {|file|
99
+ file.write(what) # Write the new file here.
100
+ }
101
+ rescue Errno::EACCES
102
+ into_where = sfile(into_where) if respond_to? :sfile
103
+ error_string = 'You do not have sufficient permission '+
104
+ 'to write into '+into_where+'.'
105
+ e error_string
106
+ end
107
+ end; alias save_what_into save_file # === save_what_into
108
+ alias save_what_to save_file # === save_what_to
109
+ alias save_what_where save_file # === save_what_where
110
+ alias write_what_into save_file # === write_what_into
111
+ alias write_what_to save_file # === write_what_to
112
+ alias save_to save_file # === save_to
113
+ alias save save_file # === save
114
+ alias save_file save_file # === save_file
115
+
116
+ # ========================================================================= #
117
+ # === SaveFile.append_what_into
118
+ # ========================================================================= #
119
+ def self.append_what_into(
120
+ what = "Testing.\n",
121
+ into_where = 'default.txt',
122
+ permission_to_use = ::SaveFile::DEFAULT_PERMISSION_TO_USE
123
+ )
124
+ ::SaveFile.save_what_into(
125
+ what,
126
+ into_where,
127
+ permission_to_use,
128
+ 'a+'
129
+ )
130
+ end; self.instance_eval { alias append_file append_what_into } # === SaveFile.append_file
131
+
132
+ extend self # Make SaveFile.save_file() available.
133
+
134
+ 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,2 @@
1
+ require 'save_file/append/append_what_into.rb'
2
+ require 'save_file/base/base.rb'
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ module SaveFile
6
+
7
+ # ========================================================================= #
8
+ # === VERSION
9
+ # ========================================================================= #
10
+ VERSION = '1.0.34'
11
+
12
+ end
data/lib/save_file.rb ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ require 'save_file/requires/requires.rb'
data/save_file.gemspec ADDED
@@ -0,0 +1,89 @@
1
+ # =========================================================================== #
2
+ # Gemspec for Project SaveFile.
3
+ # =========================================================================== #
4
+ require 'save_file/version/version.rb'
5
+ require 'roebe'
6
+
7
+ Gem::Specification.new { |s|
8
+
9
+ s.name = 'save_file'
10
+ s.version = SaveFile::VERSION
11
+ s.date = Time.now.strftime('%Y-%m-%d')
12
+
13
+ DESCRIPTION = <<-EOF
14
+
15
+ This class can be used to save a file in a convenient way,
16
+ by giving you a very simple save-file functionality.
17
+
18
+ I liked the name 'save_file()' a lot, and I found that I
19
+ was needing such a functionality in many of my projects,
20
+ so I made a tiny gem that would do exactly this. Note
21
+ that the recommended methods are write_what_into() and
22
+ append_what_into() - at the least for me, this is easy
23
+ to remember.
24
+
25
+ Of course you can use File.open() or File.new() already
26
+ as-is in block variant, so there is no need to use a
27
+ gem like this one here - but I did not want to have to
28
+ deal with file permissions or missing files or similar
29
+ all on my own, via the File API that I do not as easily
30
+ remember. So I just wrote an API that is simple enough
31
+ for my use cases.
32
+
33
+ Usage examples:
34
+
35
+ require 'save_file'
36
+ save_what_into('test', '/tmp/test.txt')
37
+
38
+ You can also use:
39
+
40
+ SaveFile.write_what_into(what, into)
41
+ SaveFile.append_what_into(what, into)
42
+
43
+ The main module can be included into a class,
44
+ which then will have these methods available.
45
+
46
+ This project has no external dependencies, deliberately
47
+ so. It has to remain stand-alone.
48
+
49
+ Version 1.0.3 made it more clear when to use save_file()
50
+ and when to use append_into_file().
51
+ EOF
52
+
53
+ s.summary = DESCRIPTION
54
+ s.description = DESCRIPTION
55
+
56
+ s.extra_rdoc_files = %w()
57
+
58
+ s.authors = ['Robert A. Heiler']
59
+ s.email = Roebe.email?
60
+ these_files = Dir['**/*']
61
+ s.files = these_files
62
+ s.files << 'README.md'
63
+ s.license = 'MIT'
64
+
65
+ # ========================================================================= #
66
+ # Show this when a user installs this project.
67
+ # ========================================================================= #
68
+ s.post_install_message = <<-EOF
69
+
70
+ This gem, called "save_file", can be used to quickly save content
71
+ into a file. This is easily possible in ruby with File.open {}
72
+ as-is, but I wanted to have this available as a toplevel-method
73
+ on module SaveFile.s.required_ruby_version = '>= '+Roebe.second_most_stable_version_of_ruby
74
+
75
+ See the following usage example:
76
+
77
+ require 'save_file'
78
+
79
+ SaveFile.write_what_into("Hello world!", 'foobar.txt')
80
+
81
+ EOF
82
+
83
+ s.homepage = 'https://rubygems.org/gems/save_file'
84
+
85
+ s.required_ruby_version = '>= '+Roebe.third_most_stable_version_of_ruby
86
+ s.required_rubygems_version = '>= '+Gem::VERSION
87
+ s.rubygems_version = '>= '+Gem::VERSION
88
+
89
+ }
@@ -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,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: save_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.34
5
+ platform: ruby
6
+ authors:
7
+ - Robert A. Heiler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-15 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
+ email: shevy@inbox.lt
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - README.md
34
+ - lib/save_file.rb
35
+ - lib/save_file/append/append_what_into.rb
36
+ - lib/save_file/autoinclude.rb
37
+ - lib/save_file/base/base.rb
38
+ - lib/save_file/constants.rb
39
+ - lib/save_file/module.rb
40
+ - lib/save_file/module/save_file.rb
41
+ - lib/save_file/requires/requires.rb
42
+ - lib/save_file/version/version.rb
43
+ - save_file.gemspec
44
+ - test/testing_save_file.rb
45
+ homepage: https://rubygems.org/gems/save_file
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message: "\nThis gem, called \"save_file\", can be used to quickly save
50
+ content\ninto a file. This is easily possible in ruby with File.open {} \nas-is,
51
+ but I wanted to have this available as a toplevel-method\non module SaveFile.s.required_ruby_version
52
+ \ = '>= '+Roebe.second_most_stable_version_of_ruby\n\nSee the following usage
53
+ example:\n\n require 'save_file'\n\n SaveFile.write_what_into(\"Hello world!\",
54
+ 'foobar.txt')\n\n"
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.5.8
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 3.2.27
68
+ requirements: []
69
+ rubygems_version: 3.2.27
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: 'This class can be used to save a file in a convenient way, by giving you
73
+ a very simple save-file functionality. I liked the name ''save_file()'' a lot,
74
+ and I found that I was needing such a functionality in many of my projects, so
75
+ I made a tiny gem that would do exactly this. Note that the recommended methods
76
+ are write_what_into() and append_what_into() - at the least for me, this is easy
77
+ to remember. Of course you can use File.open() or File.new() already as-is in block
78
+ variant, so there is no need to use a gem like this one here - but I did not want
79
+ to have to deal with file permissions or missing files or similar all on my own,
80
+ via the File API that I do not as easily remember. So I just wrote an API that is
81
+ simple enough for my use cases. Usage examples: require ''save_file'' save_what_into(''test'',
82
+ ''/tmp/test.txt'') You can also use: SaveFile.write_what_into(what, into) SaveFile.append_what_into(what,
83
+ into) The main module can be included into a class, which then will have these
84
+ methods available. This project has no external dependencies, deliberately so.
85
+ It has to remain stand-alone. Version 1.0.3 made it more clear when to use save_file()
86
+ and when to use append_into_file().'
87
+ test_files: []