roject 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +0 -5
- data/exp/loadsaveable/foo.json +3 -0
- data/exp/loadsaveable/foo.yaml +2 -0
- data/lib/loadsaveable.rb +2 -0
- data/lib/parsers.rb +44 -2
- data/lib/roject.rb +1 -1
- data/spec/parsers_spec.rb +46 -1
- data/spec/shared/loadsaveable_spec.rb +63 -7
- data/spec/spec_require.rb +39 -0
- metadata +4 -3
- data/exp/project.json +0 -3
- /data/exp/{project.foo → loadsaveable/foo.bar} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 474139482a42d0581c7cfbfe88814a9c40d8d947
|
4
|
+
data.tar.gz: 5f55210e15a6a37eb12611e6baf3cf38d8a8380d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e17d269ae387d6db4d9ffef73f9bc6763b5fefc08477af90c1f3a9f240c4815e51fcb879bf6a1384880d9777b3fbfc99524b5741537d4f24db2026bc0ef9c3c6
|
7
|
+
data.tar.gz: fb3c33d37ae4b9a6a8c57185b68ff3dd148e55e0dd847be01b4430ba18334a30faf738af08ae7772ba363c7782f8cf15eeb611e76fec72aaca7f2aacd6404ac3
|
data/Rakefile
CHANGED
data/lib/loadsaveable.rb
CHANGED
@@ -26,6 +26,8 @@ module Roject
|
|
26
26
|
#
|
27
27
|
# Note: Implementing objects need to respond to the :hash method
|
28
28
|
# which returns a hash of the data that needs to be saved
|
29
|
+
# and a private config method which sets the data to the
|
30
|
+
# given hash (for unit testing)
|
29
31
|
#
|
30
32
|
# Author: Anshul Kharbanda
|
31
33
|
# Created: 7 - 10 - 2016
|
data/lib/parsers.rb
CHANGED
@@ -12,6 +12,7 @@ Created: 7 - 8 - 2016
|
|
12
12
|
=end
|
13
13
|
|
14
14
|
require "json"
|
15
|
+
require "yaml"
|
15
16
|
|
16
17
|
# Roject is a programming project manager written in Ruby.
|
17
18
|
#
|
@@ -62,14 +63,54 @@ module Roject
|
|
62
63
|
# Parameter: text - the json text to parse
|
63
64
|
#
|
64
65
|
# Return: the hash parsed from the text
|
65
|
-
def self.parse(text); JSON.parse
|
66
|
+
def self.parse(text); JSON.parse text, symbolize_names: true; end
|
66
67
|
|
67
68
|
# Returns the given hash formatted to pretty json
|
68
69
|
#
|
69
70
|
# Parameter: hash - the hash to format
|
70
71
|
#
|
71
72
|
# Return: the given hash formatted to pretty json
|
72
|
-
def self.format(hash); JSON.pretty_generate
|
73
|
+
def self.format(hash); JSON.pretty_generate hash, indent: "\t"; end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Parses YAML files
|
77
|
+
#
|
78
|
+
# Author: Anshul Kharbanda
|
79
|
+
# Created: 7 - 13 - 2016
|
80
|
+
class YAMLParser < Parser
|
81
|
+
# Parses the given yaml text into a hash
|
82
|
+
#
|
83
|
+
# Parameter: text - the yaml text to parse
|
84
|
+
#
|
85
|
+
# Return: the hash parsed from the text
|
86
|
+
def self.parse(text); symbolized YAML.load text; end
|
87
|
+
|
88
|
+
# Returns the given hash formatted to yaml
|
89
|
+
#
|
90
|
+
# Parameter: hash - the hash to format
|
91
|
+
#
|
92
|
+
# Return: the given hash formatted to yaml
|
93
|
+
def self.format(hash); YAML.dump stringified hash end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
# Returns the hash with all of the keys converted to symbols
|
98
|
+
#
|
99
|
+
# Parameter: hash - the hash to symbolize
|
100
|
+
#
|
101
|
+
# Return: the hash with all of the keys converted to symbols
|
102
|
+
def self.symbolized hash
|
103
|
+
hash.each_pair.collect { |k, v| [k.to_sym, v.is_a?(Hash) ? symbolized(v) : v] }.to_h
|
104
|
+
end
|
105
|
+
|
106
|
+
# Returns the hash with all of the keys converted to strings
|
107
|
+
#
|
108
|
+
# Parameter: hash - the hash to stringify
|
109
|
+
#
|
110
|
+
# Return: the hash with all of the keys converted to strings
|
111
|
+
def self.stringified hash
|
112
|
+
hash.each_pair.collect { |k, v| [k.to_s, v.is_a?(Hash) ? stringified(v) : v] }.to_h
|
113
|
+
end
|
73
114
|
end
|
74
115
|
|
75
116
|
#-----------------------------------------GET------------------------------------------
|
@@ -82,6 +123,7 @@ module Roject
|
|
82
123
|
def self.get(filename)
|
83
124
|
case File.extname(filename)
|
84
125
|
when ".json" then return JSONParser
|
126
|
+
when ".yaml" then return YAMLParser
|
85
127
|
|
86
128
|
# Raise error if extension is not supported
|
87
129
|
else raise LoadError, "#{File.extname(filename)} not supported!"
|
data/lib/roject.rb
CHANGED
data/spec/parsers_spec.rb
CHANGED
@@ -67,7 +67,51 @@ describe Roject::Parsers do
|
|
67
67
|
#
|
68
68
|
# Return: the given hash formatted to pretty json
|
69
69
|
describe '::format' do
|
70
|
-
it 'formats the given hash into pretty
|
70
|
+
it 'formats the given hash into pretty json' do
|
71
|
+
expect(@parser.format(@hash)).to eql @text
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Describing Roject::Parsers::YAMLParser
|
77
|
+
#
|
78
|
+
# Parses JSON files
|
79
|
+
#
|
80
|
+
# Author: Anshul Kharbanda
|
81
|
+
# Created: 7 - 11 - 2016
|
82
|
+
describe '::YAMLParser' do
|
83
|
+
|
84
|
+
#----------------------------------BEFORE-----------------------------------
|
85
|
+
|
86
|
+
before :all do
|
87
|
+
@text = YAML.dump(stringified(@hash))
|
88
|
+
@parser = Roject::Parsers::YAMLParser
|
89
|
+
end
|
90
|
+
|
91
|
+
#----------------------------------METHODS----------------------------------
|
92
|
+
|
93
|
+
# Describing Roject::Parsers::YAMLParser::parse
|
94
|
+
#
|
95
|
+
# Parses the given yaml text into a hash
|
96
|
+
#
|
97
|
+
# Parameter: text - the yaml text to parse
|
98
|
+
#
|
99
|
+
# Return: the hash parsed from the text
|
100
|
+
describe '::parse' do
|
101
|
+
it 'parses the given YAML text into a ruby hash' do
|
102
|
+
expect(@parser.parse(@text)).to eql @hash
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# Describing Roject::Parsers::YAMLParser::format
|
107
|
+
#
|
108
|
+
# Returns the object hash formatted to yaml
|
109
|
+
#
|
110
|
+
# Parameter: hash - the hash to format
|
111
|
+
#
|
112
|
+
# Return: the given hash formatted to yaml
|
113
|
+
describe '::format' do
|
114
|
+
it 'formats the given hash into yaml' do
|
71
115
|
expect(@parser.format(@hash)).to eql @text
|
72
116
|
end
|
73
117
|
end
|
@@ -78,6 +122,7 @@ describe Roject::Parsers do
|
|
78
122
|
describe '::get' do
|
79
123
|
it 'returns the appropriate parser according to the extension of the given filename' do
|
80
124
|
expect(Roject::Parsers.get("foo.json")).to eql Roject::Parsers::JSONParser
|
125
|
+
expect(Roject::Parsers.get("foo.yaml")).to eql Roject::Parsers::YAMLParser
|
81
126
|
end
|
82
127
|
end
|
83
128
|
end
|
@@ -27,11 +27,12 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
27
27
|
#---------------------------------------BEFORE----------------------------------------
|
28
28
|
|
29
29
|
before :all do
|
30
|
-
@dir = "exp"
|
31
|
-
@default_hash = {
|
32
|
-
@modded_hash = {
|
33
|
-
@pjson_name = "#{@dir}/
|
34
|
-
@
|
30
|
+
@dir = "exp/loadsaveable"
|
31
|
+
@default_hash = { name: "project" }
|
32
|
+
@modded_hash = { name: "superproject", author: "Anshul Kharbanda" }
|
33
|
+
@pjson_name = "#{@dir}/foo.json"
|
34
|
+
@pyaml_name = "#{@dir}/foo.yaml"
|
35
|
+
@phony_name = "#{@dir}/foo.bar"
|
35
36
|
end
|
36
37
|
|
37
38
|
#--------------------------------------METHODS-----------------------------------------
|
@@ -80,6 +81,14 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
80
81
|
end
|
81
82
|
end
|
82
83
|
|
84
|
+
context "with a .yaml filename given" do
|
85
|
+
it "returns a new #{loadsaveable_class.name} from the yaml file with the given filename" do
|
86
|
+
project = loadsaveable_class.load(@pyaml_name)
|
87
|
+
expect(project).to be_an_instance_of loadsaveable_class
|
88
|
+
expect(project.hash).to eql read_yaml(@pyaml_name)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
83
92
|
#-----------------------------UNSUPPORTED-----------------------------
|
84
93
|
|
85
94
|
context "with an unsupported file extension given" do
|
@@ -113,6 +122,13 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
113
122
|
end
|
114
123
|
end
|
115
124
|
|
125
|
+
context "with a .yaml filename given" do
|
126
|
+
it "saves the project to the given filename in yaml format" do
|
127
|
+
@project.save @pyaml_name
|
128
|
+
expect(read_yaml(@pyaml_name)).to eql @modded_hash
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
116
132
|
#-----------------------------UNSUPPORTED-----------------------------
|
117
133
|
|
118
134
|
context "with an unsupported file extension given" do
|
@@ -125,6 +141,7 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
125
141
|
|
126
142
|
after :all do
|
127
143
|
write_json @pjson_name, @default_hash
|
144
|
+
write_yaml @pyaml_name, @default_hash
|
128
145
|
end
|
129
146
|
end
|
130
147
|
|
@@ -169,7 +186,37 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
169
186
|
|
170
187
|
it "saves the #{loadsaveable_class.name} when the block ends" do
|
171
188
|
# Check if the modded project was saved
|
172
|
-
expect(
|
189
|
+
expect(read_json(@pjson_name)).to eql @modded_hash
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
context "when file is .yaml" do
|
194
|
+
# Declare project local variable
|
195
|
+
project = nil
|
196
|
+
|
197
|
+
it "loads a #{loadsaveable_class.name} from the yaml file with the given filename (calling #load)" do
|
198
|
+
# Declare modded_hash local variable
|
199
|
+
mhash = @modded_hash
|
200
|
+
|
201
|
+
# Open project in a block
|
202
|
+
expect { loadsaveable_class.open @pyaml_name do
|
203
|
+
# Set project to instance
|
204
|
+
project = self
|
205
|
+
|
206
|
+
# Modify project
|
207
|
+
config mhash
|
208
|
+
end }.not_to raise_error
|
209
|
+
end
|
210
|
+
|
211
|
+
it "evaluates the given block in the context of the loaded #{loadsaveable_class.name}" do
|
212
|
+
# Check if project was actually self, and it was successfuly modified
|
213
|
+
expect(project).to be_an_instance_of loadsaveable_class
|
214
|
+
expect(project.hash).to eql @modded_hash
|
215
|
+
end
|
216
|
+
|
217
|
+
it "saves the #{loadsaveable_class.name} when the block ends" do
|
218
|
+
# Check if the modded project was saved
|
219
|
+
expect(read_yaml(@pyaml_name)).to eql @modded_hash
|
173
220
|
end
|
174
221
|
end
|
175
222
|
|
@@ -181,8 +228,9 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
181
228
|
end
|
182
229
|
end
|
183
230
|
|
184
|
-
after :all do
|
231
|
+
after :all do
|
185
232
|
write_json @pjson_name, @default_hash
|
233
|
+
write_yaml @pyaml_name, @default_hash
|
186
234
|
end
|
187
235
|
end
|
188
236
|
|
@@ -197,6 +245,14 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
197
245
|
end
|
198
246
|
end
|
199
247
|
|
248
|
+
context "when file is .yaml" do
|
249
|
+
it "loads a #{loadsaveable_class.name} from the yaml file with the given filename (calling #load) and returns it." do
|
250
|
+
project = loadsaveable_class.open @pyaml_name
|
251
|
+
expect(project).to be_an_instance_of loadsaveable_class
|
252
|
+
expect(project.hash).to eql @default_hash
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
200
256
|
#-----------------------------UNSUPPORTED-----------------------------
|
201
257
|
|
202
258
|
context "when filetype is unsupported" do
|
data/spec/spec_require.rb
CHANGED
@@ -13,6 +13,7 @@ Created: 7 - 8 - 2016
|
|
13
13
|
|
14
14
|
# Required libraries
|
15
15
|
require "json"
|
16
|
+
require "yaml"
|
16
17
|
|
17
18
|
# Required files for spec
|
18
19
|
require_relative "../lib/parsers"
|
@@ -21,6 +22,26 @@ require_relative "../lib/project"
|
|
21
22
|
# Spec shared examples
|
22
23
|
require_relative "shared/loadsaveable_spec"
|
23
24
|
|
25
|
+
#-----------------------HASHMOD METHODS------------------------
|
26
|
+
|
27
|
+
# Returns the hash with all of the keys converted to symbols
|
28
|
+
#
|
29
|
+
# Parameter: hash - the hash to symbolize
|
30
|
+
#
|
31
|
+
# Return: the hash with all of the keys converted to symbols
|
32
|
+
def symbolized hash
|
33
|
+
hash.each_pair.collect { |k, v| [k.to_sym, v.is_a?(Hash) ? symbolized(v) : v] }.to_h
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns the hash with all of the keys converted to strings
|
37
|
+
#
|
38
|
+
# Parameter: hash - the hash to stringify
|
39
|
+
#
|
40
|
+
# Return: the hash with all of the keys converted to strings
|
41
|
+
def stringified hash
|
42
|
+
hash.each_pair.collect { |k, v| [k.to_s, v.is_a?(Hash) ? stringified(v) : v] }.to_h
|
43
|
+
end
|
44
|
+
|
24
45
|
#------------------------READER METHODS------------------------
|
25
46
|
|
26
47
|
# Reads JSON from the file with the given filename
|
@@ -32,6 +53,15 @@ def read_json filename
|
|
32
53
|
JSON.parse IO.read(filename), symbolize_names: true
|
33
54
|
end
|
34
55
|
|
56
|
+
# Reads YAML from the file with the given filename
|
57
|
+
#
|
58
|
+
# Parameter: filename - the name of the file to read
|
59
|
+
#
|
60
|
+
# Return: hash parsed from YAML file
|
61
|
+
def read_yaml filename
|
62
|
+
symbolized YAML.load IO.read(filename)
|
63
|
+
end
|
64
|
+
|
35
65
|
#------------------------WRITER METHODS------------------------
|
36
66
|
|
37
67
|
# Writes the given hash as JSON to the file with the
|
@@ -41,4 +71,13 @@ end
|
|
41
71
|
# Parameter: hash - the hash to write
|
42
72
|
def write_json filename, hash
|
43
73
|
IO.write filename, JSON.pretty_generate(hash, indent: "\t")
|
74
|
+
end
|
75
|
+
|
76
|
+
# Writes the given hash as YAML to the file with the
|
77
|
+
# given filename
|
78
|
+
#
|
79
|
+
# Parameter: filename - the name of the file to write to
|
80
|
+
# Parameter: hash - the hash to write
|
81
|
+
def write_yaml filename, hash
|
82
|
+
IO.write filename, YAML.dump(stringified(hash))
|
44
83
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: roject
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anshul Kharbanda
|
@@ -77,8 +77,9 @@ files:
|
|
77
77
|
- LICENSE
|
78
78
|
- README.md
|
79
79
|
- Rakefile
|
80
|
-
- exp/
|
81
|
-
- exp/
|
80
|
+
- exp/loadsaveable/foo.bar
|
81
|
+
- exp/loadsaveable/foo.json
|
82
|
+
- exp/loadsaveable/foo.yaml
|
82
83
|
- lib/loadsaveable.rb
|
83
84
|
- lib/parsers.rb
|
84
85
|
- lib/project.rb
|
data/exp/project.json
DELETED
File without changes
|