module Hyperstack

Define a primitive Boot Operation that will act like a full blown operation. If Operation is defined before this then we skip the whole exercise. Likewise when Operation defines the Boot class, it will check for a receivers method and copy any defined receivers into the updated Boot class.

insure that stubs for Hyperstack::Hotloader.record and window.Hyperstack.hotloader are defined importing 'hyperstack/hotloader' will define/redefine these

Public Class Methods

cancel_import(value) click to toggle source
# File lib/hyperstack/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_imports() click to toggle source
# File lib/hyperstack/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) click to toggle source
# File lib/hyperstack/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) click to toggle source
# File lib/hyperstack/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() click to toggle source
# File lib/hyperstack/client_readers.rb, line 8
def client_reader_hash
  @client_readers_hash ||= {}
end
client_readers() click to toggle source
# File lib/hyperstack/client_readers.rb, line 4
def client_readers
  @client_readers ||= []
end
configuration() { |self| ... } click to toggle source
# File lib/hyperstack/config_settings.rb, line 12
def configuration
  reset_blocks.each(&:call)
  yield self
  initialized_blocks.each(&:call)
end
define_setting(name, default = nil, &block) click to toggle source
# File lib/hyperstack/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
deprecation_warning(name, message) click to toggle source
# File lib/hyperstack/deprecation_warning.rb, line 2
def self.deprecation_warning(name, message)
  return if env.production?
  @deprecation_messages ||= []
  message = "Warning: Deprecated feature used in #{name}. #{message}"
  return if @deprecation_messages.include? message
  @deprecation_messages << message
  `console.warn.apply(console, [message])`
end
env() click to toggle source
# File lib/hyperstack/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) click to toggle source
# File lib/hyperstack/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', 'hyperstack', 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) click to toggle source
# File lib/hyperstack/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) click to toggle source
# File lib/hyperstack/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_webpack() click to toggle source
# File lib/hyperstack/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) click to toggle source
# File lib/hyperstack/client_stubs.rb, line 3
def import(*args)
end
Also aliased as: imports
import_list() click to toggle source
# File lib/hyperstack/imports.rb, line 4
def import_list
  @import_list ||= []
end
import_tree(*args) click to toggle source
# File lib/hyperstack/client_stubs.rb, line 9
def import_tree(*args)
end
imports(*args) click to toggle source
# File lib/hyperstack/client_stubs.rb, line 6
def imports(*args)
end
initialized_blocks() click to toggle source
# File lib/hyperstack/config_settings.rb, line 4
def initialized_blocks
  @initialized_blocks ||= []
end
js_import(value, client_only: nil, server_only: nil, defines:) click to toggle source
# File lib/hyperstack/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?() click to toggle source
# File lib/hyperstack/on_client.rb, line 2
def self.on_client?
  !(`typeof Opal.global.document === 'undefined'`) if RUBY_ENGINE == 'opal'
end
on_config_initialized(&block) click to toggle source
# File lib/hyperstack/config_settings.rb, line 37
def on_config_initialized &block
  initialized_blocks << block
end
on_config_reset(&block) click to toggle source
# File lib/hyperstack/config_settings.rb, line 33
def on_config_reset &block
  reset_blocks << block
end
on_error(err, reason, details) click to toggle source
# File lib/hyperstack/on_error.rb, line 2
def self.on_error(err, reason, details)
  # do whatever you want here... i.e. log, debug, etc
end
reset_blocks() click to toggle source
# File lib/hyperstack/config_settings.rb, line 8
def reset_blocks
  @reset_blocks ||= []
end

Private Class Methods

define_class_method(name, &block) click to toggle source
# File lib/hyperstack/config_settings.rb, line 43
def define_class_method(name, &block)
  (class << self; self; end).instance_eval do
    define_method name, &block
  end
end