Sha256: 34dc12f77e36ea9d5df2f8526af908be58be8dc454bb2f62f6584c4e9167ea04

Contents?: true

Size: 1.43 KB

Versions: 9

Compression:

Stored size: 1.43 KB

Contents

require_relative 'base'

module Semi::Variables
  class Path < Semi::Variables::Base

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

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

    def validate
      self.validate(@value)
    end

    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

    # provide a simple scoring method to assist in identifying
    # a string as a path. 0.0 is a pure string with no path
    # markers,
    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

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

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

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
semi-0.8.6 lib/semi/variables/path.rb
semi-0.8.5 lib/semi/variables/path.rb
semi-0.8.4 lib/semi/variables/path.rb
semi-0.8.3 lib/semi/variables/path.rb
semi-0.8.2 lib/semi/variables/path.rb
semi-0.8.1 lib/semi/variables/path.rb
semi-0.8.0 lib/semi/variables/path.rb
semi-0.7.1 lib/semi/variables/path.rb
semi-0.7.0 lib/semi/variables/path.rb