truck 0.8.4 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6550bf60079074a0644f6df1401efeff49ea3fa9
4
- data.tar.gz: 0ce9473dd821156f8ecc5f0b6dea687758ab235c
3
+ metadata.gz: 12ec59f7f4fdb83b6b260880588fd189f638d081
4
+ data.tar.gz: e04dc9fca539ebe977945703ec321e47cc64e41e
5
5
  SHA512:
6
- metadata.gz: 4ed94e3c7d982d972caa6197cc0c0efb4d25f2cb4737005bc589ae21b8a9435bd63ccc089d7332f4687e568906794a6b89220f9f9746b80feb9280386ebb79e4
7
- data.tar.gz: 69070a911e5bd34d27578be9b1a8bab52e9f52e8dae705be85f3ca455810fd2928d388da54f4c632b5999c29188dd53ceaeb8870508d6262069deec8c9b25786
6
+ metadata.gz: f49b2f62c5e9115194a9abc72054960c0ea13ddbdf97698b0831a846872dd13315fbb02dc3d6bb15f6cc8f0f1f41904fc4bd7c43c53c69ca8046e1ee0247a504
7
+ data.tar.gz: ee7ca5be0089784b9ce8e0537721572b1af8ecebe4e464f93f801e4d926f66dc2c50a174f92c35e2faf555102337e077e22993547104fe77e7c6e3b81f60861a
data/README.md CHANGED
@@ -52,7 +52,7 @@ end
52
52
 
53
53
  ```ruby
54
54
  # /path/to/context/root/bar.rb
55
- class Foo
55
+ class Bar
56
56
  def self.hello_world
57
57
  "hello, world!"
58
58
  end
@@ -71,6 +71,12 @@ This works with namespaced constant names, too:
71
71
  MyContext.resolve_const("Foo::Bar::Baz")
72
72
  ```
73
73
 
74
+ You can also directly reference them through their toplevel namespace:
75
+
76
+ ```ruby
77
+ MyContext::Foo::Bar::Baz
78
+ ```
79
+
74
80
  ### Other
75
81
 
76
82
  `MyContext` has some other interesting methods on it:
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rake', 'rake')
@@ -10,6 +10,8 @@ module Truck
10
10
  attr :contexts
11
11
  @contexts = {}
12
12
 
13
+ attr_accessor :debug_mode
14
+
13
15
  def define_context(name, **params)
14
16
  contexts[name] = Context.new(name, **params)
15
17
  end
@@ -2,10 +2,11 @@ module Truck
2
2
  using Truck::StringInflections
3
3
 
4
4
  class Autoloader
5
- attr :base_nibbles, :context, :from, :dir_paths
5
+ attr :base_nibbles, :context, :file, :from, :dir_paths
6
6
 
7
- def initialize(from)
7
+ def initialize(from, file)
8
8
  @from = from
9
+ @file = file
9
10
  @context, @base_nibbles = fetch_context_and_base_nibbles
10
11
  @dir_paths = [nil]
11
12
  end
@@ -13,7 +14,7 @@ module Truck
13
14
  def <<(const_name)
14
15
  raise_name_error!(const_name) unless context
15
16
  @dir_paths = each_possible_const(const_name).reduce [] do |new_paths, possible_const|
16
- resolved_const = context.resolve_const possible_const
17
+ resolved_const = context.resolve_const possible_const, skip: file
17
18
  throw :const, resolved_const if resolved_const
18
19
  new_paths << possible_const if possible_namespace?(possible_const)
19
20
  new_paths
@@ -94,8 +95,8 @@ module Truck
94
95
  set_current_autoloader(to: nil) if found_const or name_error
95
96
  end
96
97
 
97
- def handle!(const_name, from:)
98
- autoloader = current_autoloader || new(from)
98
+ def handle!(const_name, current_file: nil, from:)
99
+ autoloader = current_autoloader || new(from, current_file)
99
100
  autoloader << String(const_name)
100
101
  set_current_autoloader to: autoloader
101
102
  end
@@ -3,11 +3,12 @@ module Truck
3
3
 
4
4
  class Context
5
5
  class ConstResolver
6
- attr :current_path, :context, :expanded_const
6
+ attr :current_path, :context, :expanded_const, :skip_files
7
7
 
8
- def initialize(context:, expanded_const:)
8
+ def initialize(context:, expanded_const:, skip_files:)
9
9
  @context = context
10
10
  @expanded_const = expanded_const
11
+ @skip_files = skip_files
11
12
  end
12
13
 
13
14
  def resolve
@@ -26,6 +27,7 @@ module Truck
26
27
  each_autoload_path do
27
28
  base_path = current_path.join expanded_const.to_snake_case
28
29
  each_rb_file_from_base_path base_path do |rb_file|
30
+ next if skip_files.include? rb_file.to_s
29
31
  yield rb_file if File.exist?(rb_file)
30
32
  end
31
33
  end
@@ -40,7 +40,8 @@ module Truck
40
40
  end
41
41
 
42
42
  def load_file(rb_file)
43
- mod.module_eval File.read(rb_file), rb_file.to_s
43
+ ruby_code = File.read rb_file
44
+ mod.module_eval ruby_code, rb_file.to_s
44
45
  end
45
46
 
46
47
  def parent
@@ -48,8 +49,8 @@ module Truck
48
49
  Truck.contexts.fetch(@parent.to_sym).mod
49
50
  end
50
51
 
51
- def resolve_const(expanded_const)
52
- build_const_resolver(expanded_const).resolve
52
+ def resolve_const(expanded_const, skip: nil)
53
+ build_const_resolver(expanded_const, Array[skip]).resolve
53
54
  end
54
55
 
55
56
  def shutdown!
@@ -58,10 +59,11 @@ module Truck
58
59
 
59
60
  private
60
61
 
61
- def build_const_resolver(expanded_const)
62
+ def build_const_resolver(expanded_const, skip_files)
62
63
  ConstResolver.new(
63
64
  context: self,
64
65
  expanded_const: String(expanded_const).dup.freeze,
66
+ skip_files: skip_files,
65
67
  )
66
68
  end
67
69
 
@@ -1,7 +1,9 @@
1
1
  class Module
2
2
  def const_missing(const)
3
+ offending_file = caller_locations.fetch(0).path
4
+ $stderr.puts "Module#const_missing: const=#{const.inspect}, self=#{inspect}, file=#{offending_file}" if Truck.debug_mode
3
5
  catch :const do
4
- Truck::Autoloader.handle const, from: self
6
+ Truck::Autoloader.handle const, from: self, current_file: offending_file
5
7
  end
6
8
  rescue NameError => name_error
7
9
  if name_error.class == NameError
@@ -1,3 +1,3 @@
1
1
  module Truck
2
- VERSION = "0.8.4"
2
+ VERSION = "0.8.6"
3
3
  end
@@ -44,4 +44,12 @@ class AutoloadingTest < Minitest::Test
44
44
  def test_reference_constants_in_included_class
45
45
  assert_equal 'hello from B::BA', MyApp::IncludesFoo.message
46
46
  end
47
+
48
+ def test_reference_constants_in_external_class_that_includes_module
49
+ TOPLEVEL_BINDING.eval File.read "/ext.rb"
50
+
51
+ assert_equal 'hello from A', MyExtClass.new.message
52
+ ensure
53
+ Object.send :remove_const, :MyExtClass
54
+ end
47
55
  end
@@ -197,6 +197,34 @@ module B
197
197
  "hello from MyApp::B::BA"
198
198
  end
199
199
  end
200
+ end
201
+ FILE
202
+
203
+ File.write "/ext.rb", <<-FILE
204
+ class MyExtClass
205
+ include Foo
206
+
207
+ def message
208
+ A.message
209
+ end
210
+ end
211
+ FILE
212
+
213
+ Dir.mkdir "/extend"
214
+ Dir.mkdir "/extend/lib"
215
+ Dir.mkdir "/extend/app"
216
+
217
+ File.write "/extend/app/my_model.rb", <<-FILE
218
+ module MyModel
219
+ extend MyMixin
220
+ end
221
+ FILE
222
+
223
+ File.write "/extend/lib/my_mixin.rb", <<-FILE
224
+ module MyMixin
225
+ def message
226
+ 'hello from MyMixin'
227
+ end
200
228
  end
201
229
  FILE
202
230
  end
@@ -4,6 +4,7 @@ module TestsAutoloading
4
4
  @bar = Truck.define_context :Bar, root: '/bar', parent: :Foo
5
5
  @my_app = Truck.define_context :MyApp, root: "/my_app", autoload_paths: %w(lib)
6
6
  @multi = Truck.define_context :MultipleAutoloadPaths, root: "/", autoload_paths: %w(foo bar)
7
+ @extend = Truck.define_context :Extend, root: "/extend", autoload_paths: %w(app lib)
7
8
  Truck.boot!
8
9
  assert_nil Truck::Autoloader.current_autoloader
9
10
  end
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.executables = []
18
18
  spec.test_files = spec.files.grep(%r{^test/})
19
19
  spec.require_paths = ["lib"]
20
20
 
@@ -22,5 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "fakefs", "~> 0.5"
23
23
  spec.add_development_dependency "minitest", "~> 5.0"
24
24
  spec.add_development_dependency "minitest-reporters", "~> 1.0"
25
+ spec.add_development_dependency "pry"
25
26
  spec.add_development_dependency "rake", "~> 10.0"
26
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
4
+ version: 0.8.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - ntl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-06 00:00:00.000000000 Z
11
+ date: 2014-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rake
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -92,6 +106,7 @@ files:
92
106
  - LICENSE.txt
93
107
  - README.md
94
108
  - Rakefile
109
+ - bin/rake
95
110
  - lib/truck.rb
96
111
  - lib/truck/autoloader.rb
97
112
  - lib/truck/const_resolver.rb