Class: Mxrb::WidgetPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/widget_package.rb

Overview

Reads pluggable-widget metadata directly from installed MPK files and builds the embedded Mendix WidgetType/WidgetObject pair without Studio Pro.

Defined Under Namespace

Classes: Definition

Constant Summary collapse

TYPE_MAP =

rubocop:disable Metrics/ClassLength

{
  'attribute' => 'Attribute', 'expression' => 'Expression',
  'texttemplate' => 'TextTemplate', 'widgets' => 'Widgets',
  'enumeration' => 'Enumeration', 'boolean' => 'Boolean',
  'integer' => 'Integer', 'datasource' => 'DataSource', 'action' => 'Action',
  'selection' => 'Selection', 'association' => 'Association', 'object' => 'Object',
  'string' => 'String', 'decimal' => 'Decimal', 'icon' => 'Icon',
  'image' => 'Image', 'file' => 'File'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ WidgetPackage

Returns a new instance of WidgetPackage.



37
38
39
# File 'lib/mxrb/widget_package.rb', line 37

def initialize(path)
  @path = path
end

Class Method Details

.find(root, widget_id) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/mxrb/widget_package.rb', line 25

def self.find(root, widget_id)
  Dir.glob(File.join(File.expand_path(root), 'widgets', '*.mpk')).sort.each do |path|
    definition = new(path).definition(widget_id)
    return definition if definition
  rescue Zip::Error, REXML::ParseException
    next
  end
  nil
end

.template(definition) ⇒ Object



35
# File 'lib/mxrb/widget_package.rb', line 35

def self.template(definition) = allocate.template(definition)

Instance Method Details

#definition(widget_id) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mxrb/widget_package.rb', line 41

def definition(widget_id)
  Zip::File.open(@path) do |archive|
    widget_entries(archive).each do |entry|
      root = REXML::Document.new(entry.get_input_stream.read).root
      next unless root&.attributes&.[]('id') == widget_id

      return build_definition(root)
    end
  end
  nil
end

#template(definition) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mxrb/widget_package.rb', line 53

def template(definition) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  object_type_id = SecureRandom.uuid
  property_types = []
  properties = []
  definition.properties.each do |property|
    property_type, value = property_pair(property)
    property_types << property_type
    properties << value if value
  end
  type = {
    '$ID' => SecureRandom.uuid, '$Type' => 'CustomWidgets$CustomWidgetType',
    'HelpUrl' => definition.help_url,
    'ObjectType' => {
      '$ID' => object_type_id, '$Type' => 'CustomWidgets$WidgetObjectType',
      'PropertyTypes' => IO::BsonCodec.build_array(property_types, marker: 2)
    },
    'OfflineCapable' => definition.offline,
    'StudioCategory' => definition.studio_category,
    'StudioProCategory' => definition.studio_pro_category,
    'SupportedPlatform' => definition.platform,
    'WidgetDescription' => definition.description,
    'WidgetId' => definition.id, 'WidgetName' => definition.name,
    'WidgetNeedsEntityContext' => definition.needs_context,
    'WidgetPluginWidget' => definition.plugin
  }
  object = {
    '$ID' => SecureRandom.uuid, '$Type' => 'CustomWidgets$WidgetObject',
    'Properties' => IO::BsonCodec.build_array(properties, marker: 2),
    'TypePointer' => object_type_id
  }
  [type, object]
end