truck 0.8.6 → 0.8.7
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 +4 -4
- data/lib/truck.rb +19 -2
- data/lib/truck/autoloader.rb +12 -9
- data/lib/truck/const_resolver.rb +7 -8
- data/lib/truck/context.rb +4 -8
- data/lib/truck/core_ext.rb +2 -2
- data/lib/truck/string_inflections.rb +23 -23
- data/lib/truck/version.rb +1 -1
- data/test/test_helper.rb +1 -3
- data/test/unit/autoloader_test.rb +10 -10
- data/test/unit/define_context_test.rb +11 -0
- data/truck.gemspec +1 -1
- metadata +9 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bd4cb8f4aee7a5e8a38bfb6c3118082430a4274a
|
|
4
|
+
data.tar.gz: d19a9524b1e2c96c9a975b61c685a0156b3f12fb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8ab2b86d092a8ba57a9f2dbcf26358273106dc426c8dc090bfe3be3c51063155c76692f93fa36467c9c63b0a348e6be5a846fc9cbe744095ae61b012354d6016
|
|
7
|
+
data.tar.gz: 5439b3c696fc640f65714d92e54ad45240bdd9b48ffbfbf118ac657fb56536f4bc08c75995e9f93eb488df21c8eefc3c32b37167c6634eb24a0c7521f7ac6471
|
data/lib/truck.rb
CHANGED
|
@@ -12,8 +12,14 @@ module Truck
|
|
|
12
12
|
|
|
13
13
|
attr_accessor :debug_mode
|
|
14
14
|
|
|
15
|
-
def define_context(name,
|
|
16
|
-
|
|
15
|
+
def define_context(name, params = {})
|
|
16
|
+
root, parent, autoload_paths = extract_args!(
|
|
17
|
+
params,
|
|
18
|
+
:root,
|
|
19
|
+
parent: nil,
|
|
20
|
+
autoload_paths: ['.'],
|
|
21
|
+
)
|
|
22
|
+
contexts[name] = Context.new(name, root, parent, autoload_paths)
|
|
17
23
|
end
|
|
18
24
|
|
|
19
25
|
def boot!
|
|
@@ -38,6 +44,17 @@ module Truck
|
|
|
38
44
|
return to_enum(:each_booted_context) unless block_given?
|
|
39
45
|
contexts.each_value.select(&:booted?).each(&block)
|
|
40
46
|
end
|
|
47
|
+
|
|
48
|
+
def extract_args!(hsh, *mandatory)
|
|
49
|
+
optional = mandatory.pop if mandatory.last.is_a? Hash
|
|
50
|
+
args = mandatory.map do |key|
|
|
51
|
+
hsh.fetch key do raise ArgumentError, "missing keyword: #{key}" end
|
|
52
|
+
end
|
|
53
|
+
optional.each do |key, default|
|
|
54
|
+
args.<< hsh.fetch key, default
|
|
55
|
+
end
|
|
56
|
+
args
|
|
57
|
+
end
|
|
41
58
|
end
|
|
42
59
|
|
|
43
60
|
# Load this last so that when truck itself has unresolvable constants, we throw
|
data/lib/truck/autoloader.rb
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
module Truck
|
|
2
|
-
using Truck::StringInflections
|
|
3
|
-
|
|
4
2
|
class Autoloader
|
|
5
3
|
attr :base_nibbles, :context, :file, :from, :dir_paths
|
|
6
4
|
|
|
@@ -14,7 +12,7 @@ module Truck
|
|
|
14
12
|
def <<(const_name)
|
|
15
13
|
raise_name_error!(const_name) unless context
|
|
16
14
|
@dir_paths = each_possible_const(const_name).reduce [] do |new_paths, possible_const|
|
|
17
|
-
resolved_const = context.resolve_const possible_const,
|
|
15
|
+
resolved_const = context.resolve_const possible_const, file
|
|
18
16
|
throw :const, resolved_const if resolved_const
|
|
19
17
|
new_paths << possible_const if possible_namespace?(possible_const)
|
|
20
18
|
new_paths
|
|
@@ -31,7 +29,8 @@ module Truck
|
|
|
31
29
|
end
|
|
32
30
|
|
|
33
31
|
def possible_namespace?(possible_const)
|
|
34
|
-
|
|
32
|
+
snaked = StringInflections.to_snake_case possible_const
|
|
33
|
+
context.root.join(snaked).directory?
|
|
35
34
|
end
|
|
36
35
|
|
|
37
36
|
def constify(*nibbles)
|
|
@@ -74,10 +73,14 @@ module Truck
|
|
|
74
73
|
autoloaders[current_thread_id]
|
|
75
74
|
end
|
|
76
75
|
|
|
77
|
-
def set_current_autoloader(to
|
|
76
|
+
def set_current_autoloader(to)
|
|
78
77
|
autoloaders[current_thread_id] = to
|
|
79
78
|
end
|
|
80
79
|
|
|
80
|
+
def unset_current_autoloader
|
|
81
|
+
set_current_autoloader nil
|
|
82
|
+
end
|
|
83
|
+
|
|
81
84
|
def current_thread_id
|
|
82
85
|
Thread.current.object_id
|
|
83
86
|
end
|
|
@@ -92,13 +95,13 @@ module Truck
|
|
|
92
95
|
throw :const, found_const
|
|
93
96
|
rescue NameError => name_error; raise name_error
|
|
94
97
|
ensure
|
|
95
|
-
|
|
98
|
+
unset_current_autoloader if found_const or name_error
|
|
96
99
|
end
|
|
97
100
|
|
|
98
|
-
def handle!(const_name, current_file
|
|
101
|
+
def handle!(const_name, from, current_file = nil)
|
|
99
102
|
autoloader = current_autoloader || new(from, current_file)
|
|
100
103
|
autoloader << String(const_name)
|
|
101
|
-
set_current_autoloader
|
|
104
|
+
set_current_autoloader autoloader
|
|
102
105
|
end
|
|
103
106
|
end
|
|
104
107
|
extend HandleConstMissing
|
|
@@ -109,7 +112,7 @@ module Truck
|
|
|
109
112
|
def method_missing(*)
|
|
110
113
|
Autoloader.current_autoloader.raise_name_error!
|
|
111
114
|
ensure
|
|
112
|
-
Autoloader.
|
|
115
|
+
Autoloader.unset_current_autoloader
|
|
113
116
|
end
|
|
114
117
|
end
|
|
115
118
|
end
|
data/lib/truck/const_resolver.rb
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
module Truck
|
|
2
|
-
using StringInflections
|
|
3
|
-
|
|
4
2
|
class Context
|
|
5
3
|
class ConstResolver
|
|
6
4
|
attr :current_path, :context, :expanded_const, :skip_files
|
|
7
5
|
|
|
8
|
-
def initialize(context
|
|
6
|
+
def initialize(context, expanded_const, skip_files)
|
|
9
7
|
@context = context
|
|
10
8
|
@expanded_const = expanded_const
|
|
11
9
|
@skip_files = skip_files
|
|
@@ -25,7 +23,8 @@ module Truck
|
|
|
25
23
|
|
|
26
24
|
def each_possible_rb_file
|
|
27
25
|
each_autoload_path do
|
|
28
|
-
|
|
26
|
+
snaked = StringInflections.to_snake_case expanded_const
|
|
27
|
+
base_path = current_path.join snaked
|
|
29
28
|
each_rb_file_from_base_path base_path do |rb_file|
|
|
30
29
|
next if skip_files.include? rb_file.to_s
|
|
31
30
|
yield rb_file if File.exist?(rb_file)
|
|
@@ -72,14 +71,14 @@ module Truck
|
|
|
72
71
|
expected_const = expected_const_defined_in_rb_file rb_file
|
|
73
72
|
walk_const_parts(expected_const).reduce context.mod do |mod, const_part|
|
|
74
73
|
mod.const_defined?(const_part) or
|
|
75
|
-
raise AutoloadError.new(
|
|
74
|
+
raise AutoloadError.new(expected_const, rb_file)
|
|
76
75
|
mod.const_get const_part
|
|
77
76
|
end
|
|
78
77
|
end
|
|
79
78
|
|
|
80
|
-
def expected_const_defined_in_rb_file(rb_file
|
|
81
|
-
rel_path = rb_file.sub_ext('').relative_path_from(
|
|
82
|
-
matcher = %r{\A(#{
|
|
79
|
+
def expected_const_defined_in_rb_file(rb_file)
|
|
80
|
+
rel_path = rb_file.sub_ext('').relative_path_from(current_path).to_s
|
|
81
|
+
matcher = %r{\A(#{StringInflections.to_camel_case rel_path})}i
|
|
83
82
|
expanded_const.match(matcher).captures.fetch 0
|
|
84
83
|
end
|
|
85
84
|
end
|
data/lib/truck/context.rb
CHANGED
|
@@ -2,7 +2,7 @@ module Truck
|
|
|
2
2
|
class Context
|
|
3
3
|
attr :autoload_paths, :name, :root
|
|
4
4
|
|
|
5
|
-
def initialize(name, parent
|
|
5
|
+
def initialize(name, root, parent, autoload_paths)
|
|
6
6
|
@name = name
|
|
7
7
|
@root = Pathname(root)
|
|
8
8
|
@parent = parent
|
|
@@ -49,7 +49,7 @@ module Truck
|
|
|
49
49
|
Truck.contexts.fetch(@parent.to_sym).mod
|
|
50
50
|
end
|
|
51
51
|
|
|
52
|
-
def resolve_const(expanded_const, skip
|
|
52
|
+
def resolve_const(expanded_const, skip = nil)
|
|
53
53
|
build_const_resolver(expanded_const, Array[skip]).resolve
|
|
54
54
|
end
|
|
55
55
|
|
|
@@ -60,11 +60,7 @@ module Truck
|
|
|
60
60
|
private
|
|
61
61
|
|
|
62
62
|
def build_const_resolver(expanded_const, skip_files)
|
|
63
|
-
ConstResolver.new(
|
|
64
|
-
context: self,
|
|
65
|
-
expanded_const: String(expanded_const).dup.freeze,
|
|
66
|
-
skip_files: skip_files,
|
|
67
|
-
)
|
|
63
|
+
ConstResolver.new self, String(expanded_const).dup.freeze, skip_files
|
|
68
64
|
end
|
|
69
65
|
|
|
70
66
|
def build_mod
|
|
@@ -84,7 +80,7 @@ module Truck
|
|
|
84
80
|
class AutoloadError < NameError
|
|
85
81
|
attr :const, :rb_file
|
|
86
82
|
|
|
87
|
-
def initialize(const
|
|
83
|
+
def initialize(const, rb_file)
|
|
88
84
|
@const = const
|
|
89
85
|
@rb_file = rb_file
|
|
90
86
|
end
|
data/lib/truck/core_ext.rb
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
class Module
|
|
2
2
|
def const_missing(const)
|
|
3
|
-
offending_file =
|
|
3
|
+
offending_file = caller[0]
|
|
4
4
|
$stderr.puts "Module#const_missing: const=#{const.inspect}, self=#{inspect}, file=#{offending_file}" if Truck.debug_mode
|
|
5
5
|
catch :const do
|
|
6
|
-
Truck::Autoloader.handle const,
|
|
6
|
+
Truck::Autoloader.handle const, self, offending_file
|
|
7
7
|
end
|
|
8
8
|
rescue NameError => name_error
|
|
9
9
|
if name_error.class == NameError
|
|
@@ -1,30 +1,30 @@
|
|
|
1
1
|
module Truck
|
|
2
2
|
module StringInflections
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
extend self
|
|
4
|
+
|
|
5
|
+
def to_camel_case(str)
|
|
6
|
+
str = "_#{str}"
|
|
7
|
+
str.gsub!(%r{_[a-z]}) { |snake| snake.slice(1).upcase }
|
|
8
|
+
str.gsub!('/', '::')
|
|
9
|
+
str
|
|
10
|
+
end
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
end
|
|
25
|
-
str.downcase!
|
|
26
|
-
str
|
|
12
|
+
def to_snake_case(str)
|
|
13
|
+
str = str.gsub '::', '/'
|
|
14
|
+
# Convert FOOBar => FooBar
|
|
15
|
+
str.gsub! %r{[[:upper:]]{2,}} do |uppercase|
|
|
16
|
+
bit = uppercase[0]
|
|
17
|
+
bit << uppercase[1..-1].downcase
|
|
18
|
+
bit
|
|
19
|
+
end
|
|
20
|
+
# Convert FooBar => foo_bar
|
|
21
|
+
str.gsub! %r{[[:lower:]][[:upper:]]+[[:lower:]]} do |camel|
|
|
22
|
+
bit = camel[0]
|
|
23
|
+
bit << '_'
|
|
24
|
+
bit << camel[1..-1].downcase
|
|
27
25
|
end
|
|
26
|
+
str.downcase!
|
|
27
|
+
str
|
|
28
28
|
end
|
|
29
29
|
end
|
|
30
30
|
end
|
data/lib/truck/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
|
@@ -2,15 +2,13 @@ require "bundler"
|
|
|
2
2
|
Bundler.setup
|
|
3
3
|
|
|
4
4
|
require "minitest/autorun"
|
|
5
|
-
require "minitest/
|
|
5
|
+
require "minitest/red_green"
|
|
6
6
|
|
|
7
7
|
require "fakefs/safe"
|
|
8
8
|
require "ostruct"
|
|
9
9
|
require "pathname"
|
|
10
10
|
require "stringio"
|
|
11
11
|
|
|
12
|
-
Minitest::Reporters.use! Minitest::Reporters::DefaultReporter.new
|
|
13
|
-
|
|
14
12
|
require_relative "../lib/truck"
|
|
15
13
|
|
|
16
14
|
$LOAD_PATH << "test/support"
|
|
@@ -6,41 +6,41 @@ class AutoloaderTest < Minitest::Test
|
|
|
6
6
|
|
|
7
7
|
def test_throws_constant_when_found
|
|
8
8
|
const = assert_catches :const do
|
|
9
|
-
Truck::Autoloader.handle :A,
|
|
9
|
+
Truck::Autoloader.handle :A, Foo
|
|
10
10
|
end
|
|
11
11
|
assert_equal 'Foo::A', const.name
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
def test_raises_error_when_const_not_found
|
|
15
15
|
exception = assert_raises NameError do
|
|
16
|
-
Truck::Autoloader.handle :Abracadabra,
|
|
16
|
+
Truck::Autoloader.handle :Abracadabra, Foo
|
|
17
17
|
end
|
|
18
18
|
assert_equal 'uninitialized constant Abracadabra (in Foo)', exception.message
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def test_simple_implicit_namespace_case
|
|
22
|
-
Truck::Autoloader.handle :B,
|
|
22
|
+
Truck::Autoloader.handle :B, Foo
|
|
23
23
|
const = assert_catches :const do
|
|
24
|
-
Truck::Autoloader.handle :BA,
|
|
24
|
+
Truck::Autoloader.handle :BA, Foo
|
|
25
25
|
end
|
|
26
26
|
assert_equal 'Foo::B::BA', const.name
|
|
27
27
|
assert_nil Truck::Autoloader.current_autoloader
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
def test_cleanup_after_implicit_namespace
|
|
31
|
-
Truck::Autoloader.handle :B,
|
|
31
|
+
Truck::Autoloader.handle :B, Foo
|
|
32
32
|
assert_raises NameError do
|
|
33
|
-
Truck::Autoloader.handle :Abracadabra,
|
|
33
|
+
Truck::Autoloader.handle :Abracadabra, Foo
|
|
34
34
|
end
|
|
35
35
|
assert_nil Truck::Autoloader.current_autoloader
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
def test_deeply_nested_module_with_implicit_namespaces
|
|
39
|
-
|
|
40
|
-
Truck::Autoloader.handle implicit_namespace,
|
|
39
|
+
[:C, :CA, :CAA].each do |implicit_namespace|
|
|
40
|
+
Truck::Autoloader.handle implicit_namespace, Foo
|
|
41
41
|
end
|
|
42
42
|
const = assert_catches :const do
|
|
43
|
-
Truck::Autoloader.handle :CAAA,
|
|
43
|
+
Truck::Autoloader.handle :CAAA, Foo
|
|
44
44
|
end
|
|
45
45
|
assert_equal 'Foo::C::CA::CAA::CAAA', const.name
|
|
46
46
|
end
|
|
@@ -49,7 +49,7 @@ class AutoloaderTest < Minitest::Test
|
|
|
49
49
|
@foo.resolve_const 'A::AB::ABA'
|
|
50
50
|
|
|
51
51
|
const = assert_catches :const do
|
|
52
|
-
Truck::Autoloader.handle :ABB,
|
|
52
|
+
Truck::Autoloader.handle :ABB, Foo::A::AB::ABA
|
|
53
53
|
end
|
|
54
54
|
assert_equal 'Foo::A::AB::ABB', const.name
|
|
55
55
|
end
|
data/truck.gemspec
CHANGED
|
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.6"
|
|
22
22
|
spec.add_development_dependency "fakefs", "~> 0.5"
|
|
23
23
|
spec.add_development_dependency "minitest", "~> 5.0"
|
|
24
|
-
spec.add_development_dependency "minitest-
|
|
24
|
+
spec.add_development_dependency "minitest-red_green"
|
|
25
25
|
spec.add_development_dependency "pry"
|
|
26
26
|
spec.add_development_dependency "rake", "~> 10.0"
|
|
27
27
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: truck
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.8.
|
|
4
|
+
version: 0.8.7
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- ntl
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2014-
|
|
11
|
+
date: 2014-11-01 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -53,19 +53,19 @@ dependencies:
|
|
|
53
53
|
- !ruby/object:Gem::Version
|
|
54
54
|
version: '5.0'
|
|
55
55
|
- !ruby/object:Gem::Dependency
|
|
56
|
-
name: minitest-
|
|
56
|
+
name: minitest-red_green
|
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
|
58
58
|
requirements:
|
|
59
|
-
- - "
|
|
59
|
+
- - ">="
|
|
60
60
|
- !ruby/object:Gem::Version
|
|
61
|
-
version: '
|
|
61
|
+
version: '0'
|
|
62
62
|
type: :development
|
|
63
63
|
prerelease: false
|
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
65
|
requirements:
|
|
66
|
-
- - "
|
|
66
|
+
- - ">="
|
|
67
67
|
- !ruby/object:Gem::Version
|
|
68
|
-
version: '
|
|
68
|
+
version: '0'
|
|
69
69
|
- !ruby/object:Gem::Dependency
|
|
70
70
|
name: pry
|
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -120,6 +120,7 @@ files:
|
|
|
120
120
|
- test/test_helper.rb
|
|
121
121
|
- test/unit/autoloader_test.rb
|
|
122
122
|
- test/unit/context_test.rb
|
|
123
|
+
- test/unit/define_context_test.rb
|
|
123
124
|
- truck.gemspec
|
|
124
125
|
homepage: https://github.com/ntl/truck
|
|
125
126
|
licenses:
|
|
@@ -154,3 +155,4 @@ test_files:
|
|
|
154
155
|
- test/test_helper.rb
|
|
155
156
|
- test/unit/autoloader_test.rb
|
|
156
157
|
- test/unit/context_test.rb
|
|
158
|
+
- test/unit/define_context_test.rb
|