Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@
/pkg/
/spec/reports/
/tmp/

# ignore gems
*.gem

# ignore Idea files
.idea
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in hyperloop-config.gemspec
source ENV['HYPER_DEV_GEM_SOURCE'] if ENV['HYPER_DEV_GEM_SOURCE']
gemspec
38 changes: 17 additions & 21 deletions hyperloop-config.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,31 @@ lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = "hyperloop-config"
spec.version = "0.9.11"
spec.authors = ["catmando"]
spec.email = ["[email protected]"]
spec.name = 'hyperloop-config'
spec.version = '0.15.3'
spec.authors = ['catmando', 'janbiedermann']
spec.email = ['[email protected]']

spec.summary = %q{Provides a single point configuration module for hyperloop gems}
spec.homepage = "http://ruby-hyperloop.io"
spec.license = "MIT"
spec.homepage = 'http://ruby-hyperloop.io'
spec.license = 'MIT'

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_dependency 'opal'
spec.add_dependency 'opal-browser'

spec.add_development_dependency "bundler", "~> 1.12"
spec.add_development_dependency "rake", "~> 10.0"

spec.add_development_dependency 'hyper-spec'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'timecop'
spec.add_development_dependency 'opal-rails'
spec.add_development_dependency 'sqlite3'
spec.add_development_dependency 'rails'
spec.add_dependency 'uglifier'
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'hyper-spec', '0.15.3'
spec.add_development_dependency 'jquery-rails'

# Keep linter-rubocop happy
spec.add_development_dependency 'opal-rails', '>= 0.9.3'
spec.add_development_dependency 'rails', '>= 5.1.4'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'rubocop'

spec.add_development_dependency 'sqlite3'
spec.add_development_dependency 'timecop', '~> 0.8.1'
end
3 changes: 3 additions & 0 deletions lib/hyperloop-config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
Hyperloop.import 'opal', gem: true
Hyperloop.import 'browser', client_only: true
Hyperloop.import 'hyperloop-config', gem: true
Hyperloop.import 'hyperloop/autoloader'
Hyperloop.import 'hyperloop/autoloader_starter'

Opal.append_path(File.expand_path('../', __FILE__).untaint)
end
138 changes: 138 additions & 0 deletions lib/hyperloop/autoloader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
require 'set'

module Hyperloop
class Autoloader
# All files ever loaded.
def self.history=(a)
@@history = a
end
def self.history
@@history
end
self.history = Set.new

def self.load_paths=(a)
@@load_paths = a
end
def self.load_paths
@@load_paths
end
self.load_paths = []

def self.loaded=(a)
@@loaded = a
end
def self.loaded
@@loaded
end
self.loaded = Set.new

def self.loading=(a)
@@loading = a
end
def self.loading
@@loading
end
self.loading = []

def self.const_missing(const_name, mod)
# name.nil? is testing for anonymous
from_mod = mod.name.nil? ? guess_for_anonymous(const_name) : mod
load_missing_constant(from_mod, const_name)
end

def self.guess_for_anonymous(const_name)
if Object.const_defined?(const_name)
raise NameError.new "#{const_name} cannot be autoloaded from an anonymous class or module", const_name
else
Object
end
end

def self.load_missing_constant(from_mod, const_name)
# see active_support/dependencies.rb in case of reloading on how to handle
qualified_name = qualified_name_for(from_mod, const_name)
qualified_path = underscore(qualified_name)

module_path = search_for_module(qualified_path)
if module_path
if loading.include?(module_path)
raise "Circular dependency detected while autoloading constant #{qualified_name}"
else
require_or_load(from_mod, module_path)
raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{module_path} to define it" unless from_mod.const_defined?(const_name, false)
return from_mod.const_get(const_name)
end
elsif (parent = from_mod.parent) && parent != from_mod &&
! from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
begin
return parent.const_missing(const_name)
rescue NameError => e
raise unless missing_name?(e, qualified_name_for(parent, const_name))
end
end
end

def self.missing_name?(e, name)
mn = if /undefined/ !~ e.message
$1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ e.message
end
mn == name
end

# Returns the constant path for the provided parent and constant name.
def self.qualified_name_for(mod, name)
mod_name = to_constant_name(mod)
mod_name == 'Object' ? name.to_s : "#{mod_name}::#{name}"
end

def self.require_or_load(from_mod, module_path)
return if loaded.include?(module_path)
loaded << module_path
loading << module_path

begin
result = require module_path
rescue Exception
loaded.delete module_path
raise LoadError, "Unable to autoload: require_or_load #{module_path} failed"
ensure
loading.pop
end

# Record history *after* loading so first load gets warnings.
history << module_path
result
# end
end

def self.search_for_module(path)
# oh my! imagine Bart Simpson, writing on the board:
# "javascript is not ruby, javascript is not ruby, javascript is not ruby, ..."
# then running home, starting irb, on the fly developing a chat client and opening a session with Homer at his workplace: "Hi Dad ..."
load_paths.each do |load_path|
mod_path = load_path + '/' + path
return mod_path if `Opal.modules.hasOwnProperty(#{mod_path})`
end
return path if `Opal.modules.hasOwnProperty(#{path})`
nil # Gee, I sure wish we had first_match ;-)
end

# Convert the provided const desc to a qualified constant name (as a string).
# A module, class, symbol, or string may be provided.
def self.to_constant_name(desc) #:nodoc:
case desc
when String then desc.sub(/^::/, '')
when Symbol then desc.to_s
when Module
desc.name ||
raise(ArgumentError, 'Anonymous modules have no name to be referenced by')
else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}"
end
end

def self.underscore(string)
string.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
end
end
end
15 changes: 15 additions & 0 deletions lib/hyperloop/autoloader_starter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Hyperloop::Autoloader.load_paths = %w[components models operations stores]

class Object
class << self
alias _autoloader_original_const_missing const_missing

def const_missing(const_name)
# need to call original code because some things are set up there
# original code may also be overloaded by reactrb, for example
_autoloader_original_const_missing(const_name)
rescue StandardError => e
Hyperloop::Autoloader.const_missing(const_name, self) || raise(e)
end
end
end