Class: Semi::Variables::Path

Inherits:
Base
  • Object
show all
Defined in:
lib/semi/variables/path.rb

Constant Summary collapse

@@path_re =
Regexp.new('^(?<path>(?:\/|\.{1,2}\/|~(?:[a-z_][a-z0-9_]{0,30})?\/?|[^~][^\/\000]+\/)*?)(?<file>[^\/\000]+)?$')
@@path_score_threshold =
0.200

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#!=, #!~, #&, #<=>, #==, #===, #=~, #^, #eql?, #equal?, #method_missing, #set, #to_s, #value, #|

Constructor Details

#initialize(val) ⇒ Path

Returns a new instance of Path.



9
10
11
12
13
14
15
# File 'lib/semi/variables/path.rb', line 9

def initialize(val)
  if @@path_re.match(val)
    @value = val
  else
    raise Semi::VariableError, '#{val} does not look like a path'
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Semi::Variables::Base

Class Method Details

.path_score(path) ⇒ Object

provide a simple scoring method to assist in identifying a string as a path. 0.0 is a pure string with no path markers,



35
36
37
38
39
40
41
42
43
# File 'lib/semi/variables/path.rb', line 35

def self.path_score(path)
  return 0.0 if path.empty?

  # use path "special" chars to calculate the score
  metach = path.count('/.~')
  plen = path.length
  #puts "#{path} m/p/s = #{metach}/#{plen}/#{1.0 - (((plen - metach) * 1.0) / (plen + metach))}"
  1.0 - (((plen - metach)*1.0) / (plen + metach))
end

.validate(value) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/semi/variables/path.rb', line 21

def self.validate(value)
  if ['String', 'Semi::Variables::Path'].include? value.class.to_s
    if @@path_re.match(value.to_s)
      if path_score(value.to_s) > @@path_score_threshold
        return true
      end
    end
  end
  false
end

Instance Method Details

#fileObject



52
53
54
55
56
57
# File 'lib/semi/variables/path.rb', line 52

def file
  match = @@path_re.match(@value)
  if match
    match['file']
  end
end

#pathObject



45
46
47
48
49
50
# File 'lib/semi/variables/path.rb', line 45

def path
  match = @@path_re.match(@value)
  if match
    match['path']
  end
end

#validateObject



17
18
19
# File 'lib/semi/variables/path.rb', line 17

def validate
  self.validate(@value)
end