Class: Asbru::Components::Sage

Inherits:
Object
  • Object
show all
Extended by:
React::Rails::ViewHelper
Defined in:
lib/asbru/components/sage.rb

Overview

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!

Class Method Summary collapse

Class Method Details

.method_missing(method_id, **opts) ⇒ Object

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.



39
40
41
42
43
44
45
46
47
# File 'lib/asbru/components/sage.rb', line 39

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

.setupObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/asbru/components/sage.rb', line 13

def setup
  debugger
  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