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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 60f7b3709ae388fbca46439b4eb751f5393c1188
4
- data.tar.gz: 98495dcaa1f7b8d6e2b598415c0cd2da3e149b7d
3
+ metadata.gz: 5aafc2d5867f7093b74d1bd90ca165ea3da22354
4
+ data.tar.gz: f394cd460581e6e883fe23d182c43bd00b2fa1da
5
5
  SHA512:
6
- metadata.gz: f53f6464cb5279c899693a3329801281f677598e462237080839b184d923680d597ebd6e3011a8a4603a7f7981fddd0f24e9412a6ba9d9c612198622be3860e7
7
- data.tar.gz: 2df03e469ef11dc22c35c314068c127c091eff7d976d106edf555770722f758bbfd75c4bcfcaa47c9fb72054aae82af0081009a3fbae8ce7184b67e25767fe5a
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
- @dictionary[name] = Semi::Variable.import(val)
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
- @dictionary[name] = Semi::Variable.import(val)
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
- def self.import(val)
15
- puts "#{val}: #{val.class}"
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)
@@ -14,10 +14,15 @@ module Semi
14
14
  def to_s
15
15
  @value.to_s
16
16
  end
17
-
17
+
18
18
  def value
19
19
  @value
20
20
  end
21
+
22
+ def method_missing(m, *args, &block)
23
+ @value.to_s.send(m, *args, &block)
24
+ end
25
+
21
26
  end
22
27
  end
23
28
  end
@@ -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.value =~ /^\d+$/
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
@@ -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
- return true
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
@@ -8,7 +8,7 @@ module Semi::Variables
8
8
  end
9
9
 
10
10
  def self.validate(value)
11
- if value.class.to_s == 'String'
11
+ if ['String', 'Semi::Variables::String'].include? value.class.to_s
12
12
  return true
13
13
  end
14
14
  false
@@ -19,7 +19,7 @@ module Semi::Variables
19
19
 
20
20
  def self.validate(val)
21
21
  if ['String', 'Semi::Variables::Url'].include? val.class.to_s
22
- if @@url_re.match(val)
22
+ if @@url_re.match(val.to_s)
23
23
  return true
24
24
  end
25
25
  end
data/lib/semi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Semi
2
- version = '0.3.3'
2
+ version = '0.3.5'
3
3
  end
@@ -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', true],
23
+ ['file.name', 'path', false],
24
24
  ['~user', 'path', true],
25
25
  ['~user/dir/file', 'path', true],
26
26
  ['http://www.simple.com',
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.3
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerard Hickey