roject 0.2.1 → 0.3.0
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.
- checksums.yaml +4 -4
- data/Rakefile +8 -46
- data/exp/loadsaveable/foo.yml +2 -0
- data/exp/project/project.yaml +0 -0
- data/lib/loadsaveable.rb +4 -5
- data/lib/parsers.rb +36 -24
- data/lib/project.rb +0 -3
- data/lib/roject.rb +1 -1
- data/spec/project_spec.rb +16 -1
- data/spec/shared/loadsaveable_spec.rb +58 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b97c2ef08f7a1a34af079cd290aa1add5150c73d
|
4
|
+
data.tar.gz: 30d00e93047cb2d05b1b85509fb9d7f56db47864
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7706843324ed9f3939834ad416e2626ac74192d491e61b756d27f428059e8d28c9588b52d95e04d2f62a6ed4691b2365124ca9032fb331c0c611e466f0c2ac14
|
7
|
+
data.tar.gz: 8b6fd585b035bc4383467250c3f7040bd7dc027b26342f9283db032e8b4e25b2309552a33da284b48a8e11696fb1c2d4808f3c50c744668b4aa4eebbdf5e3684
|
data/Rakefile
CHANGED
@@ -13,6 +13,7 @@ Created: 7 - 8 - 2016
|
|
13
13
|
|
14
14
|
# Required libraries
|
15
15
|
require "rspec/core/rake_task"
|
16
|
+
require "rubygems/tasks"
|
16
17
|
|
17
18
|
# Requird files
|
18
19
|
require_relative "lib/roject"
|
@@ -21,55 +22,19 @@ require_relative "lib/roject"
|
|
21
22
|
GEMDIR = "gem"
|
22
23
|
SPECDIR = "spec"
|
23
24
|
|
25
|
+
# Default task
|
26
|
+
task :default => :spec
|
27
|
+
|
24
28
|
#----------------------------------RSPEC----------------------------------
|
25
29
|
|
26
|
-
RSpec::Core::RakeTask.new
|
30
|
+
RSpec::Core::RakeTask.new do |task|
|
27
31
|
task.pattern = "#{SPECDIR}/*_spec.rb"
|
28
|
-
task.rspec_opts = "--format documentation"
|
29
|
-
"--color"
|
32
|
+
task.rspec_opts = "--format documentation --color"
|
30
33
|
end
|
31
34
|
|
32
35
|
#-----------------------------------GEM-----------------------------------
|
33
36
|
|
34
|
-
|
35
|
-
|
36
|
-
#----------------------------------GEM CLEAR TASKS----------------------------------
|
37
|
-
|
38
|
-
namespace :clear do
|
39
|
-
desc "Clear all gems"
|
40
|
-
task :all do
|
41
|
-
sh "rm #{GEMDIR}/*.gem"
|
42
|
-
end
|
43
|
-
|
44
|
-
desc "Clear old gems"
|
45
|
-
task :old do
|
46
|
-
sh "rm #{FileList.new("#{GEMDIR}/*.gem")
|
47
|
-
.exclude("#{GEMDIR}/roject-#{Roject::VERSION}.gem")}"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
#----------------------------------GEM BUILD TASKS----------------------------------
|
52
|
-
|
53
|
-
desc "Build gem and push to Rubygems"
|
54
|
-
task :push => "#{GEMDIR}/roject-#{Roject::VERSION}.gem" do |task|
|
55
|
-
sh "gem push #{task.source}"
|
56
|
-
end
|
57
|
-
|
58
|
-
desc "Build gem and install locally"
|
59
|
-
task :install => "#{GEMDIR}/roject-#{Roject::VERSION}.gem" do |task|
|
60
|
-
sh "gem install #{task.source}"
|
61
|
-
end
|
62
|
-
|
63
|
-
desc "Just buid gem"
|
64
|
-
task :build => "#{GEMDIR}/roject-#{Roject::VERSION}.gem"
|
65
|
-
|
66
|
-
#-------------------------------BUILD GEM FROM GEMSPEC------------------------------
|
67
|
-
|
68
|
-
file "#{GEMDIR}/roject-#{Roject::VERSION}.gem" => "roject.gemspec" do |task|
|
69
|
-
sh "gem build #{task.source}"
|
70
|
-
sh "mv #{File.basename(task.name)} #{File.dirname(task.name)}"
|
71
|
-
end
|
72
|
-
end
|
37
|
+
Gem::Tasks.new
|
73
38
|
|
74
39
|
#-----------------------------------GIT-----------------------------------
|
75
40
|
|
@@ -94,7 +59,4 @@ namespace :git do
|
|
94
59
|
task :reset_hard do
|
95
60
|
sh "git reset --hard HEAD"
|
96
61
|
end
|
97
|
-
end
|
98
|
-
|
99
|
-
# Default spec
|
100
|
-
task :default => "spec"
|
62
|
+
end
|
File without changes
|
data/lib/loadsaveable.rb
CHANGED
@@ -22,12 +22,11 @@ module Roject
|
|
22
22
|
# Implementing objects can be loaded from and saved to files in
|
23
23
|
# any of the supported data storage formats.
|
24
24
|
#
|
25
|
-
# Formats: json
|
25
|
+
# Formats: json, yaml
|
26
26
|
#
|
27
|
-
# Note: Implementing objects
|
28
|
-
# which returns a hash of the
|
29
|
-
#
|
30
|
-
# given hash (for unit testing)
|
27
|
+
# Note: Implementing objects must have a constructor with takes a single hash
|
28
|
+
# argument and respond to the :hash method which returns a hash of the
|
29
|
+
# data that needs to be saved
|
31
30
|
#
|
32
31
|
# Author: Anshul Kharbanda
|
33
32
|
# Created: 7 - 10 - 2016
|
data/lib/parsers.rb
CHANGED
@@ -11,6 +11,7 @@ Created: 7 - 8 - 2016
|
|
11
11
|
|
12
12
|
=end
|
13
13
|
|
14
|
+
# Parser libraries
|
14
15
|
require "json"
|
15
16
|
require "yaml"
|
16
17
|
|
@@ -26,9 +27,10 @@ module Roject
|
|
26
27
|
module Parsers
|
27
28
|
# Generic class parses files of a given type
|
28
29
|
#
|
29
|
-
# Subclasses must respond to :parse and :
|
30
|
+
# Subclasses must respond to :parse, :format, and :extension methods
|
30
31
|
# - parse recieves a string and returns a hash
|
31
|
-
# -
|
32
|
+
# - format recieves a hash and returns a string
|
33
|
+
# - extensions returns all of the supported extensions of the Parser
|
32
34
|
#
|
33
35
|
# Author: Anshul Kharbanda
|
34
36
|
# Created: 7 - 11 - 2016
|
@@ -39,15 +41,32 @@ module Roject
|
|
39
41
|
# Returns the descendants of Parser
|
40
42
|
#
|
41
43
|
# Return: the descendants of Parser
|
42
|
-
def self.
|
44
|
+
def self.descendants
|
43
45
|
ObjectSpace.each_object(Class).select {|c| c < self}
|
44
46
|
end
|
45
47
|
|
46
|
-
# Returns the
|
48
|
+
# Returns true if the parser is to be used for the given filename
|
47
49
|
#
|
48
|
-
# Return: the
|
49
|
-
def self.
|
50
|
-
|
50
|
+
# Return: true if the parser is to be used for the given filename
|
51
|
+
def self.for_filename? filename
|
52
|
+
extensions.include? File.extname(filename)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the parser for the given filename
|
57
|
+
#
|
58
|
+
# Parameter: filename - the name of the file to parse
|
59
|
+
#
|
60
|
+
# Return: the parser for the given filename
|
61
|
+
def self.get(filename)
|
62
|
+
# Get parser from filename
|
63
|
+
parser = Parser.descendants.find { |klass| klass.for_filename? filename }
|
64
|
+
|
65
|
+
# Raise LoadError if no parser is found, else return parser
|
66
|
+
if parser.nil?
|
67
|
+
raise LoadError, "#{File.extname(filename)} not supported!"
|
68
|
+
else
|
69
|
+
return parser
|
51
70
|
end
|
52
71
|
end
|
53
72
|
|
@@ -58,6 +77,11 @@ module Roject
|
|
58
77
|
# Author: Anshul Kharbanda
|
59
78
|
# Created: 7 - 11 - 2016
|
60
79
|
class JSONParser < Parser
|
80
|
+
# Returns the extensions supported for JSON
|
81
|
+
#
|
82
|
+
# Return: the extensions supported for JSON
|
83
|
+
def self.extensions; [".json"]; end
|
84
|
+
|
61
85
|
# Parses the given json text into a hash
|
62
86
|
#
|
63
87
|
# Parameter: text - the json text to parse
|
@@ -78,6 +102,11 @@ module Roject
|
|
78
102
|
# Author: Anshul Kharbanda
|
79
103
|
# Created: 7 - 13 - 2016
|
80
104
|
class YAMLParser < Parser
|
105
|
+
# Returns the extensions supported for YAML
|
106
|
+
#
|
107
|
+
# Return: the extensions supported for YAML
|
108
|
+
def self.extensions; [".yaml", ".yml"]; end
|
109
|
+
|
81
110
|
# Parses the given yaml text into a hash
|
82
111
|
#
|
83
112
|
# Parameter: text - the yaml text to parse
|
@@ -112,22 +141,5 @@ module Roject
|
|
112
141
|
hash.each_pair.collect { |k, v| [k.to_s, v.is_a?(Hash) ? stringified(v) : v] }.to_h
|
113
142
|
end
|
114
143
|
end
|
115
|
-
|
116
|
-
#-----------------------------------------GET------------------------------------------
|
117
|
-
|
118
|
-
# Returns the parser for the given filename
|
119
|
-
#
|
120
|
-
# Parameter: filename - the name of the file to parse
|
121
|
-
#
|
122
|
-
# Return: the parser for the given filename
|
123
|
-
def self.get(filename)
|
124
|
-
case File.extname(filename)
|
125
|
-
when ".json" then return JSONParser
|
126
|
-
when ".yaml" then return YAMLParser
|
127
|
-
|
128
|
-
# Raise error if extension is not supported
|
129
|
-
else raise LoadError, "#{File.extname(filename)} not supported!"
|
130
|
-
end
|
131
|
-
end
|
132
144
|
end
|
133
145
|
end
|
data/lib/project.rb
CHANGED
@@ -34,9 +34,6 @@ module Roject
|
|
34
34
|
@project = hash
|
35
35
|
end
|
36
36
|
|
37
|
-
# This is a test method and is depricated by default. Do not use!
|
38
|
-
def config(hash); @project = hash; end
|
39
|
-
|
40
37
|
# Returns a hash of the data contained in the project
|
41
38
|
#
|
42
39
|
# Return: a hash of the data contained in the project
|
data/lib/roject.rb
CHANGED
data/spec/project_spec.rb
CHANGED
@@ -21,5 +21,20 @@ require_relative "spec_require"
|
|
21
21
|
# Author: Anshul Kharbanda
|
22
22
|
# Created: 7 - 8 - 2016
|
23
23
|
describe Roject::Project do
|
24
|
-
|
24
|
+
# Describing LoadSaveable include
|
25
|
+
#
|
26
|
+
# Implementing objects can be loaded from and saved to files in
|
27
|
+
# any of the supported data storage formats.
|
28
|
+
#
|
29
|
+
# Formats: json, yaml
|
30
|
+
#
|
31
|
+
# Note: Implementing objects must have a constructor with takes a single hash
|
32
|
+
# argument and respond to the :hash method which returns a hash of the
|
33
|
+
# data that needs to be saved
|
34
|
+
#
|
35
|
+
# Author: Anshul Kharbanda
|
36
|
+
# Created: 7 - 10 - 2016
|
37
|
+
describe 'include LoadSaveable' do
|
38
|
+
include_examples "loadsaveable", Roject::Project
|
39
|
+
end
|
25
40
|
end
|
@@ -32,6 +32,7 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
32
32
|
@modded_hash = { name: "superproject", author: "Anshul Kharbanda" }
|
33
33
|
@pjson_name = "#{@dir}/foo.json"
|
34
34
|
@pyaml_name = "#{@dir}/foo.yaml"
|
35
|
+
@pyml_name = "#{@dir}/foo.yml"
|
35
36
|
@phony_name = "#{@dir}/foo.bar"
|
36
37
|
end
|
37
38
|
|
@@ -89,6 +90,14 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
89
90
|
end
|
90
91
|
end
|
91
92
|
|
93
|
+
context "with a .yml filename given" do
|
94
|
+
it "returns a new #{loadsaveable_class.name} from the yml file with the given filename" do
|
95
|
+
project = loadsaveable_class.load(@pyml_name)
|
96
|
+
expect(project).to be_an_instance_of loadsaveable_class
|
97
|
+
expect(project.hash).to eql read_yaml(@pyml_name)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
92
101
|
#-----------------------------UNSUPPORTED-----------------------------
|
93
102
|
|
94
103
|
context "with an unsupported file extension given" do
|
@@ -129,6 +138,13 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
129
138
|
end
|
130
139
|
end
|
131
140
|
|
141
|
+
context "with a .yml filename given" do
|
142
|
+
it "saves the project to the given filename in yml format" do
|
143
|
+
@project.save @pyml_name
|
144
|
+
expect(read_yaml(@pyml_name)).to eql @modded_hash
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
132
148
|
#-----------------------------UNSUPPORTED-----------------------------
|
133
149
|
|
134
150
|
context "with an unsupported file extension given" do
|
@@ -142,6 +158,7 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
142
158
|
after :all do
|
143
159
|
write_json @pjson_name, @default_hash
|
144
160
|
write_yaml @pyaml_name, @default_hash
|
161
|
+
write_yaml @pyml_name, @default_hash
|
145
162
|
end
|
146
163
|
end
|
147
164
|
|
@@ -174,7 +191,7 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
174
191
|
project = self
|
175
192
|
|
176
193
|
# Modify project
|
177
|
-
|
194
|
+
initialize mhash
|
178
195
|
end }.not_to raise_error
|
179
196
|
end
|
180
197
|
|
@@ -204,7 +221,7 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
204
221
|
project = self
|
205
222
|
|
206
223
|
# Modify project
|
207
|
-
|
224
|
+
initialize mhash
|
208
225
|
end }.not_to raise_error
|
209
226
|
end
|
210
227
|
|
@@ -220,6 +237,36 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
220
237
|
end
|
221
238
|
end
|
222
239
|
|
240
|
+
context "when file is .yml" do
|
241
|
+
# Declare project local variable
|
242
|
+
project = nil
|
243
|
+
|
244
|
+
it "loads a #{loadsaveable_class.name} from the yml file with the given filename (calling #load)" do
|
245
|
+
# Declare modded_hash local variable
|
246
|
+
mhash = @modded_hash
|
247
|
+
|
248
|
+
# Open project in a block
|
249
|
+
expect { loadsaveable_class.open @pyml_name do
|
250
|
+
# Set project to instance
|
251
|
+
project = self
|
252
|
+
|
253
|
+
# Modify project
|
254
|
+
initialize mhash
|
255
|
+
end }.not_to raise_error
|
256
|
+
end
|
257
|
+
|
258
|
+
it "evaluates the given block in the context of the loaded #{loadsaveable_class.name}" do
|
259
|
+
# Check if project was actually self, and it was successfuly modified
|
260
|
+
expect(project).to be_an_instance_of loadsaveable_class
|
261
|
+
expect(project.hash).to eql @modded_hash
|
262
|
+
end
|
263
|
+
|
264
|
+
it "saves the #{loadsaveable_class.name} when the block ends" do
|
265
|
+
# Check if the modded project was saved
|
266
|
+
expect(read_yaml(@pyml_name)).to eql @modded_hash
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
223
270
|
#-----------------------------UNSUPPORTED-----------------------------
|
224
271
|
|
225
272
|
context "when filetype is unsupported" do
|
@@ -231,6 +278,7 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
231
278
|
after :all do
|
232
279
|
write_json @pjson_name, @default_hash
|
233
280
|
write_yaml @pyaml_name, @default_hash
|
281
|
+
write_yaml @pyml_name, @default_hash
|
234
282
|
end
|
235
283
|
end
|
236
284
|
|
@@ -253,6 +301,14 @@ shared_examples "loadsaveable" do |loadsaveable_class|
|
|
253
301
|
end
|
254
302
|
end
|
255
303
|
|
304
|
+
context "when file is .yml" do
|
305
|
+
it "loads a #{loadsaveable_class.name} from the yml file with the given filename (calling #load) and returns it." do
|
306
|
+
project = loadsaveable_class.open @pyml_name
|
307
|
+
expect(project).to be_an_instance_of loadsaveable_class
|
308
|
+
expect(project.hash).to eql @default_hash
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
256
312
|
#-----------------------------UNSUPPORTED-----------------------------
|
257
313
|
|
258
314
|
context "when filetype is unsupported" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: roject
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anshul Kharbanda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: general
|
@@ -80,6 +80,8 @@ files:
|
|
80
80
|
- exp/loadsaveable/foo.bar
|
81
81
|
- exp/loadsaveable/foo.json
|
82
82
|
- exp/loadsaveable/foo.yaml
|
83
|
+
- exp/loadsaveable/foo.yml
|
84
|
+
- exp/project/project.yaml
|
83
85
|
- lib/loadsaveable.rb
|
84
86
|
- lib/parsers.rb
|
85
87
|
- lib/project.rb
|