module Snabberb

Constants

VERSION

Public Class Methods

html_script(file, **needs) click to toggle source

takes in a file and needs calls html on the CamelCased version of the file with the needs

# File lib/snabberb.rb, line 43
  def self.html_script(file, **needs)
    klass = file.split('/').last
                .split('.').first
                .split('_').map(&:capitalize).join

    script = <<~RUBY
      #{File.read(file)}
      #{klass}.html(`#{wrap(needs)}`)
    RUBY

    Opal.compile(script).strip.chomp(';')
  end
prerender_script(layout, application, application_id, javascript_include_tags: '', **needs) click to toggle source
# File lib/snabberb.rb, line 56
  def self.prerender_script(layout, application, application_id, javascript_include_tags: '', **needs)
    needs = wrap(needs)
    attach_func = wrap_s("Opal.#{application}.$attach(\"#{application_id}\", #{needs})")

    <<~JS
      Opal.#{layout}.$html(Opal.hash({
        application: Opal.#{application}.$new(null, #{needs}).$render(),
        javascript_include_tags: '#{javascript_include_tags.gsub("\n", '')}',
        attach_func: #{attach_func}
      }))
    JS
  end
wrap(obj) click to toggle source
# File lib/snabberb.rb, line 10
def self.wrap(obj)
  case obj
  when Hash
    wrap_h(obj)
  when Array
    wrap_a(obj)
  when Numeric, TrueClass, FalseClass
    obj
  when nil
    'Opal.nil'
  else
    wrap_s(obj.to_s)
  end
end
wrap_a(array) click to toggle source
# File lib/snabberb.rb, line 29
def self.wrap_a(array)
  "[#{array.map { |v| wrap(v) }.join(',')}]"
end
wrap_h(hash) click to toggle source
# File lib/snabberb.rb, line 33
def self.wrap_h(hash)
  args = hash.flat_map do |k, v|
    [wrap_s(k), wrap(v)]
  end.join(',')

  "Opal.hash(#{args})"
end
wrap_s(str) click to toggle source
# File lib/snabberb.rb, line 25
def self.wrap_s(str)
  JSON.generate(str)
end