Module: Hyperloop
- Defined in:
- lib/hyperloop/context.rb,
lib/hyperloop/imports.rb,
lib/hyperloop/rail_tie.rb,
lib/hyperloop/on_client.rb,
lib/hyperloop/client_stubs.rb,
lib/hyperloop/client_readers.rb,
lib/hyperloop/config_settings.rb
Defined Under Namespace
Modules: Context Classes: Railtie
Class Method Summary collapse
- .cancel_import(value) ⇒ Object
- .client_guard(render_on_server, render_on_client) ⇒ Object
- .client_reader(*args) ⇒ Object
- .client_reader_hash ⇒ Object
- .client_readers ⇒ Object
- .compile_and_compress(name, never_compress: false) ⇒ Object
- .configuration {|_self| ... } ⇒ Object
- .define_setting(name, default = nil, &block) ⇒ Object
- .generate_directive(directive, to, file, render_on_server, render_on_client) ⇒ Object
- .generate_require_tree(path, render_on_server, render_on_client) ⇒ Object
- .generate_requires(mode, sys, file) ⇒ Object
- .import(*args) ⇒ Object
- .import_list ⇒ Object
- .import_tree(*args) ⇒ Object
- .imports(*args) ⇒ Object
- .initialized_blocks ⇒ Object
- .on_client? ⇒ Boolean
- .on_config_initialized(&block) ⇒ Object
- .on_config_reset(&block) ⇒ Object
- .reset_blocks ⇒ Object
Class Method Details
.cancel_import(value) ⇒ Object
36 37 38 |
# File 'lib/hyperloop/imports.rb', line 36 def cancel_import(value) import(nil, instead_of: value) end |
.client_guard(render_on_server, render_on_client) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/hyperloop/imports.rb', line 84 def client_guard(render_on_server, render_on_client) if !render_on_server '# CLIENT ONLY' elsif !render_on_client '# SERVER ONLY' end 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 |
.compile_and_compress(name, never_compress: false) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/hyperloop/imports.rb', line 94 def compile_and_compress(name, never_compress: false) start_time = Time.now puts "\n\n##########################################################################\n" puts " HYPERLOOP LIBRARIES PRECOMPILING AND MINIFYING\n" puts " FIRST TIME BOOTING YOUR APP IT CAN TAKE 1 OR 2 MINUTES\n" puts " NB: You can force precompiling again by cleaning cache: rm -rf tmp/cache \n" puts "##########################################################################\n\n" puts "Compiling the system assets for #{name}" compiled_code = Rails.application.assets[name].to_s compilation_time = Time.now compiled_code_length = compiled_code.length puts " compiled - length: #{compiled_code_length}" if Hyperloop.compress_system_assets && !never_compress compiled_code = Uglifier.new.compile(compiled_code) puts " minimized - length: #{compiled_code.length}" puts " minification ratio #{(compiled_code.length*100.0/compiled_code_length).round}%" end puts " total time: #{(Time.now-start_time).to_f.round(2)} seconds" compiled_code 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, render_on_server, render_on_client) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/hyperloop/imports.rb', line 58 def generate_directive(directive, to, file, render_on_server, render_on_client) 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(render_on_server, render_on_client)}" puts " #{r}" "puts \"#{r}\"; #{r}" end |
.generate_require_tree(path, render_on_server, render_on_client) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/hyperloop/imports.rb', line 70 def generate_require_tree(path, render_on_server, render_on_client) base_name = Rails.root.join('app', path).to_s+'/' Dir.glob(Rails.root.join('app', path, '**', '*')).sort.collect do |fname| fname = fname.gsub(/^#{base_name}/, '') fname = fname.gsub(/\.erb$/, '') if fname =~ /(\.js$)|(\.rb$)/ fname = fname.gsub(/(\.js$)|(\.rb$)/, '') r = "require '#{fname}' #{client_guard(render_on_server, render_on_client)}" puts " #{r}" "puts \"#{r}\"; #{r}" end end.compact.join("\n") end |
.generate_requires(mode, sys, file) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/hyperloop/imports.rb', line 40 def generate_requires(mode, sys, file) import_list.collect do |value, render_on_server, render_on_client, kind| next unless value next if (sys && kind == :tree) || (!sys && kind != :tree) next if mode == :client && !render_on_client next if mode == :server && !render_on_server if kind == :tree generate_require_tree(value, render_on_server, render_on_client) elsif kind == :gem r = "require '#{value}' #{client_guard(render_on_server, render_on_client)}" puts " #{r}" "puts \"#{r}\"; #{r}" else generate_directive(:require, value, file, render_on_server, render_on_client) end end.compact.join("\n") end |
.import(*args) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/hyperloop/imports.rb', line 8 def import(value, gem: nil, instead_of: nil, client_only: nil, server_only: nil, tree: nil) if instead_of current_spec = import_list.detect { |old_value, *_rest| instead_of == old_value } if current_spec current_spec[0] = value else raise [ "Could not substitute import '#{instead_of}' with '#{value}'. '#{instead_of}' not found.", 'The following files are currently being imported:', *import_list.collect { |old_value, *_rest| old_value } ].join("\n") end elsif !import_list.detect { |current_value, *_rest| value == current_value } kind = if tree :tree else :gem end import_list << [value, !client_only, !server_only, kind] end end |
.import_list ⇒ Object
4 5 6 |
# File 'lib/hyperloop/imports.rb', line 4 def import_list @import_list ||= [] end |
.import_tree(*args) ⇒ Object
32 33 34 |
# File 'lib/hyperloop/imports.rb', line 32 def import_tree(value, instead_of: nil, client_only: nil, server_only: nil) import(value, instead_of: instead_of, client_only: client_only, server_only: server_only, tree: true) end |
.imports(*args) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/hyperloop/imports.rb', line 30 def import(value, gem: nil, instead_of: nil, client_only: nil, server_only: nil, tree: nil) if instead_of current_spec = import_list.detect { |old_value, *_rest| instead_of == old_value } if current_spec current_spec[0] = value else raise [ "Could not substitute import '#{instead_of}' with '#{value}'. '#{instead_of}' not found.", 'The following files are currently being imported:', *import_list.collect { |old_value, *_rest| old_value } ].join("\n") end elsif !import_list.detect { |current_value, *_rest| value == current_value } kind = if tree :tree else :gem end import_list << [value, !client_only, !server_only, kind] end 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 |
.reset_blocks ⇒ Object
8 9 10 |
# File 'lib/hyperloop/config_settings.rb', line 8 def reset_blocks @reset_blocks ||= [] end |