semi 0.3.1 → 0.3.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d44cc9f0c53b0135a006f573fe9381d69d62528
4
- data.tar.gz: 1a820829b1a3fd4be635ba52db765bfa381d11e7
3
+ metadata.gz: 5ddcfc2b2c39716b3545162f16871750194bd3b4
4
+ data.tar.gz: 9256ddba7ee653ec38f3d7b5f0be59b9353efa42
5
5
  SHA512:
6
- metadata.gz: 4e15d06945634ed354e841971024f72951308b9076773447b1d812d5aa1bfe8d708c9ab22f33ec844d30531fd406d982b3261a3fe9e33e5b612049e487a186d0
7
- data.tar.gz: a1f596242c41b2b22a9ae182cf7c113f4ee912085c11b5d6ca2eccfad6e5fdb78e299e01860ff24fbbf23993a6a3526a9ee55fbc98fff197b2001ba6cc7fbcf4
6
+ metadata.gz: 3e59adc27e9693d872d224173daba47be734a609ae5a6679d3b0dc205fd7481cdc7bd69df93e56d80a88fae573ccdadda76f96b4359c3edfe70c017f210d293f
7
+ data.tar.gz: f6c8f58b7371a8a928740664036309e74a653929c61e99a6df247a3d354555147ad4bb24cf9788c62bffbeec5ce73194a10e9253aa745b00e7143b25bbfff79a
@@ -12,9 +12,12 @@ module Semi
12
12
  end
13
13
 
14
14
  def to_s
15
- @value
15
+ @value.to_s
16
16
  end
17
17
 
18
+ def value
19
+ @value
20
+ end
18
21
  end
19
22
  end
20
23
  end
@@ -5,10 +5,15 @@ module Semi::Variables
5
5
 
6
6
 
7
7
  def set(val)
8
- # test to see if the value is a common true value
9
- if val =~ /true|yes|enable/i
8
+ if val.instance_of? TrueClass
9
+ @value = true
10
+ elsif val.instance_of? FalseClass
11
+ @value = false
12
+ elsif val =~ /true|yes|on|enable/i
13
+ # test to see if the value is a common true value
10
14
  @value = true
11
- elsif val =~ /false|no|disable/i
15
+ elsif val =~ /false|no|off|disable/i
16
+ # test to see if the value is a common false value
12
17
  @value = false
13
18
  else
14
19
  raise Semi::VariableError, "#{val} trying to be set as a boolean"
@@ -18,7 +23,7 @@ module Semi::Variables
18
23
  def validate
19
24
  self.validate(@value)
20
25
  end
21
-
26
+
22
27
  def self.validate(value)
23
28
  real_value = nil
24
29
 
@@ -34,6 +39,74 @@ module Semi::Variables
34
39
  end
35
40
  false
36
41
  end
37
-
42
+
43
+ def onoff
44
+ if @value
45
+ 'on'
46
+ else
47
+ 'off'
48
+ end
49
+ end
50
+
51
+ def ONOFF
52
+ onoff.upcase
53
+ end
54
+
55
+ def OnOff
56
+ onoff.capitalize
57
+ end
58
+
59
+
60
+ def yesno()
61
+ if @value
62
+ 'yes'
63
+ else
64
+ 'no'
65
+ end
66
+ end
67
+
68
+ def YESNO
69
+ yesno.upcase
70
+ end
71
+
72
+ def YesNo
73
+ yesno.capitalize
74
+ end
75
+
76
+
77
+ def enabledisable
78
+ if @value
79
+ 'enable'
80
+ else
81
+ 'disable'
82
+ end
83
+ end
84
+
85
+ def ENABLEDISABLE
86
+ enabledisable.upcase
87
+ end
88
+
89
+ def EnableDisable
90
+ enabledisable.capitalize
91
+ end
92
+
93
+
94
+ def truefalse
95
+ if @value
96
+ 'true'
97
+ else
98
+ 'false'
99
+ end
100
+ end
101
+
102
+ def TRUEFALSE
103
+ truefalse.upcase
104
+ end
105
+
106
+ def TrueFalse
107
+ truefalse.capitalize
108
+ end
38
109
  end
110
+
111
+
39
112
  end
@@ -3,7 +3,15 @@ require_relative 'base'
3
3
  module Semi::Variables
4
4
  class Path < Semi::Variables::Base
5
5
 
6
- @@path_re = Regexp.new('^(?<path>(?:\.{1,2}|\/).*?)\/(?<file>[^\/]+)?$')
6
+ @@path_re = Regexp.new('^(?<path>(?:\/|\.{1,2}\/|~(?:[a-z_][a-z0-9_]{0,30})?\/?|[^~][^\/\000]+\/)*?)(?<file>[^\/\000]+)?$')
7
+
8
+ def initialize(val)
9
+ if @@path_re.match(val)
10
+ @value = val
11
+ else
12
+ raise Semi::VariableError, '#{val} does not look like a path'
13
+ end
14
+ end
7
15
 
8
16
  def validate
9
17
  self.validate(@value)
@@ -18,5 +26,19 @@ module Semi::Variables
18
26
  false
19
27
  end
20
28
 
29
+ def path
30
+ match = @@path_re.match(@value)
31
+ if match
32
+ match['path']
33
+ end
34
+ end
35
+
36
+ def file
37
+ match = @@path_re.match(@value)
38
+ if match
39
+ match['file']
40
+ end
41
+ end
42
+
21
43
  end
22
44
  end
@@ -3,20 +3,70 @@ require_relative 'base'
3
3
  module Semi::Variables
4
4
  class Url < Semi::Variables::Base
5
5
 
6
- @@url_re = Regexp.new('^(?<proto>https?|ftp|file):\/\/(?<host>[a-z\.0-9\-_]+)?(?::(?<port>\d{1,5}))?\/?(?<path>.*?)\/?(?<file>[^\/\?]+)?(?:\?(?<params>.*?))?$', Regexp::IGNORECASE)
6
+ @@url_re = Regexp.new('^(?<proto>https?|ftp):\/{2}(?!\/)(?<host>[a-z\.0-9\-_]+)?(?::(?<port>\d{1,5}))?\/?(?<path>.*?)\/?(?<file>[^\/\?]+)?(?:\?(?<params>.*?))?$', Regexp::IGNORECASE)
7
+
8
+ def initialize(val)
9
+ if @@url_re.match(val)
10
+ @value = val
11
+ else
12
+ raise Semi::VariableError, '#{val} does not look like a URL'
13
+ end
14
+ end
7
15
 
8
16
  def validate
9
17
  self.validate(@value)
10
18
  end
11
-
12
- def self.validate(value)
13
- if ['String', 'Semi::Variables::Url'].include? value.class.to_s
14
- if @@url_re.match(value)
19
+
20
+ def self.validate(val)
21
+ if ['String', 'Semi::Variables::Url'].include? val.class.to_s
22
+ if @@url_re.match(val)
15
23
  return true
16
24
  end
17
25
  end
18
26
  false
19
27
  end
28
+
29
+
30
+ def proto
31
+ match = @@url_re.match(@value)
32
+ if match
33
+ match['proto']
34
+ end
35
+ end
20
36
 
37
+ def host
38
+ match = @@url_re.match(@value)
39
+ if match
40
+ match['host']
41
+ end
42
+ end
43
+
44
+ def port
45
+ match = @@url_re.match(@value)
46
+ if match
47
+ match['port']
48
+ end
49
+ end
50
+
51
+ def path
52
+ match = @@url_re.match(@value)
53
+ if match
54
+ match['path']
55
+ end
56
+ end
57
+
58
+ def file
59
+ match = @@url_re.match(@value)
60
+ if match
61
+ match['file']
62
+ end
63
+ end
64
+
65
+ def params
66
+ match = @@url_re.match(@value)
67
+ if match
68
+ match['params']
69
+ end
70
+ end
21
71
  end
22
72
  end
data/lib/semi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Semi
2
- version = '0.3.1'
2
+ version = '0.3.2'
3
3
  end
@@ -19,7 +19,10 @@ describe "Semi::validator" do
19
19
  [nil, ['required'], false],
20
20
  ['/etc/passwd', 'path', true],
21
21
  ['../parent', ['path'], true],
22
- ['.005', 'path', false],
22
+ ['.005', 'path', true],
23
+ ['file.name', 'path', true],
24
+ ['~user', 'path', true],
25
+ ['~user/dir/file', 'path', true],
23
26
  ['http://www.simple.com',
24
27
  'url', true],
25
28
  ['http://abit.more.complex.com/',
@@ -0,0 +1,145 @@
1
+ require 'spec_helper'
2
+
3
+ describe Semi::Variables::Boolean do
4
+
5
+ [
6
+ # test value, real value
7
+ [true, true],
8
+ [false, false],
9
+ ['true', true],
10
+ ['yes', true],
11
+ ['on', true],
12
+ ['enable', true],
13
+ ['false', false],
14
+ ['no', false],
15
+ ['off', false],
16
+ ['disable', false]
17
+ ].each do |test|
18
+ it "set '#{test[0]}' as #{test[1]}" do
19
+ expect(Semi::Variables::Boolean.new(test[0]).value).to eq test[1]
20
+ end
21
+ end
22
+
23
+ [
24
+ # test value, exception?
25
+ ['true', 'not raise'],
26
+ ['false', 'not raise'],
27
+ ['foo', 'raise'],
28
+ ['bar', 'raise'],
29
+ ['1', 'raise'],
30
+ ['0', 'raise'],
31
+ [1, 'raise'],
32
+ [0, 'raise'],
33
+ [nil, 'raise']
34
+ ].each do |test|
35
+ it "set '#{test[0]}' to #{test[1]} exception" do
36
+ if test[1] == 'raise'
37
+ expect{Semi::Variables::Boolean.new(test[0])}.to raise_error(Semi::VariableError)
38
+ else
39
+ expect{Semi::Variables::Boolean.new(test[0])}.not_to raise_error
40
+ end
41
+ end
42
+ end
43
+
44
+ ##
45
+ # Test variable specific output methods
46
+ let (:on) {Semi::Variables::Boolean.new('true')}
47
+ let (:off) {Semi::Variables::Boolean.new('false')}
48
+
49
+ it "#onoff returns 'on'" do
50
+ expect(on.onoff).to eq 'on'
51
+ end
52
+
53
+ it "#ONOFF returns 'ON'" do
54
+ expect(on.ONOFF).to eq 'ON'
55
+ end
56
+
57
+ it "#OnOff returns 'On'" do
58
+ expect(on.OnOff).to eq 'On'
59
+ end
60
+
61
+ it "#onoff returns 'off'" do
62
+ expect(off.onoff).to eq 'off'
63
+ end
64
+
65
+ it "#ONOFF returns 'OFF'" do
66
+ expect(off.ONOFF).to eq 'OFF'
67
+ end
68
+
69
+ it "#OnOff returns 'Off'" do
70
+ expect(off.OnOff).to eq 'Off'
71
+ end
72
+
73
+ it "#yesno returns 'yes'" do
74
+ expect(on.yesno).to eq 'yes'
75
+ end
76
+
77
+ it "#YESNO returns 'YES'" do
78
+ expect(on.YESNO).to eq 'YES'
79
+ end
80
+
81
+ it "#YesNo returns 'Yes'" do
82
+ expect(on.YesNo).to eq 'Yes'
83
+ end
84
+
85
+ it "#yesno returns 'no'" do
86
+ expect(off.yesno).to eq 'no'
87
+ end
88
+
89
+ it "#YESNO returns 'NO'" do
90
+ expect(off.YESNO).to eq 'NO'
91
+ end
92
+
93
+ it "#YesNo returns 'No'" do
94
+ expect(off.YesNo).to eq 'No'
95
+ end
96
+
97
+ it "#enabledisable returns 'enable'" do
98
+ expect(on.enabledisable).to eq 'enable'
99
+ end
100
+
101
+ it "#ENABLEDISABLE returns 'ENABLE'" do
102
+ expect(on.ENABLEDISABLE).to eq 'ENABLE'
103
+ end
104
+
105
+ it "#EnableDisable returns 'Enable'" do
106
+ expect(on.EnableDisable).to eq 'Enable'
107
+ end
108
+
109
+ it "#enabledisable returns 'disable'" do
110
+ expect(off.enabledisable).to eq 'disable'
111
+ end
112
+
113
+ it "#ENABLEDISABLE returns 'DISABLE'" do
114
+ expect(off.ENABLEDISABLE).to eq 'DISABLE'
115
+ end
116
+
117
+ it "#EnableDisable returns 'Disable'" do
118
+ expect(off.EnableDisable).to eq 'Disable'
119
+ end
120
+
121
+ it "#truefalse returns 'true'" do
122
+ expect(on.truefalse).to eq 'true'
123
+ end
124
+
125
+ it "#TRUEFALSE returns 'TRUE'" do
126
+ expect(on.TRUEFALSE).to eq 'TRUE'
127
+ end
128
+
129
+ it "#TrueFalse returns 'True'" do
130
+ expect(on.TrueFalse).to eq 'True'
131
+ end
132
+
133
+ it "#truefalse returns 'false'" do
134
+ expect(off.truefalse).to eq 'false'
135
+ end
136
+
137
+ it "#TRUEFALSE returns 'FALSE'" do
138
+ expect(off.TRUEFALSE).to eq 'FALSE'
139
+ end
140
+
141
+ it "#TrueFalse returns 'False'" do
142
+ expect(off.TrueFalse).to eq 'False'
143
+ end
144
+
145
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe Semi::Variables::Path do
4
+
5
+ [
6
+ # Test string,
7
+ '/',
8
+ '/bin',
9
+ '/usr/local/bin/csh',
10
+ '~user',
11
+ '~/.bashrc',
12
+ '~looser/plan.txt',
13
+ './test',
14
+ '../../usr/bin/foobar',
15
+ '.././.././test-file',
16
+ '/tmp/backup~',
17
+ '/home/user/.git/config',
18
+ 'sub/directory/file.txt',
19
+ ].each do |test|
20
+ it "stores '#{test}' correctly" do
21
+ expect(Semi::Variables::Path.new(test).value).to eq test
22
+ end
23
+ end
24
+
25
+ # Currently the regex used to identify paths allows almost
26
+ # any string.
27
+ #[
28
+ # 'foo.txt',
29
+ #].each do |test|
30
+ # it "rejects '#{test}'" do
31
+ # expect{Semi::Variables::Path.new(test)}.to raise_error(Semi::VariableError)
32
+ # end
33
+ #end
34
+
35
+ context "with relative paths" do
36
+ let (:path) {Semi::Variables::Path.new('../some/dir/file.txt')}
37
+
38
+ it "#path returns ../some/dir/" do
39
+ expect(path.path).to eq '../some/dir/'
40
+ end
41
+
42
+ it "#file returns file.text" do
43
+ expect(path.file).to eq 'file.txt'
44
+ end
45
+ end
46
+
47
+ context "with absolute paths" do
48
+ let (:path) {Semi::Variables::Path.new('/some/dir/file.txt')}
49
+
50
+ it "#path returns /some/dir/" do
51
+ expect(path.path).to eq '/some/dir/'
52
+ end
53
+
54
+ it "#file returns file.text" do
55
+ expect(path.file).to eq 'file.txt'
56
+ end
57
+ end
58
+
59
+ context "with partial paths" do
60
+ let (:path) {Semi::Variables::Path.new('some/dir/file.txt')}
61
+
62
+ it "#path returns some/dir/" do
63
+ expect(path.path).to eq 'some/dir/'
64
+ end
65
+
66
+ it "#file returns file.text" do
67
+ expect(path.file).to eq 'file.txt'
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Semi::Variables::Url do
4
+
5
+ [
6
+ # Test string, pass?
7
+ ['http://web.com', 'http://web.com'],
8
+ ['http://web.com/', 'http://web.com/'],
9
+ ['https://www.help.org', 'https://www.help.org'],
10
+ ['ftp://ftp.org/foo', 'ftp://ftp.org/foo'],
11
+ ['http://host.com:8080/', 'http://host.com:8080/'],
12
+ ['http://www/', 'http://www/'],
13
+ ['http://some.host.com/page.html', 'http://some.host.com/page.html'],
14
+ ['http://a.com/page?key=val', 'http://a.com/page?key=val']
15
+ ].each do |test|
16
+ it "set '#{test[0]}' as #{test[1]}" do
17
+ expect(Semi::Variables::Url.new(test[0]).value).to eq test[1]
18
+ end
19
+ end
20
+
21
+ [
22
+ 'htt://www.host.com',
23
+ 'https:///foo.bar.com/',
24
+ 'file:///some/file/path/doc.txt',
25
+ ].each do |test|
26
+ it "rejects '#{test}'" do
27
+ expect{Semi::Variables::Url.new(test)}.to raise_error(Semi::VariableError)
28
+ end
29
+ end
30
+
31
+ let (:url) {Semi::Variables::Url.new('https://www:994/short/path/file.name?q=foo&e=bar')}
32
+
33
+ it "#proto returns https" do
34
+ expect(url.proto).to eq 'https'
35
+ end
36
+
37
+ it "#host returns www" do
38
+ expect(url.host).to eq 'www'
39
+ end
40
+
41
+ it "#port returns 994" do
42
+ expect(url.port).to eq '994'
43
+ end
44
+
45
+ it "#path returns short/path" do
46
+ expect(url.path).to eq 'short/path'
47
+ end
48
+
49
+ it "#file returns file.name" do
50
+ expect(url.file).to eq 'file.name'
51
+ end
52
+
53
+ it "#params returns q=foo&e=bar" do
54
+ expect(url.params).to eq 'q=foo&e=bar'
55
+ end
56
+
57
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: semi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerard Hickey
@@ -91,6 +91,9 @@ files:
91
91
  - lib/semi/variables/url.rb
92
92
  - lib/semi/version.rb
93
93
  - spec/semi_validator_spec.rb
94
+ - spec/semi_variables_boolean_spec.rb
95
+ - spec/semi_variables_path_spec.rb
96
+ - spec/semi_variables_url_spec.rb
94
97
  - spec/spec_helper.rb
95
98
  homepage: https://github.com/hickey/semi
96
99
  licenses: []