require_module 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8de7ec307473e3260046abee12619a9b4239b406dffd7cb7680c5c44852e4c4f
4
+ data.tar.gz: 397cc6e76d6463b7395ae604acaebaf6e21c9ea8fdcff31a691fce13ac9a0892
5
+ SHA512:
6
+ metadata.gz: '019281db025c320f5c98a143549ba6c034276ab8a94e8166d597128620821056305e0c9474f64a42459f6718fbeebfee541d13657a6dae5ef322befb70697a0d'
7
+ data.tar.gz: 328e26a5698c513c00f5d3fca278f7c53dca358f1697f2d3e738dc00aa06aaa2b95a3945f19a979794c44d3c5f70c9edbffd163cda35fd34e815bdd76459de56
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ pkg
2
+ coverage/
3
+ .ruby-version
4
+ .ruby-gemset
5
+ .rspec
6
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ rvm:
2
+ - 2.4.0
3
+ - 2.3.3
4
+ - 2.2.6
5
+ - jruby-19mode
6
+ - rbx-3
7
+ - ruby-head
8
+ notifications:
9
+ recipients:
10
+ - melgaardbjorn@gmail.com
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: ruby-head
14
+ - rvm: jruby-19mode
15
+ - rvm: rbx-3
16
+
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem 'coveralls', require: false
5
+ gem 'pry', require: false
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jarmo Pertman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # require_module
2
+
3
+ [![Build Status](https://travis-ci.org/BjornMelgaard/require_module.svg?branch=master)](https://travis-ci.org/BjornMelgaard/require_module)
4
+
5
+ Evaluates file content inside Module.new, with cache
6
+
7
+ # API
8
+
9
+ ## require_module(fullpath)
10
+
11
+ Evaluates file content inside Module.new and returns new module
12
+
13
+ ```rb
14
+ require_module('/home/user/rubyapp/lib', cache: false) # returns #<Module:0000012312>
15
+ require_module('/home/user/rubyapp/lib') # returns :HomeUserRubyappLib
16
+ ```
17
+
18
+ ## require_module_relative(relative_path)
19
+
20
+ Similar to "require_module", but path is relative to file, where function is executed
21
+
22
+ Check `lib/require_module.rb` for more
23
+
24
+
25
+ # Example
26
+ If you don't want you modules to intersect, they should have unique name.
27
+ I was tired of coming up with new names, so created this gem.
28
+
29
+ Instead of
30
+
31
+ ```rb
32
+ # lib.rb
33
+ module GloballyUniqueName
34
+ module_function
35
+
36
+ def somefn
37
+ 'Ima helper'
38
+ end
39
+ end
40
+ ```
41
+
42
+ ```rb
43
+ # consumer.rb
44
+ require_relative './lib'
45
+
46
+ module Consumer
47
+ include GloballyUniqueName
48
+
49
+ def foo
50
+ somefn == 'Ima helper'
51
+ end
52
+ end
53
+ ```
54
+
55
+ You can write this
56
+
57
+ ```rb
58
+ # lib.rb
59
+ module_function
60
+
61
+ def somefn
62
+ 'Ima helper'
63
+ end
64
+ ```
65
+
66
+ ```rb
67
+ # consumer.rb
68
+ module Consumer
69
+ include require_module_relative('./lib')
70
+
71
+ def foo
72
+ somefn == 'Ima helper'
73
+ end
74
+ end
75
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
6
+ task build: :spec
@@ -0,0 +1,87 @@
1
+ module RequireModule
2
+ # Evaluates file content inside Module.new and returns new module
3
+ #
4
+ # ==== Attributes
5
+ #
6
+ # * +fullpath+ - Absolute path to .rb file, .rb extension is optional
7
+ #
8
+ # ==== Options
9
+ #
10
+ # * +:cache+ - Default - true.
11
+ # If false - creates new Module object with unique name.
12
+ # If true - creates new or returns already created Module with name, based on path to file
13
+ #
14
+ # ==== Examples
15
+ #
16
+ # require_module('/home/user/rubyapp/lib', cache: false) # returns #<Module:0000012312>
17
+ # require_module('/home/user/rubyapp/lib') # returns :HomeUserRubyappLib
18
+ def require_module(fullpath, cache: true)
19
+ with_ext = add_ext(fullpath)
20
+ without_ext = rem_ext(fullpath)
21
+
22
+ if cache
23
+ constant_name =
24
+ without_ext
25
+ .split(/[^a-zA-Z]/)
26
+ .map(&:capitalize)
27
+ .join
28
+ .to_sym
29
+
30
+ begin
31
+ Object.const_get(constant_name)
32
+ rescue NameError
33
+ mod = gen_mod(with_ext)
34
+ Object.const_set(constant_name, mod)
35
+ mod
36
+ end
37
+ else
38
+ gen_mod(with_ext)
39
+ end
40
+ end
41
+
42
+ # Similar to "require_module", but path is relative to file, where function is executed
43
+ #
44
+ # ==== Attributes
45
+ #
46
+ # * +path+ - Relative path to .rb file, .rb extension is optional
47
+ #
48
+ # ==== Options
49
+ #
50
+ # * +:cache+ - Default - true.
51
+ # If false - creates new Module object with unique name.
52
+ # If true - creates new or returns already created Module with name, based on path to file
53
+ def require_module_relative(path, **options)
54
+ caller_filepath = caller_locations(1..1).first.absolute_path
55
+ caller_dir = File.dirname(caller_filepath)
56
+
57
+ fullpath = File.expand_path(path, caller_dir)
58
+
59
+ require_module(fullpath, **options)
60
+ end
61
+
62
+ private
63
+
64
+ def gen_mod(fullpath)
65
+ content = read_rb(fullpath)
66
+
67
+ Module.new.tap do |mod|
68
+ mod.module_eval(content, fullpath, 1)
69
+ end
70
+ end
71
+
72
+ def read_rb(fullpath)
73
+ File.read(fullpath)
74
+ rescue Errno::ENOENT
75
+ raise LoadError, "path doesn't exist at #{fullpath}"
76
+ end
77
+
78
+ def add_ext(path)
79
+ Pathname.new(path).sub_ext('.rb').to_s
80
+ end
81
+
82
+ def rem_ext(path)
83
+ path.sub(/#{File.extname(path)}$/, '')
84
+ end
85
+ end
86
+
87
+ include RequireModule
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'require_module'
3
+ s.version = '0.0.1'
4
+ s.authors = ['Sergey Homa']
5
+ s.email = 'melgaardbjorn@gmail.com'
6
+ s.summary = 'Evaluates file content inside Module.new, with cache'
7
+
8
+ s.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
9
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
10
+ s.require_paths = ['lib']
11
+
12
+ s.homepage = 'https://github.com/BjornMelgaard/require_module'
13
+ s.license = 'MIT'
14
+
15
+ s.has_rdoc = true
16
+ s.rdoc_options = %w[--title require_all --main README.md --line-numbers]
17
+ s.extra_rdoc_files = ['LICENSE', 'README.md']
18
+ end
@@ -0,0 +1,5 @@
1
+ module_function
2
+
3
+ def some_fn
4
+ 'some_val'
5
+ end
@@ -0,0 +1,57 @@
1
+ require_relative 'spec_helper'
2
+
3
+ def unload_all_matching(regex)
4
+ Object.constants.grep(regex).each do |const|
5
+ Object.send(:remove_const, const)
6
+ end
7
+ end
8
+
9
+ describe 'require_module_relative' do
10
+ before do
11
+ unload_all_matching(/FixturesSomeLib/)
12
+ end
13
+
14
+ it 'without ext' do
15
+ mod = require_module_relative './fixtures/some_lib'
16
+
17
+ expect(mod.inspect).to end_with 'FixturesSomeLib'
18
+ expect(mod.some_fn).to eq 'some_val'
19
+ end
20
+
21
+ it 'with ext' do
22
+ mod = require_module_relative './fixtures/some_lib.rb'
23
+
24
+ expect(mod.inspect).to end_with 'FixturesSomeLib'
25
+ expect(mod.some_fn).to eq 'some_val'
26
+ end
27
+
28
+ it "raises NameError if dependencies can't be resolved" do
29
+ expect do
30
+ require_module_relative './unresolveable'
31
+ end.to raise_error(LoadError)
32
+ end
33
+
34
+ it 'test cache' do
35
+ mod = require_module_relative './fixtures/some_lib'
36
+ mod2 = require_module_relative './fixtures/some_lib'
37
+
38
+ expect(mod.inspect).to end_with 'FixturesSomeLib'
39
+ expect(mod2.inspect).to end_with 'FixturesSomeLib'
40
+
41
+ expect(mod <=> mod2).to eq 0
42
+ end
43
+
44
+ it 'test without cache' do
45
+ mod = require_module_relative('./fixtures/some_lib.rb', cache: false)
46
+
47
+ expect(mod.inspect).to start_with '#<Module:'
48
+ expect(mod.some_fn).to eq 'some_val'
49
+ end
50
+
51
+ it 'test without cache and ext' do
52
+ mod = require_module_relative('./fixtures/some_lib', cache: false)
53
+
54
+ expect(mod.inspect).to start_with '#<Module:'
55
+ expect(mod.some_fn).to eq 'some_val'
56
+ end
57
+ end
@@ -0,0 +1,11 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
5
+ SimpleCov.start
6
+
7
+ require_relative '../lib/require_module'
8
+
9
+ RSpec.configure do |config|
10
+ config.color = true
11
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: require_module
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Homa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: melgaardbjorn@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files:
18
+ - LICENSE
19
+ - README.md
20
+ files:
21
+ - ".gitignore"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - lib/require_module.rb
28
+ - require_module.gemspec
29
+ - spec/fixtures/some_lib.rb
30
+ - spec/require_module_spec.rb
31
+ - spec/spec_helper.rb
32
+ homepage: https://github.com/BjornMelgaard/require_module
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
36
+ post_install_message:
37
+ rdoc_options:
38
+ - "--title"
39
+ - require_all
40
+ - "--main"
41
+ - README.md
42
+ - "--line-numbers"
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.7.4
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Evaluates file content inside Module.new, with cache
61
+ test_files:
62
+ - spec/fixtures/some_lib.rb
63
+ - spec/require_module_spec.rb
64
+ - spec/spec_helper.rb