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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d2a2870a34570aa5e49c456137e02c233cfe1faf
4
- data.tar.gz: 955bd53c545fd715e9a9820834411cb52ab2352c
3
+ metadata.gz: 656b1a7419e8679c8e7499f2fb3b3d8950b7a547
4
+ data.tar.gz: e424893fdd05503dec8756e8aec4dcc15fd4fcb5
5
5
  SHA512:
6
- metadata.gz: 8e93ac6e14afc36a7a8c92fe0c17df9811a7924e9a26b6511a37a0fe513fe1356f78e62ff53e00eaa0d10fc5da74305fbfe2e2b8dc00a77ef5df92636923121a
7
- data.tar.gz: 2554e46fb969e624006d9005f1162e728190053f2243344f7604baea3b68071fa5afce840b04591b090a562bb8c9b1d5db337158f1c67f124f0323298bee6dc6
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.00105 seconds |
4
- ./spec/to_file_spec.rb[1:2] | passed | 0.01174 seconds |
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. </br>
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', confirmation = true)
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
- * `confirmation`(optional): To confirm if you want to overwrite the existing file from export_path. <br />
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
 
@@ -1,3 +1,3 @@
1
1
  module ToFile
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.1"
3
3
  end
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
- def self.yml(target_object, export_path = './example.yml', confirmation = true)
7
- if confirmation && File.exist?(export_path)
8
- puts 'Export canceled!'; exit unless confirm?(export_path)
9
- end
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
- File.open(export_path, 'w') { |f| f.write self.to_yml(target_object)}
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
- puts "\e[32m successfully exported to ---> " + File.expand_path(export_path) + "\e[0m"
14
- end
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
- private
27
+ private
17
28
 
18
- def self.to_yml(target_object)
19
- begin
20
- target_object.to_yaml
21
- rescue
22
- raise ArgumentError,
23
- 'target_object can not be transformed to yaml format!'
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
- def self.confirm?(export_path)
28
- msg = "\033[31mWARNING '" + export_path + "' is already existing, do you"
29
- msg += " want to overwrite it ??? please press 'y' or 'n' to confirm!( "
30
- msg += "tip: confirm can be turned off by parameter): \033[0m"
31
- puts msg
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
- prompt = STDIN.gets.chomp
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
- prompt.downcase == 'y' ? true : false
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.0.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-01-23 00:00:00.000000000 Z
11
+ date: 2018-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler