Class: Dao::Path

Inherits:
String
  • Object
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.



80
81
82
83
84
# File 'lib/dao/path.rb', line 80

def initialize(*args, &block)
  super(args.join('/'), &block)
  normalize!
  compile!
end

Instance Attribute Details

#interfaceObject

Returns the value of attribute interface.



78
79
80
# File 'lib/dao/path.rb', line 78

def interface
  @interface
end

#keysObject

Returns the value of attribute keys.



75
76
77
# File 'lib/dao/path.rb', line 75

def keys
  @keys
end

#paramsObject

Returns the value of attribute params.



77
78
79
# File 'lib/dao/path.rb', line 77

def params
  @params
end

#patternObject

Returns the value of attribute pattern.



76
77
78
# File 'lib/dao/path.rb', line 76

def pattern
  @pattern
end

#resultObject

Returns the value of attribute result.



74
75
76
# File 'lib/dao/path.rb', line 74

def result
  @result
end

Class Method Details

.=~Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/dao/path.rb', line 33

def params?(string)
  string.to_s =~ %r{/:[^/]+}
end

.absolute?(string) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/dao/path.rb', line 35

def absolute?(string)
  string.to_s =~ %r|^\s*/|
end

.absolute_path_for(arg, *args) ⇒ Object



26
27
28
# File 'lib/dao/path.rb', line 26

def absolute_path_for(arg, *args)
  ('/' + paths_for(arg, *args).join('/')).squeeze('/')
end

.defaultObject



9
10
11
# File 'lib/dao/path.rb', line 9

def default
  Path.for(:dao)
end

.expand!(path, params = {}) ⇒ Object



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

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

.extract_params(enumerable, keys) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dao/path.rb', line 58

def extract_params(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



13
14
15
# File 'lib/dao/path.rb', line 13

def for(*args)
  new(absolute_path_for(*args))
end

.keys_for(path) ⇒ Object



39
40
41
42
# File 'lib/dao/path.rb', line 39

def keys_for(path)
  path = absolute_path_for(path)
  path.scan(%r{/:[^/]+}).map{|key| key.sub(%r{^/:}, '')}
end

.params?(string) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/dao/path.rb', line 30

def params?(string)
  string.to_s =~ %r{/:[^/]+}
end

.paths_for(arg, *args) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/dao/path.rb', line 17

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



52
53
54
55
56
# File 'lib/dao/path.rb', line 52

def pattern_for(path)
  path = absolute_path_for(path)
  re = path.gsub(%r{/:[^/]+}, '/([^/]+)')
  /^#{ re }$/i
end

Instance Method Details

#+(other) ⇒ Object



138
139
140
# File 'lib/dao/path.rb', line 138

def +(other)
  Path.for(super)
end

#absolute?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/dao/path.rb', line 120

def absolute?
  Path.absolute?(self)
end

#compile!Object



94
95
96
97
98
99
# File 'lib/dao/path.rb', line 94

def compile!
  @keys = Path.keys_for(self)
  @pattern = Path.pattern_for(self)
  @params = Map.new
  @path = dup
end

#expand(params) ⇒ Object



101
102
103
# File 'lib/dao/path.rb', line 101

def expand(params)
  dup.expand!(params)
end

#expand!(params) ⇒ Object

Raises:



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/dao/path.rb', line 107

def expand!(params)
  replace(@path.dup)
  @params = extract_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

#extract_params(enumerable) ⇒ Object



134
135
136
# File 'lib/dao/path.rb', line 134

def extract_params(enumerable)
  Path.extract_params(enumerable, @keys)
end

#match(other) ⇒ Object



124
125
126
127
# File 'lib/dao/path.rb', line 124

def match(other)
  matched, *matches = @pattern.match(other).to_a
  matched ? expand(matches) : false
end

#match!(other) ⇒ Object



129
130
131
132
# File 'lib/dao/path.rb', line 129

def match!(other)
  matched, *matches = @pattern.match(other).to_a
  matched ? expand!(matches) : false
end

#normalize!Object



90
91
92
# File 'lib/dao/path.rb', line 90

def normalize!
  replace(Path.absolute_path_for(self))
end

#params?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/dao/path.rb', line 86

def params?
  Path.params?(self)
end

#to_yaml(*args, &block) ⇒ Object



142
143
144
# File 'lib/dao/path.rb', line 142

def to_yaml(*args, &block)
  "#{ self }".to_yaml(*args, &block)
end