cheeba 1.0.4

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.
Files changed (95) hide show
  1. data/.git/COMMIT_EDITMSG +1 -0
  2. data/.git/HEAD +1 -0
  3. data/.git/config +9 -0
  4. data/.git/description +1 -0
  5. data/.git/hooks/applypatch-msg.sample +15 -0
  6. data/.git/hooks/commit-msg.sample +24 -0
  7. data/.git/hooks/post-commit.sample +8 -0
  8. data/.git/hooks/post-receive.sample +15 -0
  9. data/.git/hooks/post-update.sample +8 -0
  10. data/.git/hooks/pre-applypatch.sample +14 -0
  11. data/.git/hooks/pre-commit.sample +18 -0
  12. data/.git/hooks/pre-rebase.sample +169 -0
  13. data/.git/hooks/prepare-commit-msg.sample +36 -0
  14. data/.git/hooks/update.sample +107 -0
  15. data/.git/index +0 -0
  16. data/.git/info/exclude +7 -0
  17. data/.git/logs/HEAD +7 -0
  18. data/.git/logs/refs/heads/master +6 -0
  19. data/.git/logs/refs/remotes/origin/master +6 -0
  20. data/.git/objects/02/38dae53aee5cb4ea157a3f036b93868ede9593 +0 -0
  21. data/.git/objects/02/9cafaafb83e528381f0f2f02c6f23799bd1942 +0 -0
  22. data/.git/objects/07/c654f9ff6c65626369b061c595be42ccdd6cd7 +0 -0
  23. data/.git/objects/07/dbbace3b24b2d635d2fb637581931d573fc75c +0 -0
  24. data/.git/objects/08/ce1195151ff5a51573a871947ecd913fb5aa0f +1 -0
  25. data/.git/objects/0c/ad63d44505490e1a30ad075099a25020d7a7c5 +0 -0
  26. data/.git/objects/12/215629eba6a8f0fcd849923e98a471984521e1 +2 -0
  27. data/.git/objects/18/60fee2fadb59a6cbcb7f7dd0a6f6dc3b56ac79 +0 -0
  28. data/.git/objects/1e/1800628b80bef5a8043e4adacd6ebc6bab9aa1 +0 -0
  29. data/.git/objects/27/97bc2b3d72dc6d77b7bd07f1cca96f7e06b4ed +0 -0
  30. data/.git/objects/28/41d3f97c8aa8c942a613e1c69ed4280d78cf0a +0 -0
  31. data/.git/objects/29/f0c4074f9ecc894edd7ed507a1a92499838a6d +0 -0
  32. data/.git/objects/2a/8948661c63f2bef24f330638d239c88d5d0c2e +0 -0
  33. data/.git/objects/3f/7142cf3878c65e71b54d3572d82837d20f2aa7 +4 -0
  34. data/.git/objects/40/8c36a3103bc77e90db86dc06b1598e3baa4c2d +0 -0
  35. data/.git/objects/42/eb5558727a2144e7c5e997d3e6aea5486eaf37 +0 -0
  36. data/.git/objects/4c/6098fe05db7126c8a3c97f463910e5cc398630 +0 -0
  37. data/.git/objects/51/6902dffff8d29fa2f89979b9cd81ab275e1c67 +0 -0
  38. data/.git/objects/5b/502f8e20a0556c22727b504108a2dc36d7cbed +2 -0
  39. data/.git/objects/67/c3fb20cfae5320d83c9ebf2b85c1a3ef9b706b +0 -0
  40. data/.git/objects/72/7df703ac52c4ff97aa452feee27b9990430a11 +5 -0
  41. data/.git/objects/74/bc14252b2148d11877025d7f445d2fa686e7e9 +0 -0
  42. data/.git/objects/78/9ac5e1ca6a70ec3b8a11344e6f4a03e440f309 +0 -0
  43. data/.git/objects/7a/46bd1887d1a793094da678ded3b53b0208b826 +0 -0
  44. data/.git/objects/8d/77306c0d13e98da74ddf48002674fced7e1618 +2 -0
  45. data/.git/objects/94/36859cb89dd0bbddab77ed0810860e979b1c05 +0 -0
  46. data/.git/objects/ab/454c9537f329aecaedae0586c6d61146a7f819 +0 -0
  47. data/.git/objects/b9/3b5dde3104bb495dc0d2970bbe617dafb2f084 +0 -0
  48. data/.git/objects/be/197d3b2c14a52300cc4553bf2004ef3c45a45b +0 -0
  49. data/.git/objects/be/8e3e0ab878ffcb0456ae4cf66861888d933bf5 +0 -0
  50. data/.git/objects/c5/4c515f4bff81e08b01176b1a5743fd773a9740 +0 -0
  51. data/.git/objects/d4/a471f188d32adfa9dc2ff22c53b95319a162ed +0 -0
  52. data/.git/objects/d7/83399b37ca769b5b5bc2b33727fc47acfa5e4c +0 -0
  53. data/.git/objects/d7/a544fc5715014ef027875545ef0145e9f694fe +0 -0
  54. data/.git/objects/de/eef72f84f60beab439c58d84acc42d3e5a1279 +0 -0
  55. data/.git/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 +0 -0
  56. data/.git/objects/e9/9c40e89cb90f8067f99d7051c3bebf2b145ec7 +0 -0
  57. data/.git/objects/f3/89737a46e7cad94deb222e238e8f926767637e +0 -0
  58. data/.git/objects/fa/4f98b2fe387e262f9c28011e53f1b8fb920401 +0 -0
  59. data/.git/objects/fb/72047097c38625f3598fb348b3fbe75afd6d2d +1 -0
  60. data/.git/objects/ff/7aefe3472c61b77f58669ec4b8208412156930 +0 -0
  61. data/.git/refs/heads/master +1 -0
  62. data/.git/refs/remotes/origin/master +1 -0
  63. data/History.txt +6 -0
  64. data/Manifest.txt +94 -0
  65. data/README.txt +35 -0
  66. data/Rakefile +13 -0
  67. data/bin/cheeba +68 -0
  68. data/cheeba.gemspec +37 -0
  69. data/lib/cheeba/defaults.rb +78 -0
  70. data/lib/cheeba/errors.rb +10 -0
  71. data/lib/cheeba/indicators.rb +25 -0
  72. data/lib/cheeba/reader/builder.rb +265 -0
  73. data/lib/cheeba/reader/format.rb +130 -0
  74. data/lib/cheeba/reader/node.rb +61 -0
  75. data/lib/cheeba/reader/parser.rb +64 -0
  76. data/lib/cheeba/reader.rb +33 -0
  77. data/lib/cheeba/version.rb +3 -0
  78. data/lib/cheeba/writer/builder.rb +95 -0
  79. data/lib/cheeba/writer.rb +49 -0
  80. data/lib/cheeba.rb +50 -0
  81. data/test/files/arrays.cash +15 -0
  82. data/test/files/blank.cash +0 -0
  83. data/test/files/comments.cash +17 -0
  84. data/test/files/hashes.cash +26 -0
  85. data/test/files/malformed.cash +3 -0
  86. data/test/files/mixed.cash +12 -0
  87. data/test/files/split.cash +9 -0
  88. data/test/test_cheeba.rb +185 -0
  89. data/test/test_reader.rb +210 -0
  90. data/test/test_reader_builder.rb +345 -0
  91. data/test/test_reader_format.rb +406 -0
  92. data/test/test_reader_parser.rb +190 -0
  93. data/test/test_writer.rb +133 -0
  94. data/test/test_writer_builder.rb +150 -0
  95. metadata +167 -0
@@ -0,0 +1,130 @@
1
+ module Cheeba
2
+ module Reader
3
+ module Format
4
+ #
5
+ # format datatypes in the parsed hash
6
+ #
7
+ def self.format(phs)
8
+ self.adjust_options(phs)
9
+ self.stripper(phs)
10
+ self.key_to_int(phs)
11
+ self.val_to_int(phs)
12
+ self.key_to_sym(phs)
13
+ self.val_to_sym(phs)
14
+ self.key_to_true(phs)
15
+ self.val_to_true(phs)
16
+ phs
17
+ end
18
+
19
+ #
20
+ # strips keys and values
21
+ #
22
+ def self.stripper(phs)
23
+ psp = phs[:opt][:strip]
24
+ psk = phs[:opt][:strip_keys]
25
+ psv = phs[:opt][:strip_vals]
26
+ phs[:key] = phs[:key].to_s.strip if psp or psk
27
+ phs[:val] = phs[:val].to_s.strip if psp or psv
28
+ end
29
+
30
+ #
31
+ # adjusts options
32
+ #
33
+ def self.adjust_options(phs)
34
+ if phs[:opt][:auto_sym]
35
+ phs[:opt][:auto_sym_keys] = true
36
+ phs[:opt][:auto_sym_vals] = true
37
+ end
38
+ end
39
+
40
+ #
41
+ # true key
42
+ #
43
+ def self.key_to_true(phs)
44
+ x = []
45
+ x << phs[:opt][:auto_true].is_a?(TrueClass)
46
+ x << phs[:opt][:auto_true_keys].is_a?(TrueClass)
47
+ if x.any?
48
+ case
49
+ when (phs[:key].to_s.strip == "true"): phs[:key] = true
50
+ when (phs[:key].to_s.strip == "false"): phs[:key] = false
51
+ end
52
+ end
53
+ end
54
+
55
+ #
56
+ # true val
57
+ #
58
+ def self.val_to_true(phs)
59
+ x = []
60
+ x << phs[:opt][:auto_true].is_a?(TrueClass)
61
+ x << phs[:opt][:auto_true_vals].is_a?(TrueClass)
62
+ if x.any?
63
+ case
64
+ when (phs[:val].to_s.strip == "true"): phs[:val] = true
65
+ when (phs[:val].to_s.strip == "false"): phs[:val] = false
66
+ end
67
+ end
68
+ end
69
+
70
+ #
71
+ # symbolize key
72
+ #
73
+ def self.key_to_sym(phs)
74
+ is_str = (phs[:key] =~ /^\d*$/).nil?
75
+ x = []
76
+ x << (phs[:opt][:sym_str] && is_str).is_a?(TrueClass)
77
+ x << (phs[:opt][:sym_str_keys] && is_str).is_a?(TrueClass)
78
+ x << phs[:opt][:symbolize].is_a?(TrueClass)
79
+ x << (phs[:ask] && phs[:opt][:auto_sym_keys] && is_str).is_a?(TrueClass)
80
+ x << phs[:opt][:symbolize_keys].is_a?(TrueClass)
81
+ phs[:key] = phs[:key].to_s.to_sym if (x.any? && !phs[:key].to_s.strip.empty?)
82
+ end
83
+
84
+ #
85
+ # symbolize val
86
+ #
87
+ def self.val_to_sym(phs)
88
+ is_str = (phs[:val] =~ /^\d*$/).nil?
89
+ x = []
90
+ x << (phs[:opt][:sym_str] && is_str).is_a?(TrueClass)
91
+ x << (phs[:opt][:sym_str_vals] && is_str).is_a?(TrueClass)
92
+ x << phs[:opt][:symbolize].is_a?(TrueClass)
93
+ x << (phs[:asv] && phs[:opt][:auto_sym_vals] && is_str).is_a?(TrueClass)
94
+ x << phs[:opt][:symbolize_vals].is_a?(TrueClass)
95
+ phs[:val] = phs[:val].to_s.to_sym if (x.any? && !phs[:val].to_s.strip.empty?)
96
+ end
97
+
98
+ #
99
+ # key is parsed as string, so try to_i
100
+ #
101
+ def self.key_to_int(phs)
102
+ x = []
103
+ x << phs[:opt][:int].is_a?(TrueClass)
104
+ x << phs[:opt][:int_keys].is_a?(TrueClass)
105
+ phs[:key] = self.string_to_int(phs[:key]) if x.any?
106
+ end
107
+
108
+ #
109
+ # val is parsed as string, so try to_i
110
+ #
111
+ def self.val_to_int(phs)
112
+ x = []
113
+ x << phs[:opt][:int].is_a?(TrueClass)
114
+ x << phs[:opt][:int_vals].is_a?(TrueClass)
115
+ phs[:val] = self.string_to_int(phs[:val]) if x.any?
116
+ end
117
+
118
+ #
119
+ # returns int if string is convertable
120
+ #
121
+ def self.string_to_int(string)
122
+ if string.to_i.to_s == string.to_s.gsub(/^0+/, "")
123
+ string.to_i
124
+ else
125
+ string
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,61 @@
1
+ module Cheeba
2
+ module Reader
3
+ module Node
4
+ #
5
+ # hashpair
6
+ #
7
+ def self.hashpair(hsh, sym, spc)
8
+ /^(#{spc}*)(\S*)#{hsh}#{spc}*(.*)#{spc}*$/
9
+ end
10
+
11
+ #
12
+ # hashpair, both key and val are symbol
13
+ #
14
+ def self.hashpair_sym_all(hsh, sym, spc)
15
+ /^(#{spc}*)(#{sym}\S*)#{hsh}#{spc}*(#{sym}.*)#{spc}*$/
16
+ end
17
+
18
+ #
19
+ # hashpair, key is symbol
20
+ #
21
+ def self.hashpair_sym_key(hsh, sym, spc)
22
+ /^(#{spc}*)(#{sym}\S*)#{hsh}#{spc}*(.*)#{spc}*$/
23
+ end
24
+
25
+ #
26
+ # hashpair, value is symbol
27
+ #
28
+ def self.hashpair_sym_val(hsh, sym, spc)
29
+ /^(#{spc}*)(\S*)#{hsh}#{spc}*(#{sym}.*)#{spc}*$/
30
+ end
31
+
32
+ #
33
+ # hashkey is symbol
34
+ #
35
+ def self.hashkey_sym(hsh, sym, spc)
36
+ /^(#{spc}*)(#{sym}\S*)#{hsh}#{spc}*$/
37
+ end
38
+
39
+ #
40
+ # hashkey is awesome
41
+ #
42
+ def self.hashkey(hsh, spc)
43
+ /^(#{spc}*)(\S*)#{hsh}#{spc}*$/
44
+ end
45
+
46
+ #
47
+ # docstart and docterm
48
+ #
49
+ def self.document(idc, spc)
50
+ /^#{spc}*#{idc}#{spc}*$/
51
+ end
52
+
53
+ #
54
+ # comments and arrays
55
+ #
56
+ def self.left_match(idc, spc)
57
+ /^(#{spc}*)#{idc}(#{spc}*)(.*)#{spc}*$/
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,64 @@
1
+ module Cheeba
2
+ module Reader
3
+ module Parser
4
+ class IndentError < StandardError; end
5
+
6
+ def self.parse(line, opts, num)
7
+ results = self.parse_line(line, opts)
8
+ phs = self.results_to_hash(results, opts)
9
+ self.check_indent(line, phs[:spc], opts[:indent], num)
10
+ phs
11
+ end
12
+
13
+ ##
14
+ # invalid number of spaces, line num and truncated line for context
15
+ #
16
+ def self.check_indent(line, spc, idt, num)
17
+ raise IndentError, "#{num} #{line[1..50]}" if spc % idt != 0
18
+ end
19
+
20
+ ##
21
+ # parses line, returns array, dependent on Node module
22
+ #
23
+ def self.parse_line(line, opts)
24
+ chb = Cheeba::Reader::Node
25
+ spc = opts[:space]
26
+ hsh = opts[:hash]
27
+ sym = opts[:symbol]
28
+ dcs = opts[:doc_start]
29
+ dct = opts[:doc_term]
30
+ arr = opts[:array]
31
+ com = opts[:comment]
32
+
33
+ case line
34
+ when chb.left_match(arr, spc): [:arr, Regexp.last_match.captures]
35
+ when chb.left_match(com, spc): [:com, Regexp.last_match.captures]
36
+ when chb.hashkey_sym(hsh, sym, spc): [:hky, Regexp.last_match.captures, nil, true]
37
+ when chb.hashkey(hsh, spc): [:hky, Regexp.last_match.captures]
38
+ when chb.hashpair_sym_all(hsh, sym, spc): [:hpr, Regexp.last_match.captures, true, true]
39
+ when chb.hashpair_sym_key(hsh, sym, spc): [:hpr, Regexp.last_match.captures, true]
40
+ when chb.hashpair_sym_val(hsh, sym, spc): [:hpr, Regexp.last_match.captures, nil, true]
41
+ when chb.hashpair(hsh, sym, spc): [:hpr, Regexp.last_match.captures]
42
+ when /^\s*$/: [:bla]
43
+ when chb.document(dcs, spc): [:dcs]
44
+ when chb.document(dct, spc): [:dct]
45
+ else; [:mal, nil, nil, line]
46
+ end
47
+ end
48
+
49
+ ##
50
+ # creates hash with array vals, includes options
51
+ #
52
+ def self.results_to_hash(results, opt)
53
+ msg, spc, key, val, ask, asv = results.flatten
54
+ { :msg => msg,
55
+ :spc => (spc.nil? ? 0 : spc.length),
56
+ :key => key.to_s.sub(/^:/, ""),
57
+ :val => val.to_s.sub(/^:/, ""),
58
+ :ask => ask,
59
+ :asv => asv,
60
+ :opt => opt}
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,33 @@
1
+ require 'cheeba/reader/node'
2
+ require 'cheeba/reader/format'
3
+ require 'cheeba/reader/builder'
4
+ require 'cheeba/reader/parser'
5
+
6
+ module Cheeba
7
+ module Reader
8
+ class EmptyFileError < StandardError; end
9
+ class EmptyInputError < StandardError; end
10
+ class InvalidInputError < StandardError; end
11
+ #
12
+ # parses file or string into Ruby Array, or Hash
13
+ #
14
+ def self.read(input, options)
15
+ raise Cheeba::Reader::EmptyInputError if (input.is_a?(String) && input.strip.empty?)
16
+ raise Cheeba::Reader::InvalidInputError unless (input.is_a?(String) or input.is_a?(File))
17
+ input = IO.readlines(input) if File.exists?(input)
18
+ raise Cheeba::Reader::EmptyFileError if input.empty?
19
+ hash = {}
20
+ lineno = 0
21
+ input.each {|line|
22
+ parsed_hash = Cheeba::Reader::Parser.parse(line, options, (lineno += 1))
23
+ formatted_hash = Cheeba::Reader::Format.format(parsed_hash)
24
+ Cheeba::Reader::Builder.build(hash, formatted_hash)
25
+ }
26
+ hash.delete(:adr)
27
+ case
28
+ when options[:list]: hash
29
+ when !options[:list]: hash.delete(:lst) && (hash.length == 1) ? hash[1] : hash
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Cheeba
2
+ VERSION = '1.0.4'
3
+ end
@@ -0,0 +1,95 @@
1
+ module Cheeba
2
+ module Writer
3
+ module Builder
4
+ def self.build(arr, inp, opt)
5
+ spc = (opt[:space] * opt[:indent])
6
+ case
7
+ when inp.is_a?(Hash): self.hash_to_array(arr, inp, opt, spc)
8
+ when inp.is_a?(Array): self.array_to_lines(arr, inp, opt, spc)
9
+ when inp.is_a?(String): self.string_to_array(arr, inp, opt, spc)
10
+ end
11
+ arr
12
+ end
13
+
14
+ def self.sym_to_str(x, y = nil)
15
+ a = x.is_a?(Symbol) ? x.inspect : x
16
+ b = (!y.nil? && y.is_a?(Symbol)) ? y.inspect : y
17
+ [a, b]
18
+ end
19
+
20
+ def self.string_to_array(arr, inp, opt, spc)
21
+ self.array_to_lines(arr, inp.split(opt[:line_end]), opt, spc)
22
+ end
23
+
24
+ def self.hash_to_array(arr, inp, opt, spc)
25
+ inp.each do |key,val|
26
+ case
27
+ when val.is_a?(Hash)
28
+ key = self.sym_to_str(key)
29
+ arr << opt[:doc_start] if opt[:docs]
30
+ arr << "#{key}#{opt[:hash]}" unless opt[:docs]
31
+ self.hash_to_lines(arr, val, val.keys, opt, spc, (opt[:docs] ? 0 : 1))
32
+ when val.is_a?(Array)
33
+ arr << opt[:doc_start] if opt[:docs]
34
+ self.array_to_lines(arr, val, opt)
35
+ else
36
+ key, val = self.sym_to_str(key, val)
37
+ arr << "#{key}#{opt[:hash]} #{val}"
38
+ end
39
+ end
40
+ end
41
+
42
+ #
43
+ # line is array, push each indice to array, assumes every indice is a string
44
+ #
45
+ def self.array_to_lines(arr, val, opt, spc, idt = nil)
46
+ idt = 0 if idt.nil?
47
+ ist = spc * idt
48
+ ind = opt[:array]
49
+ until val.empty? do
50
+ x = val.shift
51
+ case
52
+ when x.is_a?(Hash)
53
+ arr << "#{ist}#{ind}"
54
+ self.hash_to_lines(arr, x, x.keys, opt, spc, (idt + 1))
55
+ when x.is_a?(Array)
56
+ i = idt
57
+ arr << "#{ist}#{ind}" if opt[:yaml]
58
+ i += 1
59
+ self.array_to_lines(arr, x, opt, spc, i)
60
+ else
61
+ x = self.sym_to_str(x)
62
+ arr << "#{ist}#{ind} #{x}"
63
+ end
64
+ end
65
+ end
66
+
67
+ #
68
+ # recursively add lines to array
69
+ #
70
+ def self.hash_to_lines(arr, hsh, kys, opt, spc, idt = nil)
71
+ idt = 0 if idt.nil?
72
+ while !kys.empty?
73
+ ist = spc * idt
74
+ key = kys.shift
75
+ val = hsh[key]
76
+ ind = opt[:hash]
77
+ case
78
+ when val.is_a?(Hash)
79
+ # push key into lines_array
80
+ arr << "#{ist}#{key}#{ind}"
81
+ # step indent
82
+ # call this again (the recursive part)
83
+ self.hash_to_lines(arr, val, val.keys, opt, spc, (idt + 1))
84
+ when val.is_a?(Array)
85
+ arr << "#{ist}#{key}#{ind}"
86
+ self.array_to_lines(arr, val, opt, spc, (idt + 1))
87
+ else
88
+ # if not Writer or Array, it must be String
89
+ arr << "#{ist}#{key}#{ind} #{val}"
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,49 @@
1
+ require 'cheeba/writer/builder'
2
+
3
+ module Cheeba
4
+ module Writer
5
+ class EmptyFilenameError < StandardError; end
6
+ class EmptyInputError < StandardError; end
7
+ class InvalidInputError < StandardError; end
8
+ class InvalidFilenameError < StandardError; end
9
+ #
10
+ # array, hash, or string to array of lines of .cash file
11
+ #
12
+ def self.build(object, options)
13
+ raise Cheeba::Writer::EmptyInputError if (object.is_a?(String) && object.strip.empty?)
14
+ raise Cheeba::Writer::InvalidInputError unless (object.is_a?(String) or object.is_a?(Array) or object.is_a?(Hash))
15
+ Cheeba::Writer::Builder.build([], object, options)
16
+ end
17
+
18
+ #
19
+ # array, hash, or string to file
20
+ #
21
+ def self.write(object, filename, options)
22
+ filename = "#{ENV['HOME']}/#{File.basename(filename.to_s)}" if (filename =~ /^~/)
23
+ raise Cheeba::Writer::EmptyInputError if (object.is_a?(String) && object.strip.empty?)
24
+ raise Cheeba::Writer::InvalidInputError unless (object.is_a?(String) or object.is_a?(Array) or object.is_a?(Hash))
25
+ raise Cheeba::Writer::EmptyFilenameError if File.basename(filename.to_s).strip.empty?
26
+ raise Cheeba::Writer::InvalidFilenameError unless File.exists?(File.dirname(filename))
27
+ File.open(filename, "w") do |file|
28
+ self.build(object, options).each {|line|
29
+ file.print("#{line}\n")
30
+ }
31
+ end
32
+ filename
33
+ end
34
+
35
+ #
36
+ # write a cheeba dotfile to home dir
37
+ #
38
+ def self.dotfile(options, home)
39
+ filename = "#{home.chomp("/")}/.cheeba"
40
+ new_name = nil
41
+ if File.exists?(filename)
42
+ new_name = "#{filename}.#{Time.now.strftime("%Y%m%d%H%M%S")}"
43
+ File.rename(filename, new_name)
44
+ end
45
+ self.write(options, filename, Cheeba::Indicators.options)
46
+ new_name ? new_name : filename
47
+ end
48
+ end
49
+ end
data/lib/cheeba.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'cheeba/version'
2
+ require 'cheeba/reader'
3
+ require 'cheeba/writer'
4
+ require 'cheeba/indicators'
5
+ require 'cheeba/defaults'
6
+
7
+ module Cheeba
8
+ DOTFILE = "#{ENV['HOME']}/.cheeba"
9
+ #
10
+ # File to Hash or Array
11
+ #
12
+ def self.read(input, options = {})
13
+ Cheeba::Reader.read(input, self.options(options))
14
+ end
15
+
16
+ #
17
+ # Hash or Array to .cash Array
18
+ #
19
+ def self.parse(object, options = {})
20
+ Cheeba::Writer.build(object, self.options(options))
21
+ end
22
+
23
+ #
24
+ # Hash or Array to .cash-file, e.g. filename.cash
25
+ #
26
+ def self.write(object, filename, options = {})
27
+ Cheeba::Writer.write(object, filename, self.options(options))
28
+ end
29
+
30
+ #
31
+ # writes a .cheeba file HOME, merges with options if :dot is true
32
+ #
33
+ def self.dotfile(home = ENV['HOME'])
34
+ Cheeba::Writer.dotfile(Cheeba::Defaults.options, home)
35
+ end
36
+
37
+ private
38
+
39
+ #
40
+ # options
41
+ #
42
+ def self.options(options = {})
43
+ opt = Cheeba::Defaults.options.merge(Cheeba::Indicators.options.merge(options))
44
+ dot_opt = File.exists?(DOTFILE) ? Cheeba::Reader.read(DOTFILE, opt.merge({:symbolize_keys => true})) : nil
45
+ unless opt[:dot]
46
+ opt = opt.merge(dot_opt) if (dot_opt && dot_opt[:dot])
47
+ end
48
+ opt
49
+ end
50
+ end
@@ -0,0 +1,15 @@
1
+ -
2
+ - 1
3
+ - 2
4
+ -
5
+ -
6
+ -
7
+ - awesome
8
+ - awesome
9
+ - 4
10
+ - 5
11
+ - 6
12
+ - 7
13
+ - 8
14
+ - dude
15
+ -
File without changes
@@ -0,0 +1,17 @@
1
+ # dude
2
+ 1: 1
3
+ # awesome
4
+ 2: 2
5
+ #
6
+ # camaro
7
+ #
8
+ 3: 3
9
+ ##
10
+ # nice
11
+ # one
12
+ # bro!
13
+ #
14
+ 4:
15
+ - 1
16
+ - 2
17
+ - 3
@@ -0,0 +1,26 @@
1
+ grocery_list:
2
+ beer:
3
+ 1:
4
+ model: 24 pack - Coors Lite
5
+ count: 1
6
+ meatsez:
7
+ 1:
8
+ model: Spam
9
+ count: 5
10
+ 2:
11
+ model: Log of ground beef
12
+ count: 1
13
+ cigarettes:
14
+ 1:
15
+ model: Carton - Basic Ultra Menthol Box 100
16
+ count: 2
17
+ other:
18
+ 1:
19
+ model: Economy-Size Pork & Beans
20
+ count: 2
21
+ 2:
22
+ model: Jumbo Miracle Whip
23
+ count: 1
24
+ 3:
25
+ model: White Wonder Bread
26
+ count: 2
@@ -0,0 +1,3 @@
1
+ 2341234
2
+ awesome
3
+ dude
@@ -0,0 +1,12 @@
1
+ 1:1
2
+ 2:
3
+ -
4
+ - one
5
+ - two
6
+ 5:
7
+ 6:6
8
+ 7:7
9
+ 8:
10
+ dude:dude
11
+ -
12
+ 3: awesome
@@ -0,0 +1,9 @@
1
+ 1:1
2
+ 2:3
3
+ 3:3
4
+ - 1
5
+ - 2
6
+ - 3
7
+ 1:1
8
+ 2:2
9
+ 3:3