Class: MagicPath::DynamicPath
- Inherits:
-
Object
- Object
- MagicPath::DynamicPath
- 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
-
#params ⇒ Object
Returns the value of attribute params.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
Instance Method Summary collapse
- #exist?(extra_params = {}) ⇒ Boolean
- #finalize(extra_params = {}) ⇒ Object
- #finalized? ⇒ Boolean
-
#initialize(pattern, params = {}) ⇒ DynamicPath
constructor
A new instance of DynamicPath.
- #inspect ⇒ Object
- #join(filename, extra_params = {}) ⇒ Object
- #mkdir_p(extra_params = {}) ⇒ Object
- #pathname(extra_params = {}) ⇒ Object
- #resolve(extra_params = {}) ⇒ Object
- #rmdir(extra_params = {}) ⇒ Object
- #to_s(extra_params = {}) ⇒ Object
- #to_str(extra_params = {}) ⇒ Object
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
#params ⇒ Object
Returns the value of attribute params.
10 11 12 |
# File 'lib/magic_path/dynamic_path.rb', line 10 def params @params end |
#pattern ⇒ Object (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
33 34 35 |
# File 'lib/magic_path/dynamic_path.rb', line 33 def exist?(extra_params = {}) File.exist? resolve(extra_params) end |
#finalize(extra_params = {}) ⇒ Object
25 26 27 |
# File 'lib/magic_path/dynamic_path.rb', line 25 def finalize(extra_params = {}) @locked_path = resolve(extra_params) end |
#finalized? ⇒ Boolean
29 30 31 |
# File 'lib/magic_path/dynamic_path.rb', line 29 def finalized? !@locked_path.nil? end |
#inspect ⇒ Object
61 62 63 |
# File 'lib/magic_path/dynamic_path.rb', line 61 def inspect "DynamicPath: #{@pattern}" end |
#join(filename, extra_params = {}) ⇒ Object
45 46 47 |
# File 'lib/magic_path/dynamic_path.rb', line 45 def join(filename, extra_params = {}) File.join(resolve(extra_params), filename) end |
#mkdir_p(extra_params = {}) ⇒ Object
37 38 39 |
# File 'lib/magic_path/dynamic_path.rb', line 37 def mkdir_p(extra_params = {}) FileUtils.mkdir_p resolve(extra_params) end |
#pathname(extra_params = {}) ⇒ Object
41 42 43 |
# File 'lib/magic_path/dynamic_path.rb', line 41 def pathname(extra_params = {}) Pathname.new resolve(extra_params) end |
#resolve(extra_params = {}) ⇒ Object
19 20 21 22 23 |
# 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) @pattern.split('/').map { |f| f[0] == ':' ? _var(f[1..-1], full_params) : f }.join('/') end |
#rmdir(extra_params = {}) ⇒ Object
57 58 59 |
# File 'lib/magic_path/dynamic_path.rb', line 57 def rmdir(extra_params = {}) Dir.rmdir resolve(extra_params) end |
#to_s(extra_params = {}) ⇒ Object
49 50 51 |
# File 'lib/magic_path/dynamic_path.rb', line 49 def to_s(extra_params = {}) resolve extra_params end |
#to_str(extra_params = {}) ⇒ Object
53 54 55 |
# File 'lib/magic_path/dynamic_path.rb', line 53 def to_str(extra_params = {}) to_s extra_params end |