magic_path 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 60fc211126220aad9cfa38f5267ee8817c98d08e
4
+ data.tar.gz: b850dace2bf6c6f75dff819a654d3680ad43b495
5
+ SHA512:
6
+ metadata.gz: 10da9388d49cedaafc28ce05c5ee10ae4f09996333ccd4b1efc30ffbf0bdbc5f363503194d5f4edc415f6368823557b40c0e4e7f2cc15b127a4c40c14c2186a8
7
+ data.tar.gz: 7ae953bbb7ba30a278ed75e97802d8793d891a77c524c0765ec947d0b17ccf4d6e73090495c6333f1fdab61644fd094dbe979b2a673c711c33e03b33417f58a7
data/.gitignore ADDED
@@ -0,0 +1,51 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ Lint/Debugger:
2
+ Exclude:
3
+ - 'bin/console'
4
+
5
+ Style/EndOfLine:
6
+ Enabled: false
7
+
8
+ Metrics/LineLength:
9
+ Max: 100
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.4
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in magic_path.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ magic_path (0.1.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ ast (2.3.0)
10
+ byebug (9.0.6)
11
+ coderay (1.1.1)
12
+ diff-lcs (1.3)
13
+ method_source (0.8.2)
14
+ nenv (0.3.0)
15
+ parallel (1.12.0)
16
+ parser (2.4.0.2)
17
+ ast (~> 2.3)
18
+ powerpack (0.1.1)
19
+ pry (0.10.4)
20
+ coderay (~> 1.1.0)
21
+ method_source (~> 0.8.1)
22
+ slop (~> 3.4)
23
+ pry-byebug (3.4.2)
24
+ byebug (~> 9.0)
25
+ pry (~> 0.10)
26
+ rainbow (2.2.2)
27
+ rake
28
+ rake (10.5.0)
29
+ rspec (3.6.0)
30
+ rspec-core (~> 3.6.0)
31
+ rspec-expectations (~> 3.6.0)
32
+ rspec-mocks (~> 3.6.0)
33
+ rspec-core (3.6.0)
34
+ rspec-support (~> 3.6.0)
35
+ rspec-expectations (3.6.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.6.0)
38
+ rspec-mocks (3.6.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.6.0)
41
+ rspec-support (3.6.0)
42
+ rubocop (0.51.0)
43
+ parallel (~> 1.10)
44
+ parser (>= 2.3.3.1, < 3.0)
45
+ powerpack (~> 0.1)
46
+ rainbow (>= 2.2.2, < 3.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (~> 1.0, >= 1.0.1)
49
+ ruby-progressbar (1.9.0)
50
+ slop (3.6.0)
51
+ unicode-display_width (1.3.0)
52
+
53
+ PLATFORMS
54
+ ruby
55
+ x86-mingw32
56
+
57
+ DEPENDENCIES
58
+ bundler (~> 1.13)
59
+ magic_path!
60
+ nenv (~> 0.3)
61
+ pry (~> 0.10)
62
+ pry-byebug (~> 3.4)
63
+ rake (~> 10.0)
64
+ rspec (~> 3.0)
65
+ rubocop
66
+
67
+ BUNDLED WITH
68
+ 1.16.0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Donavan Stanley
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # magic_path
2
+ 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.
3
+
4
+
5
+
6
+ # Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'magic_path'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install magic_path
21
+
22
+ ## Usage
23
+
24
+ ### Define a path and pass params to resolve
25
+ ```ruby
26
+ # Create our path
27
+ MagicPath.create_path :my_path, { pattern: 'data/:state/fixtures' }
28
+
29
+ # use the path
30
+ MagicPath.my_path.resolve { state: 'ohio' }
31
+ # /data/ohio/fixtures
32
+ ```
33
+
34
+ ### Define a path using initial params
35
+ ```ruby
36
+ # Create our path
37
+ MagicPath.create_path :my_path, { pattern: 'data/:state/:product/fixtures', params: { product: 'foobar' } }
38
+
39
+ # use the path
40
+ MagicPath.my_path.resolve { state: 'ohio' }
41
+ # /data/ohio/foobar/fixtures
42
+ ```
43
+
44
+ ### Define a path using other paths as variables
45
+ ```ruby
46
+ ## Create our base path
47
+ MagicPath.create_path :base_data, { pattern: 'data/:state/:product', params: { product: 'foobar' } }
48
+ # Create our path
49
+ MagicPath.create_path :my_path, { pattern: ':base_data/fixtures' }
50
+
51
+ # use the path
52
+ MagicPath.my_path.resolve { state: 'ohio' }
53
+ # /data/ohio/foobar/fixtures
54
+ ```
55
+
56
+ ### Define a path using environment variables (via Nenv)
57
+ ```ruby
58
+ require 'Nenv'
59
+
60
+ # Create our path
61
+ MagicPath.create_path :my_path, { pattern: 'data/:test_env/fixtures' }
62
+
63
+ # use the path (Assuming test_env has been set to "test" in the environment)
64
+ MagicPath.my_path.to_s # to_s is the same as calling resolve without an aditional params hash.
65
+ # /data/test/fixtures
66
+ ```
67
+
68
+ ## Resolvers
69
+ 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:
70
+
71
+ ```ruby
72
+ class MyResolver
73
+ def test_env
74
+ "test"
75
+ end
76
+ end
77
+
78
+ # Create our path
79
+ MagicPath.create_path :my_path, { pattern: 'data/:test_env/fixtures' }
80
+ MagicPath.add_resolver MyResolver.new
81
+ MagicPath.my_path.to_s # to_s is the same as calling resolve without an aditional params hash.
82
+ # /data/test/fixtures
83
+ ```
84
+
85
+ If Nenv has already been required by your code, it will be automatically used as a resolver.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'magic_path'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+
14
+
15
+ require 'pry'
16
+ # rubocop:disable Lint/Debugger
17
+ binding.pry
18
+ puts 'Obligatory line for pry.'
19
+ # rubocop:enable Lint/Debugger
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,81 @@
1
+ # Root namespace for our gem
2
+ module MagicPath
3
+ # A class to dynamically build paths based on parameters
4
+ # Constructed with a pattern like: /test_data/:product/:state/:env
5
+ # Each string beginning with a colon will be replaced from either
6
+ # the params hash passed to the initializer, the "extra_params" hash passed to resolve
7
+ # or environment variables (via Nenv).
8
+ # extra_params is merged into the instance level params hash before resolution
9
+ class DynamicPath
10
+ attr_accessor :params
11
+ attr_reader :pattern
12
+
13
+ def initialize(pattern, params = {})
14
+ @params = params || {}
15
+ @pattern = pattern
16
+ @locked_path = nil
17
+ end
18
+
19
+ def resolve(extra_params = {})
20
+ return @locked_path unless @locked_path.nil?
21
+ full_params = @params.merge(extra_params)
22
+ @pattern.split('/').map { |f| f[0] == ':' ? _var(f[1..-1], full_params) : f }.join('/')
23
+ end
24
+
25
+ def finalize(extra_params = {})
26
+ @locked_path = resolve(extra_params)
27
+ end
28
+
29
+ def finalized?
30
+ !@locked_path.nil?
31
+ end
32
+
33
+ def exist?(extra_params = {})
34
+ File.exist? resolve(extra_params)
35
+ end
36
+
37
+ def mkdir_p(extra_params = {})
38
+ FileUtils.mkdir_p resolve(extra_params)
39
+ end
40
+
41
+ def pathname(extra_params = {})
42
+ Pathname.new resolve(extra_params)
43
+ end
44
+
45
+ def join(filename, extra_params = {})
46
+ File.join(resolve(extra_params), filename)
47
+ end
48
+
49
+ def to_s(extra_params = {})
50
+ resolve extra_params
51
+ end
52
+
53
+ def to_str(extra_params = {})
54
+ to_s extra_params
55
+ end
56
+
57
+ def rmdir(extra_params = {})
58
+ Dir.rmdir resolve(extra_params)
59
+ end
60
+
61
+ def inspect
62
+ "DynamicPath: #{@pattern}"
63
+ end
64
+
65
+ private
66
+
67
+ def _var(var_name, full_params = {})
68
+ return full_params[var_name] if full_params.key?(var_name)
69
+ return full_params[var_name.to_sym] if full_params.key?(var_name.to_sym)
70
+
71
+ return MagicPath.send(var_name).resolve(full_params) if MagicPath.respond_to? var_name
72
+ resolver = _resolver_for(var_name)
73
+ return resolver.send(var_name) unless resolver.nil?
74
+ raise ArgumentError, "Could not locate #{var_name}, in params or resolvers."
75
+ end
76
+
77
+ def _resolver_for(var_name)
78
+ MagicPath.resolvers.find { |r| r.respond_to?(var_name) }
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,68 @@
1
+ require 'magic_path/dynamic_path'
2
+
3
+ module MagicPath
4
+ class PathManager
5
+ class Error < ArgumentError
6
+ end
7
+
8
+ class MethodError < Error
9
+ def initialize(meth)
10
+ @meth = meth
11
+ end
12
+ end
13
+
14
+ class AlreadyExistsError < MethodError
15
+ def message
16
+ format('Path %s already exists', @meth.inspect)
17
+ end
18
+ end
19
+
20
+ attr_writer :resolvers
21
+ def resolvers
22
+ @resolvers ||= default_resolvers
23
+ end
24
+
25
+ def default_resolvers
26
+ defined?(Nenv) ? [Nenv] : []
27
+ end
28
+
29
+ def add_resolver(resolver)
30
+ resolvers << resolver
31
+ end
32
+
33
+ def create_path(meth, opts)
34
+ self.class._create_path_accessor(singleton_class, meth)
35
+ send("#{meth}=", opts)
36
+ end
37
+
38
+ class << self
39
+ def create_path(meth, opts)
40
+ _create_path_accessor(self, meth, opts)
41
+ send("#{meth}=", opts)
42
+ end
43
+
44
+ def _create_path_accessor(klass, meth)
45
+ _fail_if_accessor_exists(klass, meth)
46
+ _create_path_writer(klass, meth)
47
+ _create_path_reader(klass, meth)
48
+ end
49
+
50
+ def _create_path_writer(klass, meth)
51
+ klass.send(:define_method, "#{meth}=") do |opts|
52
+ path = DynamicPath.new(opts[:pattern], opts[:params])
53
+ klass.class_variable_set("@@#{meth}".to_sym, path)
54
+ end
55
+ end
56
+
57
+ def _create_path_reader(klass, meth)
58
+ klass.send(:define_method, meth.to_s.delete('=')) do
59
+ klass.class_variable_get("@@#{meth}".to_sym)
60
+ end
61
+ end
62
+
63
+ def _fail_if_accessor_exists(klass, meth)
64
+ raise(AlreadyExistsError, meth) if klass.method_defined?(meth)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,3 @@
1
+ module MagicPath
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/magic_path.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'magic_path/version'
2
+ require 'magic_path/dynamic_path'
3
+ # Root module for Magic Path
4
+ require 'magic_path/path_manager'
5
+ module MagicPath
6
+ class << self
7
+ def respond_to?(meth)
8
+ instance.respond_to?(meth)
9
+ end
10
+
11
+ def method_missing(meth, *args)
12
+ instance.send(meth, *args)
13
+ end
14
+
15
+ def reset
16
+ @instance = nil
17
+ end
18
+
19
+ def instance
20
+ @instance ||= MagicPath::PathManager.new
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'magic_path/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'magic_path'
9
+ spec.version = MagicPath::VERSION
10
+ spec.authors = ['Donavan Stanley']
11
+ spec.email = ['donavan.stanley@gmail.com']
12
+
13
+ spec.summary = 'A gem for managing dynamic path names.'
14
+ spec.homepage = 'https://github.com/centric-automation/magic_path'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.13'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'pry', '~> 0.10'
28
+ spec.add_development_dependency 'pry-byebug', '~> 3.4'
29
+ spec.add_development_dependency 'rubocop'
30
+ spec.add_development_dependency 'nenv', '~> 0.3'
31
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magic_path
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Donavan Stanley
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: nenv
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.3'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.3'
111
+ description:
112
+ email:
113
+ - donavan.stanley@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/magic_path.rb
130
+ - lib/magic_path/dynamic_path.rb
131
+ - lib/magic_path/path_manager.rb
132
+ - lib/magic_path/version.rb
133
+ - magic_path.gemspec
134
+ homepage: https://github.com/centric-automation/magic_path
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.6.13
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: A gem for managing dynamic path names.
158
+ test_files: []