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- SVELTE_RACK_ATTRS =
[:app_root_dir, :components_dir_in, :components_dir_out, :dir_out_overwrite, :dir_out_clear]
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Cogs
constructor
A new instance of Cogs.
Constructor Details
#initialize(app, options = {}) ⇒ Cogs
Returns a new instance of Cogs.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rack/svelte/cogs.rb', line 17 def initialize(app, = {}) @app = app symbolize_keys!() @app_root_dir = .fetch(:app_root_dir, DEFAULT_ROOT) @components_dir_in = .fetch(:components_dir_in, DEFAULT_COMPONENTS_IN) @components_dir_out = .fetch(:components_dir_out, DEFAULT_COMPONENTS_OUT) @dir_out_overwrite = .fetch(:dir_out_overwrite, DEFAULT_DIR_OUT_OVERWRITE) @dir_out_clear = .fetch(:dir_out_clear, DEFAULT_DIR_OUT_CLEAR) = DEFAULT_SVELTE_OPTS.merge(.reject {|x| SVELTE_RACK_ATTRS.include?(x)}) end |
Instance Method Details
#call(env) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rack/svelte/cogs.rb', line 28 def call(env) dir_in = File.join(@app_root_dir, @components_dir_in) dir_out = File.join(@app_root_dir, @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 @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, '.html') fn_out_basename_js = File.basename(fn_in, '.html') + '.js' fn_out_dir = File.dirname(fn_in) fn_out = File.join(dir_out, fn_out_dir, fn_out_basename + '.js') # Kenny Powers # ensure output dir FileUtils.mkdir_p(fn_out_dir) unless Dir.exist?(fn_out_dir) # compile comp_opts = .dup comp_opts.merge!(name: fn_out_basename) unless comp_opts.key?(:name) sv_hash = ::Svelte.exec_method('svelte.compile', filename_in, nil, comp_opts) unless @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 @app.call(env) end |