Module: Jets::Core

Extended by:
Memoist
Included in:
Jets
Defined in:
lib/jets/core.rb

Constant Summary collapse

@@application =

Calling application triggers load of configs. Jets’ the default config/application.rb is loaded, then the project’s config/application.rb is loaded.

nil
@@call_count =

NOTE: In development this will always be 1 because the app gets reloaded. On AWS Lambda, this will be ever increasing until the container gets replaced.

0
@@prewarm_count =
0

Instance Method Summary collapse

Instance Method Details

#applicationObject



11
12
13
14
15
16
17
# File 'lib/jets/core.rb', line 11

def application
  return @@application if @@application

  @@application = Jets::Application.new
  @@application.setup!
  @@application
end

#awsObject



25
26
27
# File 'lib/jets/core.rb', line 25

def aws
  application.aws
end

#boot(options = {}) ⇒ Object

Load all application base classes and project classes



30
31
32
# File 'lib/jets/core.rb', line 30

def boot(options={})
  Jets::Booter.boot!(options)
end

#build_rootObject



52
53
54
# File 'lib/jets/core.rb', line 52

def build_root
  "/tmp/jets/#{config.project_name}".freeze
end

#call_countObject



146
147
148
# File 'lib/jets/core.rb', line 146

def call_count
  @@call_count
end

#class_mappings(class_name) ⇒ Object



115
116
117
118
119
120
# File 'lib/jets/core.rb', line 115

def class_mappings(class_name)
  map = {
    "Jets::Io" => "Jets::IO",
  }
  map[class_name] || class_name
end

#configObject

For some reason memoize doesnt work with application, think there’s some circular dependency issue. Figure this out later.



21
22
23
# File 'lib/jets/core.rb', line 21

def config
  application.config
end

#eager_load!Object



75
76
77
78
# File 'lib/jets/core.rb', line 75

def eager_load!
  eager_load_jets
  eager_load_app
end

#eager_load_appObject

Eager load user’s application



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/jets/core.rb', line 123

def eager_load_app
  Dir.glob("#{Jets.root}app/**/*.rb").select do |path|
    next if !File.file?(path) or path =~ %r{/javascript/} or path =~ %r{/views/}
    next if path.include?('app/functions') || path.include?('app/shared/functions')

    class_name = path
                  .sub(/\.rb$/,'') # remove .rb
                  .sub(%{^\./},'') # remove ./
                  .sub(Jets.root.to_s,'')
                  .sub(%r{app/shared/\w+/},'') # remove shared/resources or shared/extensions
                  .sub(%r{app/\w+/},'') # remove app/controllers or app/jobs etc
    class_name = class_name.classify
    class_name.constantize # use constantize instead of require so dont have to worry about order.
  end
end

#eager_load_jetsObject

Eager load jet’s lib and classes



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/jets/core.rb', line 81

def eager_load_jets
  lib_jets = File.expand_path(".", File.dirname(__FILE__))
  Dir.glob("#{lib_jets}/**/*.rb").select do |path|
    next if !File.file?(path)
    next if skip_eager_load_paths?(path)

    path = path.sub("#{lib_jets}/","jets/")
    class_name = path
                  .sub(/\.rb$/,'') # remove .rb
                  .sub(/^\.\//,'') # remove ./
                  .sub(/app\/\w+\//,'') # remove app/controllers or app/jobs etc
                  .camelize
    # special class mappings
    class_name = class_mappings(class_name)
    class_name.constantize # use constantize instead of require so dont have to worry about order.
  end
end

#envObject



45
46
47
48
49
# File 'lib/jets/core.rb', line 45

def env
  env = ENV['JETS_ENV'] || 'development'
  ENV['RAILS_ENV'] = ENV['RACK_ENV'] = env
  ActiveSupport::StringInquirer.new(env)
end

#increase_call_countObject



142
143
144
# File 'lib/jets/core.rb', line 142

def increase_call_count
  @@call_count += 1
end

#increase_prewarm_countObject



151
152
153
# File 'lib/jets/core.rb', line 151

def increase_prewarm_count
  @@prewarm_count += 1
end

#lazy_load?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/jets/core.rb', line 168

def lazy_load?
  config.ruby.lazy_load
end

#load_tasksObject



67
68
69
# File 'lib/jets/core.rb', line 67

def load_tasks
  Jets::Commands::RakeTasks.load!
end

#loggerObject



57
58
59
# File 'lib/jets/core.rb', line 57

def logger
  Jets::Logger.new($stderr)
end

#prewarm_countObject



155
156
157
# File 'lib/jets/core.rb', line 155

def prewarm_count
  @@prewarm_count
end

#project_namespaceObject



159
160
161
# File 'lib/jets/core.rb', line 159

def project_namespace
  [config.project_name, config.short_env, config.env_extra].compact.join('-').gsub('_','-')
end

#rack?Boolean

Returns:

  • (Boolean)


163
164
165
166
# File 'lib/jets/core.rb', line 163

def rack?
  path = "#{Jets.root}rack"
  File.exist?(path) || File.symlink?(path)
end

#rootObject

Ensures trailing slash Useful for appending a ‘./’ in front of a path or leaving it alone. Returns: ‘/path/with/trailing/slash/’ or ‘./’



37
38
39
40
41
42
# File 'lib/jets/core.rb', line 37

def root
  root = ENV['JETS_ROOT'].to_s
  root = '.' if root == ''
  root = "#{root}/" unless root.ends_with?('/')
  Pathname.new(root)
end

#skip_eager_load_paths?(path) ⇒ Boolean

Skip these paths because eager loading doesnt work for them.

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jets/core.rb', line 100

def skip_eager_load_paths?(path)
  path =~ %r{/cli} ||
  path =~ %r{/core_ext} ||
  path =~ %r{/default/application} ||
  path =~ %r{/functions} ||
  path =~ %r{/internal/app} ||
  path =~ %r{/jets/stack} ||
  path =~ %r{/rackup_wrappers} ||
  path =~ %r{/rails_overrides} ||
  path =~ %r{/reconfigure_rails} ||
  path =~ %r{/templates/} ||
  path =~ %r{/version} ||
  path =~ %r{/webpacker}
end

#versionObject



71
72
73
# File 'lib/jets/core.rb', line 71

def version
  Jets::VERSION
end

#webpacker?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/jets/core.rb', line 62

def webpacker?
  Gem.loaded_specs.keys.include?("webpacker")
end