general 1.2.8 → 1.3.0

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: 6fc766e79d0ec1c3e71a07ac54fa1193b3b88c43
4
- data.tar.gz: fa31d5069f16b9bcf189fcefd01d170d95cf5866
3
+ metadata.gz: c8069e0ce29ba1633760f2ddcf5ad50e7dbfb007
4
+ data.tar.gz: bd9199ea2c9b5ee9622466672c8282cc8f14e88b
5
5
  SHA512:
6
- metadata.gz: b4075d13c08a615e5c2a8b98e25cefce84f4204cd934e89b1f69950d93b1584521b8b3a44513cbb7db94b3892834940463a53006c6ae8a34536c65a4eb54b9fc
7
- data.tar.gz: 5e5d75fa9e38506bd9422f9a6879dcbdfa369c85857f20707b999c0e7b7cae0e5df0856453687b0369a50e7810d8c6b17f7cf1bd4e06d7010674d99a62810e5d
6
+ metadata.gz: 7519b19cf990fd5ddeff22eaca88233396a1130ad56bf78573965cc64cbe6932046c8a588075c5d9a6695dcd7235dd56b69b71091007480853ef951d02950bb9
7
+ data.tar.gz: c3837725bbe8a263debe367d500f9e60ef0f984edbe3576e01187ec0cd2783a603851f9dee4b6fe10ecdfa4bc32578eeed5768fafa3c3cfbd5ac612f76a62b82
data/lib/general.rb CHANGED
@@ -15,4 +15,5 @@
15
15
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
  require_relative "gtemplate"
18
- require_relative "gfile"
18
+ require_relative "gtimeformat"
19
+ require_relative "gio"
@@ -21,50 +21,40 @@ require_relative "gtemplate"
21
21
  # Author: Anshul Kharbanda
22
22
  # Created: 3 - 4 - 2016
23
23
  module General
24
- # Implements the general file IO
24
+ # Implements the general IO writer template
25
25
  #
26
26
  # Author: Anshul Kharbanda
27
27
  # Created: 3 - 4 - 2016
28
- class GFile
29
- # The general file extention
30
- EXTENTION = ".general"
28
+ class GIO < GTemplate
29
+ # The general file extension
30
+ EXTENSION = ".general"
31
31
 
32
- # Can write to attributes "name" and "path"
32
+ # Loads a GIO from a file with the given path
33
33
  #
34
- # "name" is the name of the target file
34
+ # Parameter: path - the path of the file to load
35
35
  #
36
- # "path" is the path of the target file
37
- attr_writer :name, :path
38
-
39
- # Creates a new GFile with the given path
40
- #
41
- # Paraeter: path the path to the GFile
42
- def initialize path
43
- @name = File.basename path, EXTENTION
44
- @path = File.dirname path
45
- @general = General::GTemplate.new IO.read path
46
- end
47
-
48
- # Returns the path of the target file
49
- #
50
- # Return: the path of the target file
51
- def target
52
- @path + "/" + @name
36
+ # Return: GIO loaded from the file
37
+ def self.load path
38
+ file = File.new path
39
+ gio = self.new file.read
40
+ file.close
41
+ return gio
53
42
  end
54
43
 
55
- # Writes the general with the given
56
- # data applied to the target file
44
+ # Writes the template with the given
45
+ # data applied to the target stream
57
46
  #
47
+ # Parameter: ios - if String, is the name of the file to write to
48
+ # if IO, is the stream to write to
58
49
  # Parameter: data - the data to be applied (merges with defaults)
59
- def write data={}
60
- IO.write target, @general.apply(data)
61
- end
62
-
63
- # Returns the string representation of the GFile
64
- #
65
- # Return: the string representation of the GFile
66
- def to_s
67
- "#{@general} >>> #{target}"
50
+ def write ios, data={}
51
+ if ios.is_a? String
52
+ IO.write ios, apply(data)
53
+ elsif ios.is_a? IO
54
+ ios.write apply(data)
55
+ else
56
+ raise TypeError.new "Expected IO or String, got: #{ios.class}"
57
+ end
68
58
  end
69
59
  end
70
60
  end
data/lib/goperations.rb CHANGED
@@ -68,13 +68,13 @@ module General
68
68
  end
69
69
  end
70
70
 
71
- # Returns the integer time value (in seconds) formatted to H:MM:SS
71
+ # Returns the integer time value (in seconds) formatted to I:MM:SS A
72
72
  #
73
- # Parameter: integer - the integer being formatted (representing the time in second)
73
+ # Parameter: integer - the integer being formatted (representing the time in seconds)
74
74
  #
75
- # Return: the formatted H:MM:SS
76
- def self.hourminsec integer
77
- return format_time "H:MM:SS", integer
75
+ # Return: the formatted I:MM:SS A
76
+ def self.time integer
77
+ return format_time "I:MM:SS A", integer
78
78
  end
79
79
 
80
80
  private
data/lib/gpartials.rb CHANGED
@@ -21,7 +21,7 @@
21
21
  module General
22
22
  private
23
23
 
24
- # Represents a plain string part in a GTemplate
24
+ # Represents a plain string partial in a GTemplate
25
25
  #
26
26
  # Author: Anshul Kharbanda
27
27
  # Created: 7 - 1 - 2016
@@ -48,7 +48,7 @@ module General
48
48
  end
49
49
  end
50
50
 
51
- # Represents a placeholder part in a GTemplate
51
+ # Represents a placeholder partial in a GTemplate
52
52
  #
53
53
  # Author: Anshul Kharbanda
54
54
  # Created: 7 - 1 - 2016
@@ -92,7 +92,7 @@ module General
92
92
  end
93
93
  end
94
94
 
95
- # Represents an array placeholder part in a GTemplate
95
+ # Represents an array placeholder partial in a GTemplate
96
96
  #
97
97
  # Author: Anshul Kharbanda
98
98
  # Created: 7 - 1 - 2016
@@ -125,5 +125,74 @@ module General
125
125
  def apply data
126
126
  return @template.apply_all(data[@name]).join(@delimeter)
127
127
  end
128
+
129
+ # Returns the string representation of the array placeholder
130
+ #
131
+ # Return: the string representation of the array placeholder
132
+ def to_s
133
+ return "@[#{@name}] #{@template.to_s} @[#{@delimeter.inspect}]"
134
+ end
135
+ end
136
+
137
+ # Represents an timeformat placeholder partial in a GTimeFormat
138
+ #
139
+ # Author: Anshul Kharbanda
140
+ # Created: 7 - 1 - 2016
141
+ class GTimeFormatPlaceholder
142
+ # Regular expression that matches timeformat placeholders
143
+ REGEX = /@(?<type>[A-Z]+)/
144
+
145
+ # Read type
146
+ attr :type
147
+
148
+ # Initializes the GTimeFormatPlaceholder with the given match
149
+ #
150
+ # Parameter: match - the match data from the string being parsed
151
+ def initialize match
152
+ @type = match[:type]
153
+ end
154
+
155
+ # Returns the value of the timeformat placeholder in the given time value
156
+ # formatted according to the time format type
157
+ #
158
+ # Parameter: value - the time value being applied
159
+ #
160
+ # Return: the value of the timeformat placeholder in the given time value
161
+ # formatted according to the time format type
162
+ def apply value
163
+ if value.is_a? Integer
164
+ map = type_map(value).to_s
165
+ return is_justify? ? map.rjust(@type.length, '0') : map
166
+ else
167
+ raise TypeError.new "Expected Integer, got: #{value.class}"
168
+ end
169
+ end
170
+
171
+ # Returns true if the timeformat placeholder is a justifiable type
172
+ #
173
+ # Return: true if the timeformat placeholder is a justifiable type
174
+ def is_justify?; "HIMS".include? @type[0]; end
175
+
176
+ # Returns the string representation of the timeformat placeholder
177
+ #
178
+ # Return: the string representation of the timeformat placeholder
179
+ def to_s; return "@#{@type}"; end
180
+
181
+ private
182
+
183
+ # Returns the value modified according to the raw timeformat type
184
+ #
185
+ # Parameter: value - the time value being applied
186
+ #
187
+ # Return: the value modified according to the raw timeformat type
188
+ def type_map value
189
+ case @type[0]
190
+ when "H" then return (value / 3600)
191
+ when "I" then return (value / 3600 % 12 + 1)
192
+ when "M" then return (value % 3600 / 60)
193
+ when "S" then return (value % 3600 % 60)
194
+ when "A" then return (value / 3600 > 11 ? 'PM' : 'AM')
195
+ end
196
+ end
128
197
  end
129
198
  end
data/lib/gtemplate.rb CHANGED
@@ -41,29 +41,23 @@ module General
41
41
  # Returns a string representation of the string
42
42
  #
43
43
  # Return: a string representation of the string
44
- def to_s
45
- return @partials.collect(&:to_s).join
46
- end
44
+ def to_s; @partials.collect(&:to_s).join; end
47
45
 
48
46
  # Applies the given data to the template and returns the generated string
49
47
  #
50
48
  # Parameter: data - the data to be applied (as a hash. merges with defaults)
51
49
  #
52
50
  # Return: string of the template with the given data applied
53
- def apply data={}
54
- return @partials.collect { |partial| partial.apply(data) }.join
55
- end
51
+ def apply(data={}); @partials.collect { |partial| partial.apply(data) }.join; end
56
52
 
57
53
  # Applies each data structure in the array independently to the template
58
54
  # and returns an array of the generated strings
59
55
  #
60
- # Parameter: data_array - the array of data to be applied
56
+ # Parameter: array - the array of data to be applied
61
57
  # (each data hash will be merged with defaults)
62
58
  #
63
59
  # Return: array of strings generated from the template with the given data applied
64
- def apply_all data_array
65
- return data_array.collect { |data| apply(data) }
66
- end
60
+ def apply_all(array); array.collect { |data| apply(data) }; end
67
61
 
68
62
  private
69
63
 
data/spec/gio_spec.rb ADDED
@@ -0,0 +1,64 @@
1
+ require "spec_require"
2
+
3
+ describe General::GIO do
4
+ before :all do
5
+ @gio = General::GIO.load("exp/sample" + General::GIO::EXTENSION)
6
+ @out = "exp/out.txt"
7
+ @phony = 3
8
+ @data = {name: "joe", food: "Joe's Schmoes"}
9
+ @default_text = "There once was a chef name Gordon Ramsay, and he loved eating carrots."
10
+ @applied_text = "There once was a chef name Joe, and he loved eating Joe's Schmoes."
11
+ end
12
+
13
+ describe "#new" do
14
+ it "creates a new GIO with the given template string" do
15
+ expect(@gio).to be_an_instance_of General::GIO
16
+ end
17
+ end
18
+
19
+ describe "#write" do
20
+ context "with target and no data" do
21
+ context 'if target is string' do
22
+ it "writes the default data to the file with the given filename (the target string)" do
23
+ @gio.write @out
24
+ expect(IO.read(@out)).to eql @default_text
25
+ end
26
+ end
27
+
28
+ context 'if target is io' do
29
+ it "writes the default data to the target io" do
30
+ File.open(@out, "w+") { |ios| @gio.write ios }
31
+ expect(IO.read(@out)).to eql @default_text
32
+ end
33
+ end
34
+
35
+ context 'if target is not string or io' do
36
+ it "raises TypeError" do
37
+ expect{ @gio.write(@phony) }.to raise_error TypeError
38
+ end
39
+ end
40
+ end
41
+
42
+ context "with target and given data" do
43
+ context 'if target is string' do
44
+ it "writes the given data to the file with the given filename (the target string)" do
45
+ @gio.write @out, @data
46
+ expect(IO.read(@out)).to eql @applied_text
47
+ end
48
+ end
49
+
50
+ context 'if target is io' do
51
+ it "writes the given data to the target io" do
52
+ File.open(@out, "w+") { |ios| @gio.write ios, @data }
53
+ expect(IO.read(@out)).to eql @applied_text
54
+ end
55
+ end
56
+
57
+ context 'if target is not string or io' do
58
+ it "raises TypeError" do
59
+ expect{ @gio.write(@phony, @data) }.to raise_error TypeError
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -21,6 +21,7 @@ describe General::GTemplate do
21
21
  ]}
22
22
  @applied_text2 = "Hello, Ben! How is the dog?\nHello, Jen! How is the cat?\nHello, Ken! How is the plant?"
23
23
 
24
+ # General array template and placeholders test
24
25
  @template3 = General::GTemplate.new "@(film: The Dark Knight)\nCrew:\n@[crew] \t@(name): @(role) @[\n]\nScore: @(score)/10"
25
26
  @data3 = {
26
27
  film: 'Batman Begins',
@@ -52,44 +53,39 @@ describe General::GTemplate do
52
53
  end
53
54
 
54
55
  describe "#apply" do
55
- context "With no data" do
56
- it "Returns the template with the default data applied" do
56
+ context "with no data" do
57
+ it "returns the template with the default data applied" do
57
58
  expect(@template1.apply).to eql @default_text
58
59
  end
59
60
  end
60
61
 
61
- context "With only name given" do
62
- it "Returns the template with the given name and default food applied" do
62
+ context "with partial data" do
63
+ it "returns the template with the given data applied to corresponding placeholders and default data applied to the rest" do
63
64
  expect(@template1.apply(name: @name)).to eql @name_applied_text
64
- end
65
- end
66
-
67
- context "With only food given" do
68
- it "Returns the template with the default name and given food applied" do
69
65
  expect(@template1.apply(food: @food)).to eql @food_applied_text
70
66
  end
71
67
  end
72
68
 
73
- context "With all data" do
74
- it "Returns the template with the given data applied" do
69
+ context "with all data" do
70
+ it "returns the template with the given data applied" do
75
71
  expect(@template1.apply(@data1)).to eql @all_applied_text
76
72
  end
77
73
  end
78
74
 
79
- context "With array template" do
80
- it "Returns the template with the given array data applied and formatted according to the template" do
75
+ context "with array template" do
76
+ it "returns the template with the given array data applied and formatted according to the template" do
81
77
  expect(@template2.apply(@data2)).to eql @applied_text2
82
78
  end
83
79
  end
84
80
 
85
- context "With array template and regular placeholder" do
86
- it "Returns the template with the given data applied (including array data) and formatted according to the template" do
81
+ context "with array template and regular placeholder" do
82
+ it "returns the template with the given data applied (including array data) and formatted according to the template" do
87
83
  expect(@template3.apply(@data3)).to eql @applied_text3
88
84
  end
89
85
  end
90
86
 
91
- context "With placeholder operation" do
92
- it "Returns the template with the given array data applied and formatted according to the format operations" do
87
+ context "with placeholder operation" do
88
+ it "returns the template with the given array data applied and formatted according to the format operations" do
93
89
  expect(@template4.apply(@data4)).to eql @applied_text4
94
90
  end
95
91
  end
data/spec/spec_require.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require_relative "../lib/gtemplate"
2
- require_relative "../lib/gfile"
2
+ require_relative "../lib/gio"
3
+ require_relative "../lib/gtimeformat"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: general
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.8
4
+ version: 1.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-02 00:00:00.000000000 Z
11
+ date: 2016-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -34,11 +34,11 @@ extensions: []
34
34
  extra_rdoc_files: []
35
35
  files:
36
36
  - lib/general.rb
37
- - lib/gfile.rb
37
+ - lib/gio.rb
38
38
  - lib/goperations.rb
39
39
  - lib/gpartials.rb
40
40
  - lib/gtemplate.rb
41
- - spec/gfile_spec.rb
41
+ - spec/gio_spec.rb
42
42
  - spec/gtemplate_spec.rb
43
43
  - spec/spec_require.rb
44
44
  homepage: https://andydevs.github.io/general
data/spec/gfile_spec.rb DELETED
@@ -1,59 +0,0 @@
1
- require "spec_require"
2
-
3
- describe General::GFile do
4
- before :all do
5
- @filepath = "exp"
6
- @filename = "sample.txt"
7
- @new_filename = "supersample.txt"
8
- @new_filepath = "exp/superexp"
9
- @data = {name: "joe", food: "Joe's Schmoes"}
10
- @default_text = "There once was a chef name Gordon Ramsay, and he loved eating carrots."
11
- @applied_text = "There once was a chef name Joe, and he loved eating Joe's Schmoes."
12
- end
13
-
14
- before :each do
15
- @file = General::GFile.new (@filepath + "/" + @filename + General::GFile::EXTENTION)
16
- end
17
-
18
- describe "#new" do
19
- it "Creates a new GFile with the given filename" do
20
- expect(@file).to be_an_instance_of General::GFile
21
- end
22
- end
23
-
24
- describe "#target" do
25
- it "Returns the name of the target" do
26
- expect(@file.target).to eql (@filepath + "/" + @filename)
27
- end
28
- end
29
-
30
- describe "#name=" do
31
- it "Changes the name of the target" do
32
- @file.name = @new_filename
33
- expect(@file.target).to eql (@filepath + "/" + @new_filename)
34
- end
35
- end
36
-
37
- describe "#path=" do
38
- it "Changes the path of the target" do
39
- @file.path = @new_filepath
40
- expect(@file.target).to eql (@new_filepath + "/" + @filename)
41
- end
42
- end
43
-
44
- describe "#write" do
45
- context "With no data" do
46
- it "Writes the default data to the target file" do
47
- @file.write
48
- expect(IO.read(@file.target)).to eql @default_text
49
- end
50
- end
51
-
52
- context "With given data" do
53
- it "Writes the given data to the target file" do
54
- @file.write(@data)
55
- expect(IO.read(@file.target)).to eql @applied_text
56
- end
57
- end
58
- end
59
- end