class Asbru::Components::Sage

This module uses components.json to see what components are available to be rendered. Because it defines methods on runtime instead of relying on method_missing its quite a bit faster and provides better feedback in case of errors. This implementation is just a PoC and doesn't work atm. More on this later!

Public Class Methods

method_missing(method_id, **opts) click to toggle source

We use method missing to enable the make this available via the same API as Savage. The other API here is nicer and maybe we should create a way to disable the savage API.

Calls superclass method
# File lib/asbru/components/sage.rb, line 36
def method_missing(method_id, **opts)
  return super if self.respond_to? :method_id

  name_parts = method_id.to_s.split('_')
  namespace = name_parts.shift.capitalize

  "Asbru::Components::Sage::#{namespace}".constantize
      .send(name_parts.join('_'), **opts)
end
setup() click to toggle source
# File lib/asbru/components/sage.rb, line 11
def setup
  file = File.open(Rails.root.join('app',
      'javascript',
      'components',
      'components.json'))

  JSON.parse(file.read).each do |folder|
    cname = folder['folder'].capitalize
    const_set cname, Class.new
    klass = "Asbru::Components::Sage::#{cname}".constantize
    klass.extend ::React::Rails::ViewHelper

    folder['components'].each do |component_name|
      method_name = component_name.underscore.to_sym

      klass.define_singleton_method(method_name) do |**opts|
        react_component "#{folder['folder']}.#{component_name}", **opts
      end
    end
  end
end