Module: Hyperloop
- Defined in:
- lib/hyperloop/rail_tie.rb,
lib/hyperloop/requires.rb,
lib/hyperloop/on_client.rb,
lib/hyperloop/client_stubs.rb,
lib/hyperloop/client_readers.rb,
lib/hyperloop/config_settings.rb
Defined Under Namespace
Classes: Railtie
Class Method Summary collapse
- .client_guard(client_only) ⇒ Object
- .client_reader(*args) ⇒ Object
- .client_reader_hash ⇒ Object
- .client_readers ⇒ Object
- .configuration {|_self| ... } ⇒ Object
- .define_setting(name, default = nil, &block) ⇒ Object
- .generate_directive(directive, to, file, client_only = false) ⇒ Object
- .generate_require_tree(path, client_only) ⇒ Object
- .generate_requires(mode, file) ⇒ Object
- .initialized_blocks ⇒ Object
- .on_client? ⇒ Boolean
- .on_config_initialized(&block) ⇒ Object
- .on_config_reset(&block) ⇒ Object
- .require(value, gem: nil, override_with: nil, client_only: nil, tree: nil) ⇒ Object
- .require_gem(value, override_with: nil, client_only: nil) ⇒ Object
- .require_tree(value, override_with: nil, client_only: nil) ⇒ Object
- .requires ⇒ Object
- .reset_blocks ⇒ Object
Class Method Details
.client_guard(client_only) ⇒ Object
71 72 73 |
# File 'lib/hyperloop/requires.rb', line 71 def client_guard(client_only) "# CLIENT ONLY" if client_only end |
.client_reader(*args) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/hyperloop/client_readers.rb', line 12 def client_reader(*args) # configuration.client_reader[:foo] = 12 initialize your own client value # configuration.client_reader :foo, :bar make previous setting readable on client client_readers += [*args] client_reader_hash end |
.client_reader_hash ⇒ Object
8 9 10 |
# File 'lib/hyperloop/client_readers.rb', line 8 def client_reader_hash @client_readers_hash ||= {} end |
.client_readers ⇒ Object
4 5 6 |
# File 'lib/hyperloop/client_readers.rb', line 4 def client_readers @client_readers ||= [] end |
.configuration {|_self| ... } ⇒ Object
12 13 14 15 16 |
# File 'lib/hyperloop/config_settings.rb', line 12 def configuration reset_blocks.each(&:call) yield self initialized_blocks.each(&:call) end |
.define_setting(name, default = nil, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/hyperloop/config_settings.rb', line 18 def define_setting(name, default = nil, &block) class_variable_set("@@#{name}", default) define_class_method "#{name}=" do |value| class_variable_set("@@#{name}", value) block.call value if block value end define_class_method name do class_variable_get("@@#{name}") end end |
.generate_directive(directive, to, file, client_only = false) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/hyperloop/requires.rb', line 48 def generate_directive(directive, to, file, client_only = false) gem_path = File.('../', file).split('/') comp_path = Rails.root.join('app', 'hyperloop', to).to_s.split('/') while comp_path.first == gem_path.first do gem_path.shift comp_path.shift end r = "#{directive} '#{(['.'] + ['..'] * gem_path.length + comp_path).join('/')}' #{client_guard(client_only)}" puts r "puts \"#{r}\"; #{r}" end |
.generate_require_tree(path, client_only) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/hyperloop/requires.rb', line 60 def generate_require_tree(path, client_only) base_name = Rails.root.join('app', path).to_s+'/' Dir.glob(Rails.root.join('app', path, '**', '*')).collect do |fname| if ['.js', '.rb', '.erb'].include? File.extname(fname) r = "require '#{fname.gsub(base_name, '')}' #{client_guard(client_only)}" puts r "puts \"#{r}\"; #{r}" end end.compact.join("\n") end |
.generate_requires(mode, file) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/hyperloop/requires.rb', line 31 def generate_requires(mode, file) puts "***** generating requires for #{mode} - #{file}" requires.collect do |name, value, client_only, kind| next unless value next if client_only && mode != :client if kind == :tree generate_require_tree(value, client_only) elsif kind == :gem r = "require '#{value}' #{client_guard(client_only)}" puts r "puts \"#{r}\"; #{r}" else generate_directive(:require, value, file, client_only) end end.compact.join("\n") end |
.initialized_blocks ⇒ Object
4 5 6 |
# File 'lib/hyperloop/config_settings.rb', line 4 def initialized_blocks @initialized_blocks ||= [] end |
.on_client? ⇒ Boolean
2 3 4 |
# File 'lib/hyperloop/on_client.rb', line 2 def self.on_client? !(`typeof Opal.global.document === 'undefined'`) if RUBY_ENGINE == 'opal' end |
.on_config_initialized(&block) ⇒ Object
37 38 39 |
# File 'lib/hyperloop/config_settings.rb', line 37 def on_config_initialized &block initialized_blocks << block end |
.on_config_reset(&block) ⇒ Object
33 34 35 |
# File 'lib/hyperloop/config_settings.rb', line 33 def on_config_reset &block reset_blocks << block end |
.require(value, gem: nil, override_with: nil, client_only: nil, tree: nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/hyperloop/requires.rb', line 7 def require(value, gem: nil, override_with: nil, client_only: nil, tree: nil) if override_with define_class_method "#{override_with}=" do |value| requires.detect do |name, _value, _client_only, _kind| name == override_with end[1] = value end end kind = if gem :gem elsif tree :tree end requires << [override_with, value, client_only, kind] end |
.require_gem(value, override_with: nil, client_only: nil) ⇒ Object
27 28 29 |
# File 'lib/hyperloop/requires.rb', line 27 def require_gem(value, override_with: nil, client_only: nil) require(value, override_with: override_with, client_only: client_only, gem: true) end |
.require_tree(value, override_with: nil, client_only: nil) ⇒ Object
23 24 25 |
# File 'lib/hyperloop/requires.rb', line 23 def require_tree(value, override_with: nil, client_only: nil) require(value, override_with: override_with, client_only: client_only, tree: true) end |
.requires ⇒ Object
3 4 5 |
# File 'lib/hyperloop/requires.rb', line 3 def requires @requires ||= [] end |
.reset_blocks ⇒ Object
8 9 10 |
# File 'lib/hyperloop/config_settings.rb', line 8 def reset_blocks @reset_blocks ||= [] end |