Class: Mxrb::Compiler::GenericWidgetBundleCompiler

Inherits:
Object
  • Object
show all
Includes:
ModelValues
Defined in:
lib/mxrb/compiler/generic_widget_bundle_compiler.rb

Overview

Compiles schema-described pluggable widgets whose properties use the standard client contracts. rubocop:disable Metrics

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, page_name, widget, scope:, entity:, render_widgets: nil, key_prefix: 'p', action_property: nil) ⇒ GenericWidgetBundleCompiler

Returns a new instance of GenericWidgetBundleCompiler.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mxrb/compiler/generic_widget_bundle_compiler.rb', line 15

def initialize(source, page_name, widget, scope:, entity:, render_widgets: nil, key_prefix: 'p',
               action_property: nil)
  @source = source
  @page_name = page_name
  @widget = widget
  @scope = scope
  @entity = entity.to_s
  @render_widgets = render_widgets
  @key_prefix = key_prefix
  @action_property = action_property
  @index = source.document_index
  @widget_type = find_widget_type
  @values = property_values(widget['Object'])
  @list_data_source = @values.values.find { _1.first == 'DataSource' }&.last
  @component_name = widget_id.to_s.split('.').last
  @module_path = widget_id.to_s.tr('.', '/')
end

Instance Attribute Details

#component_nameObject (readonly)

Returns the value of attribute component_name.



13
14
15
# File 'lib/mxrb/compiler/generic_widget_bundle_compiler.rb', line 13

def component_name
  @component_name
end

#module_pathObject (readonly)

Returns the value of attribute module_path.



13
14
15
# File 'lib/mxrb/compiler/generic_widget_bundle_compiler.rb', line 13

def module_path
  @module_path
end

Instance Method Details

#renderObject



38
39
40
41
42
43
44
45
# File 'lib/mxrb/compiler/generic_widget_bundle_compiler.rb', line 38

def render
  properties = @values.each_with_object({}) do |(key, pair), result|
    compiled = compile_property(pair)
    result[key.to_sym] = compiled unless compiled.equal?(:undefined)
  end
  properties.merge!(key: widget_key, '$widgetId': widget_key, class: css_class)
  "React.createElement($#{@component_name}, #{javascript(properties)})"
end

#supported?Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/mxrb/compiler/generic_widget_bundle_compiler.rb', line 33

def supported?
  present_identifier?(@component_name) && package_module? &&
    @values.values.all? { supported_property?(_1) }
end