Class: Dao::Path
- Inherits:
-
String
show all
- Defined in:
- lib/dao/path.rb
Defined Under Namespace
Classes: Error, ParamsError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args, &block) ⇒ Path
Returns a new instance of Path.
81
82
83
84
85
|
# File 'lib/dao/path.rb', line 81
def initialize(*args, &block)
super(args.join('/'), &block)
normalize!
compile!
end
|
Instance Attribute Details
#interface ⇒ Object
Returns the value of attribute interface.
79
80
81
|
# File 'lib/dao/path.rb', line 79
def interface
@interface
end
|
#keys ⇒ Object
Returns the value of attribute keys.
76
77
78
|
# File 'lib/dao/path.rb', line 76
def keys
@keys
end
|
#params ⇒ Object
Returns the value of attribute params.
78
79
80
|
# File 'lib/dao/path.rb', line 78
def params
@params
end
|
#pattern ⇒ Object
Returns the value of attribute pattern.
77
78
79
|
# File 'lib/dao/path.rb', line 77
def pattern
@pattern
end
|
#result ⇒ Object
Returns the value of attribute result.
75
76
77
|
# File 'lib/dao/path.rb', line 75
def result
@result
end
|
Class Method Details
.=~ ⇒ Boolean
34
35
36
|
# File 'lib/dao/path.rb', line 34
def params?(string)
string.to_s =~ %r{/:[^/]+}
end
|
.absolute?(string) ⇒ Boolean
36
37
38
|
# File 'lib/dao/path.rb', line 36
def absolute?(string)
string.to_s =~ %r|^\s*/|
end
|
.absolute_path_for(arg, *args) ⇒ Object
27
28
29
|
# File 'lib/dao/path.rb', line 27
def absolute_path_for(arg, *args)
('/' + paths_for(arg, *args).join('/')).squeeze('/')
end
|
.default ⇒ Object
10
11
12
|
# File 'lib/dao/path.rb', line 10
def default
Path.for(:dao)
end
|
.expand!(path, params = {}) ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/dao/path.rb', line 45
def expand!(path, params = {})
params = Map.for(params)
path.keys.each do |key|
path.gsub!(%r{/:#{ Regexp.escape(key) }\b}, params[key])
end
path
end
|
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/dao/path.rb', line 59
def (enumerable, keys)
params = Map.new
keys = Array(keys)
case enumerable
when Array
keys.each_with_index{|key, index| params[key] = enumerable[index]}
when Hash
enumerable = Map.for(enumerable)
keys.each{|key| params[key] = enumerable[key]}
else
raise(ArgumentError, enumerable.class.name)
end
params
end
|
.for(*args) ⇒ Object
14
15
16
|
# File 'lib/dao/path.rb', line 14
def for(*args)
new(absolute_path_for(*args))
end
|
.keys_for(path) ⇒ Object
40
41
42
43
|
# File 'lib/dao/path.rb', line 40
def keys_for(path)
path = absolute_path_for(path)
path.scan(%r{/:[^/]+}).map{|key| key.sub(%r{^/:}, '')}
end
|
.params?(string) ⇒ Boolean
31
32
33
|
# File 'lib/dao/path.rb', line 31
def params?(string)
string.to_s =~ %r{/:[^/]+}
end
|
.paths_for(arg, *args) ⇒ Object
18
19
20
21
22
23
24
25
|
# File 'lib/dao/path.rb', line 18
def paths_for(arg, *args)
path = [arg, *args].flatten.compact.join('/')
path.gsub!(%r|[.]+/|, '/')
path.squeeze!('/')
path.sub!(%r|^/|, '')
path.sub!(%r|/$|, '')
paths = path.split('/')
end
|
.pattern_for(path) ⇒ Object
53
54
55
56
57
|
# File 'lib/dao/path.rb', line 53
def pattern_for(path)
path = absolute_path_for(path)
re = path.gsub(%r{/:[^/]+}, '/([^/]+)')
/^#{ re }$/i
end
|
Instance Method Details
#+(other) ⇒ Object
139
140
141
|
# File 'lib/dao/path.rb', line 139
def +(other)
Path.for(super)
end
|
#absolute? ⇒ Boolean
121
122
123
|
# File 'lib/dao/path.rb', line 121
def absolute?
Path.absolute?(self)
end
|
#compile! ⇒ Object
95
96
97
98
99
100
|
# File 'lib/dao/path.rb', line 95
def compile!
@keys = Path.keys_for(self)
@pattern = Path.pattern_for(self)
@params = Map.new
@path = dup
end
|
#expand(params) ⇒ Object
102
103
104
|
# File 'lib/dao/path.rb', line 102
def expand(params)
dup.expand!(params)
end
|
#expand!(params) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/dao/path.rb', line 108
def expand!(params)
replace(@path.dup)
@params = (params)
keys.each do |key|
next if @params[key].nil?
re = %r{/:#{ Regexp.escape(key) }\b}
val = "/#{ @params[key] }"
self.gsub!(re, val)
end
raise(Error::Params, "#{ self }(#{ @params.inspect })") if params?
self
end
|
135
136
137
|
# File 'lib/dao/path.rb', line 135
def (enumerable)
Path.(enumerable, @keys)
end
|
#match(other) ⇒ Object
125
126
127
128
|
# File 'lib/dao/path.rb', line 125
def match(other)
matched, *matches = @pattern.match(other).to_a
matched ? expand(matches) : false
end
|
#match!(other) ⇒ Object
130
131
132
133
|
# File 'lib/dao/path.rb', line 130
def match!(other)
matched, *matches = @pattern.match(other).to_a
matched ? expand!(matches) : false
end
|
#params? ⇒ Boolean
87
88
89
|
# File 'lib/dao/path.rb', line 87
def params?
Path.params?(self)
end
|
#to_yaml(*args, &block) ⇒ Object
143
144
145
|
# File 'lib/dao/path.rb', line 143
def to_yaml(*args, &block)
"#{ self }".to_yaml(*args, &block)
end
|