Class: Rack::Svelte::Cogs
- Inherits:
-
Object
- Object
- Rack::Svelte::Cogs
- Defined in:
- lib/rack/svelte/cogs.rb
Overview
This class is the interface between Rack and the svelte-ruby gem
Constant Summary collapse
- DEFAULT_SVELTE_OPTS =
defaults for the Svelte gem config
{}
- DEFAULT_ROOT =
'/'- DEFAULT_COMPONENTS_IN =
'/app/components'- DEFAULT_COMPONENTS_OUT =
'/public/app/js'- DEFAULT_DIR_OUT_OVERWRITE =
false- DEFAULT_DIR_OUT_CLEAR =
false
Instance Attribute Summary collapse
-
#app_root_dir ⇒ Object
options for Rack::Svelte processing.
-
#components_dir_in ⇒ Object
Returns the value of attribute components_dir_in.
-
#components_dir_out ⇒ Object
Returns the value of attribute components_dir_out.
-
#dir_out_clear ⇒ Object
Returns the value of attribute dir_out_clear.
-
#dir_out_overwrite ⇒ Object
Returns the value of attribute dir_out_overwrite.
-
#svelte_options ⇒ Object
svelte-ruby gem options, see github.com/sveltejs/svelte#options.
Instance Method Summary collapse
-
#call(env) ⇒ Object
method required by Rack interface.
-
#call!(env) ⇒ Object
thread safe version using shallow copy of env.
-
#initialize(app, options = {}) ⇒ Cogs
constructor
A new instance of Cogs.
Constructor Details
#initialize(app, options = {}) ⇒ Cogs
Returns a new instance of Cogs.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rack/svelte/cogs.rb', line 26 def initialize(app, = {}) @app = app symbolize_keys!() self.app_root_dir = .delete(:app_root_dir) || DEFAULT_ROOT self.components_dir_in = .delete(:components_dir_in) || DEFAULT_COMPONENTS_IN self.components_dir_out = .delete(:components_dir_out) || DEFAULT_COMPONENTS_OUT self.dir_out_overwrite = .delete(:dir_out_overwrite) || DEFAULT_DIR_OUT_OVERWRITE self.dir_out_clear = .delete(:dir_out_clear) || DEFAULT_DIR_OUT_CLEAR self. = DEFAULT_SVELTE_OPTS.merge() end |
Instance Attribute Details
#app_root_dir ⇒ Object
options for Rack::Svelte processing
20 21 22 |
# File 'lib/rack/svelte/cogs.rb', line 20 def app_root_dir @app_root_dir end |
#components_dir_in ⇒ Object
Returns the value of attribute components_dir_in.
21 22 23 |
# File 'lib/rack/svelte/cogs.rb', line 21 def components_dir_in @components_dir_in end |
#components_dir_out ⇒ Object
Returns the value of attribute components_dir_out.
22 23 24 |
# File 'lib/rack/svelte/cogs.rb', line 22 def components_dir_out @components_dir_out end |
#dir_out_clear ⇒ Object
Returns the value of attribute dir_out_clear.
24 25 26 |
# File 'lib/rack/svelte/cogs.rb', line 24 def dir_out_clear @dir_out_clear end |
#dir_out_overwrite ⇒ Object
Returns the value of attribute dir_out_overwrite.
23 24 25 |
# File 'lib/rack/svelte/cogs.rb', line 23 def dir_out_overwrite @dir_out_overwrite end |
#svelte_options ⇒ Object
svelte-ruby gem options, see github.com/sveltejs/svelte#options
17 18 19 |
# File 'lib/rack/svelte/cogs.rb', line 17 def @svelte_options end |
Instance Method Details
#call(env) ⇒ Object
method required by Rack interface
38 39 40 |
# File 'lib/rack/svelte/cogs.rb', line 38 def call(env) call! env end |
#call!(env) ⇒ Object
thread safe version using shallow copy of env
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rack/svelte/cogs.rb', line 43 def call!(env) dir_in = File.join(self.app_root_dir, self.components_dir_in) dir_out = File.join(self.app_root_dir, self.components_dir_out) raise "ERROR: Dir In cannot be found: #{dir_in}" unless Dir.exist? dir_in raise "ERROR: Dir Out cannot be found: #{dir_out}" unless Dir.exist? dir_out FileUtils.rm File.join(dir_out, '*') if self.dir_out_clear Dir.glob(File.join(dir_in, '**/*')).each do |filename_in| # only html components next unless filename_in =~ /.\.html$/i # create output filename fn_in = filename_in[dir_in.length..-1] # remove input dir fn_out_basename = File.basename(fn_in).split('.')[0] + '.js' fn_out_dir = File.dirname(fn_in) fn_out = File.join(dir_out, fn_out_dir, fn_out_basename) # Kenny Powers # ensure output dir FileUtils.mkdir_p(fn_out_dir) unless Dir.exist?(fn_out_dir) # compile sv_hash = ::Svelte.exec_method('svelte.compile', filename_in, *self.) unless self.dir_out_overwrite raise "Error: file exists: #{fn_out}\n use option: 'dir_out_overwrite: true'" if File.exist?(fn_out) end # write IO.write(fn_out, sv_hash['code']) end ## # TODO: research thread safety (the following is adopted) @env = env.dup @app.call(@env) end |