Class: Waves::Resources::Paths

Inherits:
Object
  • Object
show all
Defined in:
lib/waves/resources/paths.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compiledObject



7
# File 'lib/waves/resources/paths.rb', line 7

def self.compiled; @compiled ||= {} ; end

Instance Method Details

#compiled_pathsObject



9
# File 'lib/waves/resources/paths.rb', line 9

def compiled_paths; self.class.compiled ; end

#generate(template, args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/waves/resources/paths.rb', line 11

def generate( template, args )
  return "/" if template.empty?
  if template.is_a? Array
    if args.size == 1 and args.first.is_a? Hash
      process_hash( template, args.first )
    else
      process_array( template, args)
    end
  else
    "/#{ args * '/' }"
  end
end

#original_generate(template, args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/waves/resources/paths.rb', line 96

def original_generate( template, args )
  if template.is_a? Array and not template.empty?
    path = []
    ( "/#{ path * '/' }" ) if template.all? do | want |
      case want
      when true then path += args
      when String then path << want
      when Symbol then path << args.shift
      when Regexp
        component = args.shift.to_s
        raise ArgumentError, "#{component} does not match #{want.inspect}" unless component =~ want
        path << component
      when Hash
        key, value = want.to_a.first
        case value
        when true then path += args
        when String, Symbol
          # if no args to interpolate, use hash element value as default
          !args.empty? ? path << args.shift : path << value
        when Regexp
          component = args.shift.to_s
          raise ArgumentError, "#{component} does not match #{want.inspect}" unless component =~ value
          path << component
        end
      end
    end
  else
    "/#{ args * '/' }"
  end
end

#process_array(template, args) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/waves/resources/paths.rb', line 24

def process_array( template, args )
  template_key = template
  compiled = compiled_paths[template_key]
  if compiled
    return ( compiled % args ) rescue raise [template, args].inspect
  end
  compilable = true
  cpath, interpolations = "", []
  result = ( cpath % interpolations ) if template.all? do | want |
    case want
    when Symbol
      cpath << "/%s" ; interpolations << args.shift
    when String
      cpath << "/#{want}"
    when true
      compilable = false
      cpath += "/#{args.join("/")}"; args = []
    when Hash
      compilable = false
      key, value = want.to_a.first
      case value
      when true
        cpath += "/#{args.join("/")}"; args = []
      when String, Symbol
        compilable = true
        component = args.shift
        cpath << "/%s"
        component ? interpolations << component : interpolations << value 
      when Regexp
        component = args.shift.to_s
        raise ArgumentError, "#{component} does not match #{want.inspect}" unless component =~ value
        cpath << "/%s"; interpolations << component
      end
    when Regexp
      compilable = false
      component = args.shift.to_s
      raise ArgumentError, "#{component} does not match #{want.inspect}" unless component =~ want
      cpath << "/%s"; interpolations << component
    end
  end
  raise ArgumentError, "Too many args" unless args.empty?
  compiled_paths[template_key] = cpath if compilable
  result
end

#process_hash(template, hash) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/waves/resources/paths.rb', line 69

def process_hash( template, hash )
  path = []
  ( "/#{ path * '/' }" ) if template.all? do |want|
    case want
    when Symbol
      raise ArgumentError, "Path generator needs a value for #{want.inspect}" unless component = hash[want]
      path << component
    when String then path << want
    when Hash
      key, value = want.to_a.first
      case value
      when Regexp
        raise ArgumentError, "Path generator needs a value for #{want.inspect}" unless component = hash[key]
        raise ArgumentError, "#{component} does not match #{want.inspect}" unless component =~ value
        path << component
      when String, Symbol
        hash.has_key?(key) ? path << hash[key] : path << value
      when true
        raise ArgumentError, "Path generator needs a value for #{want.inspect}" unless component = hash[key]
        path += [component].flatten
      end
    when Regexp, true
      raise ArgumentError, "Path generator can't take an args hash, as it contains a Regexp or the value true"           
    end
  end
end