class MagicPath::DynamicPath

A class to dynamically build paths based on parameters Constructed with a pattern like: /test_data/:product/:state/:env Each string beginning with a colon will be replaced from either the params hash passed to the initializer, the “extra_params” hash passed to resolve or environment variables (via Nenv). extra_params is merged into the instance level params hash before resolution

Attributes

params[RW]
pattern[R]

Public Class Methods

new(pattern, params = {}) click to toggle source
# File lib/magic_path/dynamic_path.rb, line 13
def initialize(pattern, params = {})
  @params = params || {}
  @pattern = pattern
  @locked_path = nil
end

Public Instance Methods

exist?(extra_params = {}) click to toggle source
# File lib/magic_path/dynamic_path.rb, line 35
def exist?(extra_params = {})
  File.exist? resolve(extra_params)
end
finalize(extra_params = {}) click to toggle source
# File lib/magic_path/dynamic_path.rb, line 27
def finalize(extra_params = {})
  @locked_path = resolve(extra_params)
end
finalized?() click to toggle source
# File lib/magic_path/dynamic_path.rb, line 31
def finalized?
  !@locked_path.nil?
end
inspect() click to toggle source
# File lib/magic_path/dynamic_path.rb, line 63
def inspect
  "DynamicPath: #{@pattern}"
end
join(filename, extra_params = {}) click to toggle source
# File lib/magic_path/dynamic_path.rb, line 47
def join(filename, extra_params = {})
  File.join(resolve(extra_params), filename)
end
mkdir_p(extra_params = {}) click to toggle source
# File lib/magic_path/dynamic_path.rb, line 39
def mkdir_p(extra_params = {})
  FileUtils.mkdir_p resolve(extra_params)
end
pathname(extra_params = {}) click to toggle source
# File lib/magic_path/dynamic_path.rb, line 43
def pathname(extra_params = {})
  Pathname.new resolve(extra_params)
end
resolve(extra_params = {}) click to toggle source
# File lib/magic_path/dynamic_path.rb, line 19
def resolve(extra_params = {})
  return @locked_path unless @locked_path.nil?
  full_params = @params.merge(extra_params)
  resolved_path = @pattern.dup
  @pattern.scan(/(\:\w(?:\w+|\d+))/).flatten.each { |t| resolved_path.gsub!(t, _var(t, full_params)) }
  resolved_path
end
rmdir(extra_params = {}) click to toggle source
# File lib/magic_path/dynamic_path.rb, line 59
def rmdir(extra_params = {})
  Dir.rmdir resolve(extra_params)
end
to_s(extra_params = {}) click to toggle source
# File lib/magic_path/dynamic_path.rb, line 51
def to_s(extra_params = {})
  resolve extra_params
end
to_str(extra_params = {}) click to toggle source
# File lib/magic_path/dynamic_path.rb, line 55
def to_str(extra_params = {})
  to_s extra_params
end

Private Instance Methods

_resolver_for(var_name) click to toggle source
# File lib/magic_path/dynamic_path.rb, line 79
def _resolver_for(var_name)
  MagicPath.resolvers.find { |r| r.respond_to?(var_name) }
end
_var(var, full_params = {}) click to toggle source
# File lib/magic_path/dynamic_path.rb, line 69
def _var(var, full_params = {})
  var_name = var.delete ':'
  return full_params[var_name] if full_params.key?(var_name)
  return full_params[var_name.to_sym] if full_params.key?(var_name.to_sym)
  return MagicPath.send(var_name.to_sym).resolve(full_params) if MagicPath.instance.respond_to? var_name
  resolver = _resolver_for(var_name)
  return resolver.send(var_name) unless resolver.nil?
  raise ArgumentError, "Could not locate #{var_name}, in params or resolvers."
end