Module: Hyperloop

Defined in:
lib/hyperloop/env.rb,
lib/hyperloop/context.rb,
lib/hyperloop/imports.rb,
lib/hyperloop/on_error.rb,
lib/hyperloop/rail_tie.rb,
lib/hyperloop/on_client.rb,
lib/hyperloop/autoloader.rb,
lib/hyperloop/js_imports.rb,
lib/hyperloop/client_stubs.rb,
lib/hyperloop/client_readers.rb,
lib/hyperloop/config/version.rb,
lib/hyperloop/config_settings.rb,
lib/hyperloop/environment/test/hyperloop_env.rb,
lib/hyperloop/environment/staging/hyperloop_env.rb,
lib/hyperloop/environment/production/hyperloop_env.rb,
lib/hyperloop/environment/development/hyperloop_env.rb

Defined Under Namespace

Modules: Config, Context Classes: Autoloader, Railtie

Class Method Summary collapse

Class Method Details

.cancel_import(value) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/hyperloop/imports.rb', line 22

def cancel_import(value)
  return unless value
  current_spec = import_list.detect { |old_value, *_rest| value == old_value }
  if current_spec
    current_spec[1] = true
  else
    import_list << [value, true, true, true, false]
  end
end

.cancel_webpack_importsObject



32
33
34
35
# File 'lib/hyperloop/imports.rb', line 32

def cancel_webpack_imports
  import_list.collect { |name, _c, _co, _so, _t, js_import, *_rest| js_import && name }
             .each { |name| cancel_import name }
end

.client_guard(render_on_server, render_on_client) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/hyperloop/imports.rb', line 92

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_hashObject



8
9
10
# File 'lib/hyperloop/client_readers.rb', line 8

def client_reader_hash
  @client_readers_hash ||= {}
end

.client_readersObject



4
5
6
# File 'lib/hyperloop/client_readers.rb', line 4

def client_readers
  @client_readers ||= []
end

.configuration {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Hyperloop)

    the object that the method was called on



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

.envObject



2
3
4
5
6
7
8
9
# File 'lib/hyperloop/env.rb', line 2

def self.env
  @environment ||= begin
    env = Rails.env if defined? Rails
    env ||= ENV['RACK_ENV']
    env = 'development' unless %w[development production test staging].include? env
    ActiveSupport::StringInquirer.new(env)
  end
end

.generate_directive(directive, to, file, render_on_server, render_on_client) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hyperloop/imports.rb', line 66

def generate_directive(directive, to, file, render_on_server, render_on_client)
  gem_path = File.expand_path('../', 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



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hyperloop/imports.rb', line 78

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hyperloop/imports.rb', line 47

def generate_requires(mode, sys, file)
  handle_webpack
  import_list.collect do |value, cancelled, render_on_server, render_on_client, kind|
    next if cancelled
    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

.handle_webpackObject



37
38
39
40
41
42
43
44
45
# File 'lib/hyperloop/imports.rb', line 37

def handle_webpack
  return unless defined? Webpacker
  client_only_manifest = Webpacker.manifest.lookup("client_only.js")
  client_and_server_manifest = Webpacker.manifest.lookup("client_and_server.js")
  return unless client_only_manifest || client_and_server_manifest
  cancel_webpack_imports
  import client_only_manifest.split("/").last, client_only: true, at_head: true if client_only_manifest
  import client_and_server_manifest.split("/").last, at_head: true if client_and_server_manifest
end

.import(*args) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/hyperloop/imports.rb', line 8

def import(value, gem: nil, cancelled: nil, client_only: nil, server_only: nil, tree: nil, js_import: nil, at_head: nil)
  return if import_list.detect { |current_value, *_rest| value == current_value }
  new_element = [
    value, cancelled, !client_only, !server_only, (tree ? :tree : :gem), js_import
  ]
  import_list.send(at_head ? :unshift : :push, new_element)
end

.import_listObject



4
5
6
# File 'lib/hyperloop/imports.rb', line 4

def import_list
  @import_list ||= []
end

.import_tree(*args) ⇒ Object



18
19
20
# File 'lib/hyperloop/imports.rb', line 18

def import_tree(value, cancelled: nil, client_only: nil, server_only: nil)
  import(value, cancelled: cancelled, client_only: client_only, server_only: server_only, tree: true)
end

.imports(*args) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/hyperloop/imports.rb', line 16

def import(value, gem: nil, cancelled: nil, client_only: nil, server_only: nil, tree: nil, js_import: nil, at_head: nil)
  return if import_list.detect { |current_value, *_rest| value == current_value }
  new_element = [
    value, cancelled, !client_only, !server_only, (tree ? :tree : :gem), js_import
  ]
  import_list.send(at_head ? :unshift : :push, new_element)
end

.initialized_blocksObject



4
5
6
# File 'lib/hyperloop/config_settings.rb', line 4

def initialized_blocks
  @initialized_blocks ||= []
end

.js_import(value, client_only: nil, server_only: nil, defines:) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/hyperloop/js_imports.rb', line 3

def js_import(value, client_only: nil, server_only: nil, defines:)
  defines = [*defines]
  if RUBY_ENGINE != 'opal'
    import(value, client_only: client_only, server_only: server_only, js_import: true)
  else
    on_server = `typeof Opal.global.document === 'undefined'`
    return if (server_only && !on_server) || (client_only && on_server)
    defines.each do |name|
      next unless `Opal.global[#{name}] === undefined`
      raise "The package #{name} was not found. Add it to the webpack "\
            "#{client_only ? 'client_only.js' : 'client_and_server.js'} manifest."
    end
  end
end

.on_client?Boolean

Returns:

  • (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

.on_error(err, reason, details) ⇒ Object



2
3
4
# File 'lib/hyperloop/on_error.rb', line 2

def self.on_error(err, reason, details)
  # do whatever you want here... i.e. log, debug, etc
end

.reset_blocksObject



8
9
10
# File 'lib/hyperloop/config_settings.rb', line 8

def reset_blocks
  @reset_blocks ||= []
end