Class: MagicPath::DynamicPath

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_path/dynamic_path.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, params = {}) ⇒ DynamicPath

Returns a new instance of DynamicPath.



13
14
15
16
17
# File 'lib/magic_path/dynamic_path.rb', line 13

def initialize(pattern, params = {})
  @params = params || {}
  @pattern = pattern
  @locked_path = nil
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



10
11
12
# File 'lib/magic_path/dynamic_path.rb', line 10

def params
  @params
end

#patternObject (readonly)

Returns the value of attribute pattern.



11
12
13
# File 'lib/magic_path/dynamic_path.rb', line 11

def pattern
  @pattern
end

Instance Method Details

#exist?(extra_params = {}) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/magic_path/dynamic_path.rb', line 35

def exist?(extra_params = {})
  File.exist? resolve(extra_params)
end

#finalize(extra_params = {}) ⇒ Object



27
28
29
# File 'lib/magic_path/dynamic_path.rb', line 27

def finalize(extra_params = {})
  @locked_path = resolve(extra_params)
end

#finalized?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/magic_path/dynamic_path.rb', line 31

def finalized?
  !@locked_path.nil?
end

#inspectObject



63
64
65
# File 'lib/magic_path/dynamic_path.rb', line 63

def inspect
  "DynamicPath: #{@pattern}"
end

#join(filename, extra_params = {}) ⇒ Object



47
48
49
# 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 = {}) ⇒ Object



39
40
41
# File 'lib/magic_path/dynamic_path.rb', line 39

def mkdir_p(extra_params = {})
  FileUtils.mkdir_p resolve(extra_params)
end

#pathname(extra_params = {}) ⇒ Object



43
44
45
# File 'lib/magic_path/dynamic_path.rb', line 43

def pathname(extra_params = {})
  Pathname.new resolve(extra_params)
end

#resolve(extra_params = {}) ⇒ Object



19
20
21
22
23
24
25
# 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 = {}) ⇒ Object



59
60
61
# File 'lib/magic_path/dynamic_path.rb', line 59

def rmdir(extra_params = {})
  Dir.rmdir resolve(extra_params)
end

#to_s(extra_params = {}) ⇒ Object



51
52
53
# File 'lib/magic_path/dynamic_path.rb', line 51

def to_s(extra_params = {})
  resolve extra_params
end

#to_str(extra_params = {}) ⇒ Object



55
56
57
# File 'lib/magic_path/dynamic_path.rb', line 55

def to_str(extra_params = {})
  to_s extra_params
end