semi 0.3.3 → 0.3.5
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/lib/semi/driver.rb +7 -3
- data/lib/semi/validate.rb +2 -2
- data/lib/semi/variable.rb +16 -2
- data/lib/semi/variables/base.rb +6 -1
- data/lib/semi/variables/boolean.rb +5 -0
- data/lib/semi/variables/integer.rb +4 -1
- data/lib/semi/variables/path.rb +18 -2
- data/lib/semi/variables/string.rb +1 -1
- data/lib/semi/variables/url.rb +1 -1
- data/lib/semi/version.rb +1 -1
- data/spec/semi_validator_spec.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5aafc2d5867f7093b74d1bd90ca165ea3da22354
|
4
|
+
data.tar.gz: f394cd460581e6e883fe23d182c43bd00b2fa1da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee879f014907fd71ded5389af61c12de915a9e51ea4e6aca559bfd74fbe81dffb2c0168494f97f15588ba1593d8bd59415dda03cb16037c8b6f67faec76f07e1
|
7
|
+
data.tar.gz: c5ce3748b761dfa249577a4c105c3256ce03fd51e17268e07f622b72380cb3b11d7d8600270a2beb8c4fb553c9af62257a6ad267cbe9ef4d4acda9be21fc0df4
|
data/lib/semi/driver.rb
CHANGED
@@ -12,14 +12,18 @@ module Semi
|
|
12
12
|
# Initialize the dictionary with the defaults
|
13
13
|
@dictionary = {}
|
14
14
|
@config.defaults.each_pair do |name,val|
|
15
|
-
@
|
15
|
+
hints = @config.validators[name] || nil
|
16
|
+
@dictionary[name] = Semi::Variable.import(val, hints)
|
16
17
|
end
|
17
18
|
|
18
19
|
# Now manually merge in the env vars
|
19
20
|
ENV.each_pair do |name, val|
|
20
|
-
@
|
21
|
+
hints = @config.validators[name] || nil
|
22
|
+
@dictionary[name] = Semi::Variable.import(val, hints)
|
21
23
|
end
|
22
24
|
|
25
|
+
#puts @dictionary.inspect
|
26
|
+
|
23
27
|
# Check any validations being asserted
|
24
28
|
@config.validators.each_key { |key|
|
25
29
|
begin
|
@@ -42,7 +46,7 @@ module Semi
|
|
42
46
|
# Read the template file and render
|
43
47
|
if File.exist? file
|
44
48
|
contents = File.open(file, 'r') do |fp|
|
45
|
-
renderer = ERB.new(fp.readlines.join)
|
49
|
+
renderer = ERB.new(fp.readlines.join, nil, '%<>-')
|
46
50
|
renderer.result(@dictionary.instance_eval {binding})
|
47
51
|
end
|
48
52
|
|
data/lib/semi/validate.rb
CHANGED
@@ -28,13 +28,13 @@ module Semi
|
|
28
28
|
# test for regular expression
|
29
29
|
if rule.start_with?('/') and rule.end_with?('/')
|
30
30
|
re = Regexp.new(rule[1..-2])
|
31
|
-
tests[rule] = true if re.match(value)
|
31
|
+
tests[rule] = true if re.match(value.to_s)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
35
|
failures = tests.each_pair.map { |r,t| r if not t }.compact
|
36
36
|
unless failures.empty?
|
37
|
-
raise ValidationError, "#{value} does not validate against #{failures} rules"
|
37
|
+
raise ValidationError, "#{value}(#{value.class}) does not validate against #{failures} rules"
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
data/lib/semi/variable.rb
CHANGED
@@ -11,8 +11,22 @@ module Semi
|
|
11
11
|
|
12
12
|
module Variable
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
# Variable types are ordered from most to least specific
|
15
|
+
@@var_types = { 'url' => Semi::Variables::Url,
|
16
|
+
'path' => Semi::Variables::Path,
|
17
|
+
'boolean' => Semi::Variables::Boolean,
|
18
|
+
'integer' => Semi::Variables::Integer,
|
19
|
+
'string' => Semi::Variables::String, }
|
20
|
+
|
21
|
+
def self.import(val, hints=nil)
|
22
|
+
# If hints have been supplied, try to create from them
|
23
|
+
if not hints.nil?
|
24
|
+
hints = [hints].flatten.select {|h| @@var_types.include? h }
|
25
|
+
if hints.count > 0
|
26
|
+
return @@var_types[hints[0]].new(val)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
16
30
|
# look for the obsure patterns before returning a string var
|
17
31
|
case
|
18
32
|
when Semi::Variables::Url::validate(val)
|
data/lib/semi/variables/base.rb
CHANGED
@@ -27,6 +27,11 @@ module Semi::Variables
|
|
27
27
|
def self.validate(value)
|
28
28
|
real_value = nil
|
29
29
|
|
30
|
+
# If Semi::Variables::Boolean, then get the string representation
|
31
|
+
if value.class == Semi::Variables::Boolean
|
32
|
+
value = value.to_s
|
33
|
+
end
|
34
|
+
|
30
35
|
# test to see if the value is a common true value
|
31
36
|
if value =~ /true|yes|enable/i
|
32
37
|
real_value = true
|
@@ -21,11 +21,14 @@ module Semi::Variables
|
|
21
21
|
def self.validate(value)
|
22
22
|
if value.class == Fixnum
|
23
23
|
return true
|
24
|
-
elsif value.class == Semi::Variables::Integer and value.
|
24
|
+
elsif value.class == Semi::Variables::Integer and value.to_s =~ /^\d+$/
|
25
25
|
return true
|
26
26
|
end
|
27
27
|
false
|
28
28
|
end
|
29
29
|
|
30
|
+
def method_missing(m, *args, &block)
|
31
|
+
@value.to_i.send(m, *args, &block)
|
32
|
+
end
|
30
33
|
end
|
31
34
|
end
|
data/lib/semi/variables/path.rb
CHANGED
@@ -4,6 +4,7 @@ module Semi::Variables
|
|
4
4
|
class Path < Semi::Variables::Base
|
5
5
|
|
6
6
|
@@path_re = Regexp.new('^(?<path>(?:\/|\.{1,2}\/|~(?:[a-z_][a-z0-9_]{0,30})?\/?|[^~][^\/\000]+\/)*?)(?<file>[^\/\000]+)?$')
|
7
|
+
@@path_score_threshold = 0.200
|
7
8
|
|
8
9
|
def initialize(val)
|
9
10
|
if @@path_re.match(val)
|
@@ -19,13 +20,28 @@ module Semi::Variables
|
|
19
20
|
|
20
21
|
def self.validate(value)
|
21
22
|
if ['String', 'Semi::Variables::Path'].include? value.class.to_s
|
22
|
-
if @@path_re.match(value)
|
23
|
-
|
23
|
+
if @@path_re.match(value.to_s)
|
24
|
+
if path_score(value.to_s) > @@path_score_threshold
|
25
|
+
return true
|
26
|
+
end
|
24
27
|
end
|
25
28
|
end
|
26
29
|
false
|
27
30
|
end
|
28
31
|
|
32
|
+
# provide a simple scoring method to assist in identifying
|
33
|
+
# a string as a path. 0.0 is a pure string with no path
|
34
|
+
# markers,
|
35
|
+
def self.path_score(path)
|
36
|
+
return 0.0 if path.empty?
|
37
|
+
|
38
|
+
# use path "special" chars to calculate the score
|
39
|
+
metach = path.count('/.~')
|
40
|
+
plen = path.length
|
41
|
+
#puts "#{path} m/p/s = #{metach}/#{plen}/#{1.0 - (((plen - metach) * 1.0) / (plen + metach))}"
|
42
|
+
1.0 - (((plen - metach)*1.0) / (plen + metach))
|
43
|
+
end
|
44
|
+
|
29
45
|
def path
|
30
46
|
match = @@path_re.match(@value)
|
31
47
|
if match
|
data/lib/semi/variables/url.rb
CHANGED
data/lib/semi/version.rb
CHANGED
data/spec/semi_validator_spec.rb
CHANGED
@@ -20,7 +20,7 @@ describe "Semi::validator" do
|
|
20
20
|
['/etc/passwd', 'path', true],
|
21
21
|
['../parent', ['path'], true],
|
22
22
|
['.005', 'path', true],
|
23
|
-
['file.name', 'path',
|
23
|
+
['file.name', 'path', false],
|
24
24
|
['~user', 'path', true],
|
25
25
|
['~user/dir/file', 'path', true],
|
26
26
|
['http://www.simple.com',
|