Class: PathString

Inherits:
String show all
Defined in:
lib/path_string.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from String

#bold, #matcher

Class Method Details

.as_sorted_json(val) ⇒ Object



29
30
31
32
33
# File 'lib/path_string.rb', line 29

def self.as_sorted_json(val)
  val = self.sort_json(val)
  val = val.pathify_strings
  val
end

.diff(a, b) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/path_string.rb', line 64

def self.diff(a,b)
  a, b = self.as_sorted_json(a), self.as_sorted_json(b)
  diff = []
  a.each_index do |i|
    unless a[i] == b[i]
      diff << {:expected => a[i], :got => b[i]}
    end
  end
  diff
end

.extract_params(known_path, entered_path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/path_string.rb', line 46

def self.extract_params(known_path, entered_path)
  params = {}.with_indifferent_access
  
  self.get_zipped_array(known_path, entered_path).each do |kp, ep|
    if kp.nil? || ep.nil?
      raise Exception.new("Cannot extract params for routes that don't match")
    end
    if kp.start_with?(":")
      if params[kp[1..-1]]
        raise Exception.new("Cannot define a route containing two parameters with the same name")
      else
        params[kp[1..-1]] = ep
      end
    end
  end
  return params
end

.paths_match?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/path_string.rb', line 17

def self.paths_match?(a, b)
  self.get_zipped_array(a, b).each do |kp, ep|
    # only known path can have things prefixed with colons which is protected
    if kp.nil? || ep.nil?
      return false
    elsif String.new(kp) != String.new(ep) && !kp.start_with?(":") && !ep.start_with?(":")
      return false
    end
  end
  return true
end

.sort_json(val) ⇒ Object

helper method to be called recursively



36
37
38
39
40
41
42
# File 'lib/path_string.rb', line 36

def self.sort_json(val)
  return val if val.is_a?(TimeDateMatcher)
  val = ActiveSupport::JSON.decode(val) if val.is_a?(String)
  val = self.stringify_keys(val).sort if val.is_a?(Hash)
  val = val.collect{|v| v.collect{|n| n.is_a?(Hash) ? self.sort_json(n) : n}} if val.is_a?(Array)
  val.sort
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



5
6
7
8
9
10
11
12
13
14
# File 'lib/path_string.rb', line 5

def == (other)
  # if either is a string that starts with a :, return true
 if self =~ /^:/ || (other.is_a?(Numeric) && self =~ /^:\d+$/) || other =~ /^:/
    return true
  elsif self =~ /\/:/ || other =~ /\/:/
    return self.class.paths_match?(self, other)
  else
    super
  end
end