Skip to content

Donavan/magic_path

Repository files navigation

magic_path

A gem for dynamic file paths. Paths can be defined using strings with variable substitution. i.e. 'fixtures/:product/:state". When the path is resolved into a string any part that begins with a colon will be replaced with it's value from a params hash or resolver object.

Installation

Add this line to your application's Gemfile:

gem 'magic_path'

And then execute:

$ bundle

Or install it yourself as:

$ gem install magic_path

Usage

Define a path and pass params to resolve

# Create our path
MagicPath.create_path :my_path, { pattern: 'data/:state/fixtures' }

# use the path
MagicPath.my_path.resolve { state: 'ohio' }
# /data/ohio/fixtures

Define a path using initial params

# Create our path
MagicPath.create_path :my_path, { pattern: 'data/:state/:product/fixtures', params: { product: 'foobar' } }

# use the path
MagicPath.my_path.resolve { state: 'ohio' }
# /data/ohio/foobar/fixtures

Define a path using other paths as variables

## Create our base path
MagicPath.create_path :base_data, { pattern: 'data/:state/:product', params: { product: 'foobar' } }
# Create our path
MagicPath.create_path :my_path, { pattern: ':base_data/fixtures' }

# use the path
MagicPath.my_path.resolve { state: 'ohio' }
# /data/ohio/foobar/fixtures

Define a path using environment variables (via Nenv)

require 'Nenv' 

# Create our path
MagicPath.create_path :my_path, { pattern: 'data/:test_env/fixtures' }

# use the path (Assuming test_env has been set to "test" in the environment)
MagicPath.my_path.to_s # to_s is the same as calling resolve without an aditional params hash.
# /data/test/fixtures

Resolvers

A resolver is a simply an object that can respond to a variable name. You can create your own and add the to MagicPath like so:

class MyResolver
  def test_env
    "test"
  end
end

# Create our path
MagicPath.create_path :my_path, { pattern: 'data/:test_env/fixtures' }
MagicPath.add_resolver MyResolver.new
MagicPath.my_path.to_s # to_s is the same as calling resolve without an aditional params hash.
# /data/test/fixtures

If Nenv has already been required by your code, it will be automatically used as a resolver.

About

A gem for dynamic file paths.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published