Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit b9ee1ed

Browse files
committed
passes its test
1 parent 64952a4 commit b9ee1ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1351
-53
lines changed

hyperloop-config-0.8.1.gem

-7.5 KB
Binary file not shown.

hyperloop-config.gemspec

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
44

55
Gem::Specification.new do |spec|
66
spec.name = "hyperloop-config"
7-
spec.version = "0.9.0"
7+
spec.version = "0.9.1"
88
spec.authors = ["catmando"]
99
spec.email = ["mitch@catprint.com"]
1010

@@ -21,4 +21,16 @@ Gem::Specification.new do |spec|
2121

2222
spec.add_development_dependency "bundler", "~> 1.12"
2323
spec.add_development_dependency "rake", "~> 10.0"
24+
25+
spec.add_development_dependency 'hyper-spec'
26+
spec.add_development_dependency 'rspec'
27+
spec.add_development_dependency 'timecop'
28+
spec.add_development_dependency 'opal-rails'
29+
spec.add_development_dependency 'sqlite3'
30+
spec.add_development_dependency 'rails'
31+
spec.add_development_dependency 'jquery-rails'
32+
33+
# Keep linter-rubocop happy
34+
spec.add_development_dependency 'rubocop'
35+
2436
end

lib/components.rb.erb

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/hyperloop-config.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,5 @@
1010
require 'hyperloop/rail_tie' if defined? Rails
1111
Hyperloop.require 'opal', gem: true
1212
Hyperloop.require 'hyperloop-config', gem: true
13-
# class Sprockets::DirectiveProcessor
14-
# def process_depend_on_config_directive(file)
15-
# path = File.expand_path(file, "#{Rails.root}/config")
16-
# resolve(path)
17-
# end
18-
# end
1913
Opal.append_path(File.expand_path('../', __FILE__).untaint)
2014
end

lib/hyperloop-prerender-loader.rb.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= Hyperloop.generate_requires(:server, __FILE__) %>

lib/hyperloop/rail_tie.rb

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ class Options < ActiveSupport::OrderedOptions
66
def delete_first(a, e)
77
a.delete_at(a.index(e) || a.length)
88
end
9-
def add_paths=(x)
9+
10+
def auto_config=(on)
1011
Rails.configuration.tap do |config|
11-
if x
12+
if on
1213
config.eager_load_paths += %W(#{config.root}/app/hyperloop/models)
1314
config.autoload_paths += %W(#{config.root}/app/hyperloop/models)
1415
# config.eager_load_paths += %W(#{config.root}/app/hyperloop/stores)
@@ -17,9 +18,6 @@ def add_paths=(x)
1718
config.autoload_paths += %W(#{config.root}/app/hyperloop/operations)
1819

1920
config.assets.paths.unshift ::Rails.root.join('app', 'hyperloop').to_s
20-
config.eager_load_paths += %W(#{config.root}/app/models/public)
21-
config.autoload_paths += %W(#{config.root}/app/models/public)
22-
config.assets.paths.unshift ::Rails.root.join('app', 'models').to_s
2321
else
2422
delete_first config.eager_load_paths, "#{config.root}/app/hyperloop/models"
2523
delete_first config.autoload_paths, "#{config.root}/app/hyperloop/models"
@@ -29,31 +27,31 @@ def add_paths=(x)
2927
delete_first config.autoload_paths, "#{config.root}/app/hyperloop/operations"
3028

3129
delete_first config.assets.paths, ::Rails.root.join('app', 'hyperloop').to_s
32-
delete_first config.eager_load_paths, %W(#{config.root}/app/models/public)
33-
delete_first config.autoload_paths, %W(#{config.root}/app/models/public)
34-
delete_first config.assets.paths, ::Rails.root.join('app', 'models').to_s
3530
end
3631
end
3732
super
3833
end
3934
end
4035

41-
def add_hyperloop_directories(config)
42-
return if config.hyperloop.key?(:add_directories) && !config.hyperloop.add_directories
43-
Hyperloop.require_tree('views/components')
44-
Hyperloop.require_tree('hyperloop')
45-
Hyperloop.require_gem 'opal-jquery', override_with: :opal_jquery, client_only: true # move to hyper-component once things are working
46-
end
47-
4836
# note in case of problems with eager load paths have a look at
4937
# https://github.com/opal/opal-rails/blob/master/lib/opal/rails/engine.rb
5038
config.hyperloop = Options.new
5139

5240
config.before_configuration do |app|
53-
config.hyperloop.add_paths = true
41+
config.hyperloop.auto_config = true
5442
end
43+
44+
FILES = {files: ['hyperloop-prerender-loader.js']}
45+
5546
config.after_initialize do |app|
56-
add_hyperloop_directories(app.config)
47+
next unless config.hyperloop.auto_config
48+
Hyperloop.require_tree('hyperloop')
49+
next unless config.respond_to?(:react)
50+
if (opts = config.react.server_renderer_options)
51+
opts.merge! FILES
52+
else
53+
config.react.server_renderer_options = FILES
54+
end
5755
end
5856
end
5957
end

lib/hyperloop/requires.rb

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,90 @@ def requires
44
@requires ||= []
55
end
66

7-
def require(value, gem: nil, override_with: nil, client_only: nil, tree: nil)
8-
if override_with
9-
define_class_method "#{override_with}=" do |value|
10-
requires.detect do |name, _value, _client_only, _kind|
11-
name == override_with
12-
end[1] = value
7+
def require(value, gem: nil, instead_of: nil, client_only: nil, server_only: nil, tree: nil)
8+
if instead_of
9+
current_spec = requires.detect { |old_value, *_rest| instead_of == old_value }
10+
if current_spec
11+
current_spec[0] = value
12+
else
13+
raise [
14+
"Could not substitute require '#{instead_of}' with '#{value}'. '#{instead_of}' not found.",
15+
'The following requires are listed:',
16+
*requires.collect { |old_value, *_rest| old_value }
17+
].join("\n")
1318
end
19+
else
20+
kind = if gem
21+
:gem
22+
elsif tree
23+
:tree
24+
end
25+
requires << [value, !client_only, !server_only, kind]
1426
end
15-
kind = if gem
16-
:gem
17-
elsif tree
18-
:tree
19-
end
20-
requires << [override_with, value, client_only, kind]
2127
end
2228

23-
def require_tree(value, override_with: nil, client_only: nil)
24-
require(value, override_with: override_with, client_only: client_only, tree: true)
29+
def unrequire(value)
30+
require(nil, instead_of: value)
2531
end
2632

27-
def require_gem(value, override_with: nil, client_only: nil)
28-
require(value, override_with: override_with, client_only: client_only, gem: true)
33+
def require_tree(value, instead_of: nil, client_only: nil, server_only: nil)
34+
require(value, instead_of: instead_of, client_only: client_only, server_only: server_only, tree: true)
35+
end
36+
37+
def require_gem(value, instead_of: nil, client_only: nil, server_only: nil)
38+
require(value, instead_of: instead_of, client_only: client_only, server_only: server_only, gem: true)
2939
end
3040

3141
def generate_requires(mode, file)
3242
puts "***** generating requires for #{mode} - #{file}"
33-
requires.collect do |name, value, client_only, kind|
43+
requires.collect do |value, render_on_server, render_on_client, kind|
3444
next unless value
35-
next if client_only && mode != :client
45+
next if mode == :client && !render_on_client
46+
next if mode == :server && !render_on_server
3647
if kind == :tree
37-
generate_require_tree(value, client_only)
48+
generate_require_tree(value, render_on_server, render_on_client)
3849
elsif kind == :gem
39-
r = "require '#{value}' #{client_guard(client_only)}"
50+
r = "require '#{value}' #{client_guard(render_on_server, render_on_client)}"
4051
puts r
4152
"puts \"#{r}\"; #{r}"
4253
else
43-
generate_directive(:require, value, file, client_only)
54+
generate_directive(:require, value, file, render_on_server, render_on_client)
4455
end
4556
end.compact.join("\n")
4657
end
4758

48-
def generate_directive(directive, to, file, client_only = false)
59+
def generate_directive(directive, to, file, render_on_server, render_on_client)
4960
gem_path = File.expand_path('../', file).split('/')
5061
comp_path = Rails.root.join('app', 'hyperloop', to).to_s.split('/')
5162
while comp_path.first == gem_path.first do
5263
gem_path.shift
5364
comp_path.shift
5465
end
55-
r = "#{directive} '#{(['.'] + ['..'] * gem_path.length + comp_path).join('/')}' #{client_guard(client_only)}"
66+
r = "#{directive} '#{(['.'] + ['..'] * gem_path.length + comp_path).join('/')}' #{client_guard(render_on_server, render_on_client)}"
5667
puts r
5768
"puts \"#{r}\"; #{r}"
5869
end
5970

60-
def generate_require_tree(path, client_only)
71+
def generate_require_tree(path, render_on_server, render_on_client)
6172
base_name = Rails.root.join('app', path).to_s+'/'
6273
Dir.glob(Rails.root.join('app', path, '**', '*')).collect do |fname|
63-
if ['.js', '.rb', '.erb'].include? File.extname(fname)
64-
r = "require '#{fname.gsub(base_name, '')}' #{client_guard(client_only)}"
74+
fname = fname.gsub(/^#{base_name}/, '')
75+
fname = fname.gsub(/\.erb$/, '')
76+
if fname =~ /(\.js$)|(\.rb$)/
77+
fname = fname.gsub(/(\.js$)|(\.rb$)/, '')
78+
r = "require '#{fname}' #{client_guard(render_on_server, render_on_client)}"
6579
puts r
6680
"puts \"#{r}\"; #{r}"
6781
end
6882
end.compact.join("\n")
6983
end
7084

71-
def client_guard(client_only)
72-
"# CLIENT ONLY" if client_only
85+
def client_guard(render_on_server, render_on_client)
86+
if !render_on_server
87+
'# CLIENT ONLY'
88+
elsif !render_on_client
89+
'# SERVER ONLY'
90+
end
7391
end
7492

7593
end

spec/hyperloop_config_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
3+
# rubocop:disable Metrics/BlockLength
4+
describe 'Hyperloop Auto Config', js: true do
5+
6+
after(:each) do
7+
Timecop.return
8+
end
9+
10+
it 'will find and load everything and expand erb files' do
11+
`rm -rf spec/test_app/tmp/cache`
12+
Timecop.freeze
13+
visit '/'
14+
expect(evaluate_script("Opal.Test.$const_get('TIME')")).to eq("#{Time.now}")
15+
end
16+
17+
end

spec/spec_helper.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'hyper-spec'
2+
require 'pry'
3+
require 'opal-rails'
4+
require 'hyperloop-config'
5+
require 'jquery-rails'
6+
7+
8+
ENV['RAILS_ENV'] ||= 'test'
9+
require File.expand_path('../test_app/config/environment', __FILE__)
10+
11+
require 'rspec/rails'
12+
require 'timecop'
13+
14+
RSpec.configure do |config|
15+
config.color = true
16+
config.formatter = :documentation
17+
end

spec/test_app/.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile '~/.gitignore_global'
6+
7+
# Ignore bundler config.
8+
/.bundle
9+
10+
# Ignore the default SQLite database.
11+
/db/*.sqlite3
12+
/db/*.sqlite3-journal
13+
14+
# Ignore all logfiles and tempfiles.
15+
/log/*
16+
/tmp/*
17+
!/log/.keep
18+
!/tmp/.keep
19+
20+
# Ignore Byebug command history file.
21+
.byebug_history

spec/test_app/Gemfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
source 'https://rubygems.org'
2+
3+
git_source(:github) do |repo_name|
4+
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
5+
"https://github.com/#{repo_name}.git"
6+
end
7+
8+
9+
gem 'rails', '~> 5.0.1'
10+
gem 'sqlite3'
11+
gem 'puma', '~> 3.6.0'
12+
gem 'jquery-rails'
13+
14+
gem 'hyperloop-config', path: '../..'
15+
gem 'hyper-spec', git: 'https://www.github.com/ruby-hyperloop/hyper-spec'
16+
gem 'opal', '~> 0.9.0'
17+
gem 'opal-browser'
18+
gem 'opal-jquery'
19+
gem 'opal-rails', '~> 0.9.0'
20+
gem 'therubyracer', platforms: :ruby
21+
gem 'react-rails', '~> 1.9.0'
22+
23+
group :development, :test do
24+
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
25+
gem 'byebug', platform: :mri
26+
gem 'rspec-steps'
27+
end
28+
29+
group :development do
30+
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
31+
gem 'web-console', '>= 3.3.0'
32+
gem 'listen', '~> 3.0.5'
33+
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
34+
gem 'spring'
35+
gem 'spring-watcher-listen', '~> 2.0.0'
36+
gem 'rubocop', require: false
37+
end
38+
39+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
40+
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

0 commit comments

Comments
 (0)