requirium 0.0.1 → 0.0.2

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/requirium.rb +66 -32
  3. metadata +4 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7dd79837d4d7b07b5b43a6e4bdb2b0d9282d3b98
4
- data.tar.gz: 13ddcef5f0246ccbfcef8ded2a482d979fc7aedb
3
+ metadata.gz: 593b6e06dc07239dd7cc9051101bc8f01335cef6
4
+ data.tar.gz: 2155e503347b54640fe77b3877db80b033ebb9d6
5
5
  SHA512:
6
- metadata.gz: 09e1b4dd928cdb25678789a5297e2294433a92a5d2a8e4f387cac139a6c9f8f5edca4e6039f24f505f62847f4a70960c885a392cbaf458724a8bf0602b0c6a4c
7
- data.tar.gz: 87c1368019c3383bf822180a25054d93bccc5308095b20ddd4565cb096c151b6449964d1a41d7f9555f89216f28f55b7723e054123e045e2dd89bbf5ad9ab5e3
6
+ metadata.gz: 7a74e063cbb3dd9d070e3041d53d3b3b372991ce0b4d42c82e089e9ca9761fdd2b9fa291a7603808facf3d68ef69dc72577bd88e5d10096b636238480bf95fba
7
+ data.tar.gz: ab262703c89fbc6e40ae32d288cad96a552354e6c6e22f19e9bcd52f3e059929eb29f2d0bb9c27123b0de1ee4beafbd271b4410327b4eb172f7688acf254457d
data/lib/requirium.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'thread'
2
2
  require 'pathname'
3
3
  require 'facets/string/snakecase'
4
+ require 'facets/module/home'
5
+ require 'facets/module/ancestor'
4
6
 
5
7
  #Automatically calls <code>Kernel#load</code> or <code>Kernel#require</code> on first use.
6
8
  #Example usage:
@@ -23,23 +25,29 @@ require 'facets/string/snakecase'
23
25
  # autorequire_relative X: nil, Y: ['y', 'y1', 'y2']
24
26
  # end
25
27
  module Requirium
26
- VERSION = '0.0.1'.freeze
28
+ VERSION = '0.0.2'.freeze
27
29
 
28
- EXTENSIONS = "{#{Gem.suffixes.join(',')}}".freeze
30
+ class Info
31
+ attr_accessor :sym
32
+ attr_accessor :mod
33
+ attr_reader :cond, :mutex
29
34
 
30
- class CondVar < ConditionVariable
31
- def mutex
32
- @mutex ||= Mutex.new
35
+ def initialize(mod, sym)
36
+ @mod, @sym, @cond, @mutex = mod, sym, ConditionVariable.new, Mutex.new
37
+ end
38
+
39
+ def internal_load
40
+ self.sym = mod.send(:internal_load, sym)
33
41
  end
34
42
 
35
43
  def ready!
36
44
  @ready = true
37
- mutex.synchronize { signal }
45
+ mutex.synchronize { cond.signal }
38
46
  nil
39
47
  end
40
48
 
41
49
  def wait_ready
42
- mutex.synchronize { until @ready; wait(mutex) end }
50
+ mutex.synchronize { until @ready; cond.wait(mutex) end }
43
51
  nil
44
52
  end
45
53
  end
@@ -47,15 +55,16 @@ module Requirium
47
55
  @queue = Queue.new
48
56
  @loader_thread = Thread.new do
49
57
  loop do
50
- mod, sym, cond = @queue.pop
58
+ info = @queue.pop
51
59
  begin
52
- mod.send(:internal_load, sym)
60
+ info.internal_load
53
61
  rescue ScriptError => e
54
62
  $stderr.puts e
55
63
  rescue => e
56
64
  $stderr.puts e
65
+ ensure
66
+ info.ready!
57
67
  end
58
- cond.ready!
59
68
  end
60
69
  end
61
70
 
@@ -84,21 +93,18 @@ module Requirium
84
93
  #end
85
94
 
86
95
  def const_missing(sym)
87
- if Thread.current == Requirium.loader_thread
88
- internal_load(sym)
89
- else
90
- cond = CondVar.new
91
- Requirium.queue.push [self, sym, cond]
92
- cond.wait_ready
93
- end
94
- const_defined?(sym) ? const_get(sym) : super
96
+ return internal_load(sym) if Thread.current == Requirium.loader_thread
97
+
98
+ Requirium.queue.push(info = Info.new(self, sym))
99
+ info.wait_ready
100
+ const_defined?(sym) ? info.sym : super
95
101
  end
96
102
 
97
103
  private
98
104
 
99
105
  def common_auto(method, args, dirname = nil)
100
106
  if args.length == 1 && args.first.is_a?(Hash)
101
- args.each { |sym, paths| add_load_item method, sym, paths, dirname }
107
+ args.first.each { |sym, paths| add_load_item method, sym, paths, dirname }
102
108
  return
103
109
  end
104
110
 
@@ -106,30 +112,58 @@ module Requirium
106
112
  add_load_item method, sym, paths, dirname
107
113
  end
108
114
 
109
- def add_load_item(method, sym, paths, dirname = nil)
115
+ def add_load_item(method, sym, paths, dirname)
110
116
  return if const_defined?(sym)
111
- paths = [*paths]
117
+
118
+ paths = clean_paths(method, sym, paths, dirname)
119
+
112
120
  #puts "auto#{method}#{dirname ? '_relative' : ''} #{sym.inspect}, #{paths.map(&:inspect).join(', ')}"
113
- load_list { |l| l[sym.to_s] = [method, paths, dirname] }
121
+ load_list { |l| l[sym.to_s] = [method, paths] }
114
122
  nil
115
123
  end
116
124
 
117
- def internal_load(sym)
118
- return if const_defined?(sym)
119
- method, paths, dirname = load_list { |l| l[sym.to_s] }
120
- return unless method
125
+ def clean_paths(method, sym, paths, dirname)
126
+ paths = [*paths]
121
127
  paths = [sym.to_s.snakecase] if paths.empty?
122
- paths = paths.map { |path| (Pathname(dirname) + path).to_s } if dirname
128
+
129
+ if dirname
130
+ dirname = Pathname(dirname)
131
+ paths.map! { |path| (dirname + path).to_s }
132
+ end
133
+
134
+ # append possible suffix
135
+ paths.map! { |p| Dir[*Gem.suffixes.map { |e| p + e }].first }.compact! if method == :load
136
+
137
+ paths
138
+ end
139
+
140
+ def internal_load(sym)
141
+ return const_get(sym) if const_defined?(sym)
142
+ str_sym = sym.to_s
143
+ has_sym, method, paths = load_list { |l| [l.has_key?(str_sym), *l[str_sym]] }
144
+
145
+ return parent.const_missing(sym) unless has_sym # go to parent
146
+
147
+ raise NoMethodError, "invalid method type: #{method.inspect}" unless [:load, :require].include?(method)
148
+
123
149
  paths.each do |filename|
124
150
  #puts "#{method} #{sym.inspect}, #{filename.inspect}"
125
- raise NoMethodError, "invalid method type: #{method.inspect}" unless [:load, :require].include?(method)
126
151
  send(method, filename)
127
152
  end
128
- load_list { |l| l.delete(sym.to_s) } if const_defined?(sym)
129
- nil
153
+
154
+ if const_defined?(sym)
155
+ load_list { |l| l.delete(sym.to_s) }
156
+ const_get(sym)
157
+ end
130
158
  end
131
159
 
132
160
  def load_list
133
- (@mutex ||= Mutex.new).synchronize { yield(@load_list ||= {}) }
161
+ @mutex ||= Mutex.new
162
+ @load_list ||= {}
163
+ @mutex.synchronize { yield(@load_list) }
164
+ end
165
+
166
+ def parent
167
+ name.split('::')[0..-2].inject(Object) { |mod, name| mod.const_get(name) }
134
168
  end
135
- end
169
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: requirium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - SilverPhoenix99
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-15 00:00:00.000000000 Z
11
+ date: 2014-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: facets
@@ -31,8 +31,8 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
- - README.md
35
34
  - lib/requirium.rb
35
+ - README.md
36
36
  homepage: https://github.com/SilverPhoenix99/requirium
37
37
  licenses:
38
38
  - MIT
@@ -61,9 +61,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  version: '0'
62
62
  requirements: []
63
63
  rubyforge_project:
64
- rubygems_version: 2.2.2
64
+ rubygems_version: 2.0.14
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: An autoload alternative
68
68
  test_files: []
69
- has_rdoc: