to_file 1.0.1 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec_status +3 -2
- data/README.md +4 -3
- data/lib/to_file/version.rb +1 -1
- data/lib/to_file.rb +51 -25
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 656b1a7419e8679c8e7499f2fb3b3d8950b7a547
|
4
|
+
data.tar.gz: e424893fdd05503dec8756e8aec4dcc15fd4fcb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 414cfa65d46d95e80bb3bd4b49e9cecee8ebb6592356a1942d247fa0647d37924cdcfe96d0f6970434cfd43c5bb01e6e2f853f0462bc38503cde6f9d28d43747
|
7
|
+
data.tar.gz: 79d0e2d6e463241797cb92af15c88e511765b855479981bd7c77c33101c0a6283745f76ee24c372a097ce1896d5a829f91852941a3990fcf347f63edaa4724a7
|
data/.rspec_status
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
example_id | status | run_time |
|
2
2
|
--------------------------- | ------ | --------------- |
|
3
|
-
./spec/to_file_spec.rb[1:1] | passed | 0.
|
4
|
-
./spec/to_file_spec.rb[1:2] | passed | 0.
|
3
|
+
./spec/to_file_spec.rb[1:1] | passed | 0.001 seconds |
|
4
|
+
./spec/to_file_spec.rb[1:2] | passed | 0.01262 seconds |
|
5
|
+
./spec/to_file_spec.rb[1:3] | passed | 0.00066 seconds |
|
data/README.md
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
# Introduction about ToFile
|
2
2
|
|
3
|
-
This gem is aim to transform the format of objects to be suitable for exporting , and export them into specific files.
|
3
|
+
This gem is aim to transform the format of objects to be suitable for exporting , and export them into specific files. It's super easy to extend data type which supported by ruby but not in this gem.</br>
|
4
4
|
|
5
5
|
Currently supported export files: </br>
|
6
6
|
* `YAML`
|
7
|
+
* `JSON`
|
7
8
|
|
8
9
|
## Installation
|
9
10
|
|
@@ -26,14 +27,14 @@ Or install it yourself as:
|
|
26
27
|
It's very easy to use and the common usage is: <br />
|
27
28
|
|
28
29
|
```ruby
|
29
|
-
ToFile::To.NAME_OF_FILE(object, export_path = './exaple.FILE_TYPE',
|
30
|
+
ToFile::To.NAME_OF_FILE(object, export_path = './exaple.FILE_TYPE', force_overwrite = false)
|
30
31
|
```
|
31
32
|
|
32
33
|
Some explanation about the parameters: <br />
|
33
34
|
|
34
35
|
* `object`: The ruby object which you want to export into files. <br />
|
35
36
|
* `export_path`(optional): The path where you want to generate your export file. Default will be './example.FILE_TYPE' such as './example.yml' <br />
|
36
|
-
* `
|
37
|
+
* `force_overwrite`(optional): This option is ```false``` by default. If you set it to true, it will automaticlly overwrite existing file `without` any interrupt warning. <br />
|
37
38
|
|
38
39
|
## Here are some samples:
|
39
40
|
|
data/lib/to_file/version.rb
CHANGED
data/lib/to_file.rb
CHANGED
@@ -1,43 +1,69 @@
|
|
1
1
|
require "to_file/version"
|
2
2
|
require 'yaml'
|
3
|
+
require 'json'
|
3
4
|
|
4
5
|
module ToFile
|
5
6
|
class To
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
class << self
|
8
|
+
# type hash should be {input_file_type: export_file_suffix}
|
9
|
+
type_hash = {
|
10
|
+
yml: "yml",
|
11
|
+
yaml: "yml",
|
12
|
+
json: "json"
|
13
|
+
}
|
10
14
|
|
11
|
-
|
15
|
+
type_hash.each do |key, value|
|
16
|
+
define_method :"#{key}" do |target_object, export_path = "./example.#{value}", force_overwrite = false|
|
17
|
+
if !force_overwrite && File.exist?(export_path)
|
18
|
+
puts 'Export canceled!'; exit unless overwrite_confirm?(export_path)
|
19
|
+
end
|
12
20
|
|
13
|
-
|
14
|
-
|
21
|
+
File.open(export_path, 'w') { |f| f.write send("to_#{value}", target_object)}
|
22
|
+
|
23
|
+
puts "\e[32m successfully exported to ---> " + File.expand_path(export_path) + "\e[0m"
|
24
|
+
end
|
25
|
+
end
|
15
26
|
|
16
|
-
|
27
|
+
private
|
17
28
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
29
|
+
def to_yml(target_object)
|
30
|
+
begin
|
31
|
+
target_object.to_yaml
|
32
|
+
rescue => e
|
33
|
+
raise ArgumentError, error_msg("yaml", e.message)
|
34
|
+
end
|
24
35
|
end
|
25
|
-
end
|
26
36
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
37
|
+
def to_json(target_object)
|
38
|
+
begin
|
39
|
+
JSON.pretty_generate target_object
|
40
|
+
rescue => e
|
41
|
+
raise ArgumentError, error_msg("json", e.message)
|
42
|
+
end
|
43
|
+
end
|
32
44
|
|
33
|
-
|
45
|
+
def error_msg(data_type, err)
|
46
|
+
'target_object can not be transformed to ' +
|
47
|
+
data_type.to_s +
|
48
|
+
' format! And the error msg is: ' +
|
49
|
+
err.to_s
|
50
|
+
end
|
51
|
+
|
52
|
+
def overwrite_confirm?(export_path)
|
53
|
+
msg = "\033[31mWARNING '" + export_path + "' is already existing, do you"
|
54
|
+
msg += " want to overwrite it ??? please press 'y' or 'n' to confirm!( "
|
55
|
+
msg += "tip: overwrite feature can be turned off manually by parameter): \033[0m"
|
56
|
+
puts msg
|
34
57
|
|
35
|
-
until %w(y n).include? prompt.downcase do
|
36
|
-
puts "\033[31mInvalid input, please input 'y' or 'n'\033[0m"
|
37
58
|
prompt = STDIN.gets.chomp
|
38
|
-
end
|
39
59
|
|
40
|
-
|
60
|
+
until %w(y n).include? prompt.downcase do
|
61
|
+
puts "\033[31mInvalid input, please input 'y' or 'n'\033[0m"
|
62
|
+
prompt = STDIN.gets.chomp
|
63
|
+
end
|
64
|
+
|
65
|
+
prompt.downcase == 'y' ? true : false
|
66
|
+
end
|
41
67
|
end
|
42
68
|
end
|
43
69
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: to_file
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- feiyang yu
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|