This Ruby object mapper is a module designed to convert XMI (XML Metadata Interchange) files into Ruby objects. It can also leverage the nokogiri
gem for XML parsing and provides methods to dynamically generate Ruby classes and modules based on the XML structure, particularly those related to EA (Enterprise Architect) MDG (Model Driven Generation) extensions.
Add this line to your application’s Gemfile:
gem "xmi"
And then execute:
$ bundle install
Or install it yourself as:
$ gem install xmi
To convert XMI file into Ruby objects, run:
xml = "path/to/your/file.xmi"
xml_content = File.read(xml)
xmi_root_model = Xmi::Sparx::SparxRoot.parse_xml(xml_content)
This method takes the path to an XMI file and generate the Ruby objects.
To load an extension from an XML file, use the load_extension
method:
Xmi::EaRoot.load_extension("path/to/your/extension.xml")
xml = "path/to/your/file.xmi"
xml_content = File.read(xml)
xmi_root_model = Xmi::Sparx::SparxRoot.parse_xml(xml_content)
Xmi::EaRoot.load_extension
takes the path to an XML file and generate the
Ruby classes and modules defined in XML file dynamically.
Then, you can generate Ruby objects by Xmi::Sparx::SparxRoot.parse_xml
.
You can also generate Ruby files directly from the XMI content:
Xmi::EaRoot.load_extension(
input_xml_path: 'path/to/your/custom_extension.xml',
module_name: 'CustomModule'
)
Xmi::EaRoot.output_rb_file('path/to/output_file.rb')
This approach allows you to save the dynamically generated Ruby code to a file for further use.
If you would like to create an extension, which allows to be loaded later, you can create an extension XML file.
For example, you would like to create an extension:
-
A Module named
Mymodule
. -
A class named
Klass
under this module. -
The class
Klass
has two attributes:base_apply_attribute
andtag
.
First, you create an extension XML file mymodule.xml
as the following:
<?xml version='1.0' encoding='UTF-8'?>
<MYMODULE version="1.0">
<UMLProfiles>
<UMLProfile profiletype="uml2">
<Documentation name="MYMODULE" version="1.0"
URI="http://www.test.com/profiles/MYMODULE/1.0" />
<Content>
<Stereotypes>
<Stereotype name="klass">
<AppliesTo>
<Apply type="ApplyAttribute" />
</AppliesTo>
<TaggedValues>
<Tag name="tag" type="String" description="" unit="" values="" default=""/>
</TaggedValues>
</Stereotype>
</Stereotypes>
<TaggedValueTypes />
<ViewDefinitions />
<Metamodel />
</Content>
</UMLProfile>
</UMLProfiles>
</MYMODULE>
-
The attribute
name
in theDocumentation
defines the module name. -
The attribute
URI
in theDocumentation
defines the namespace. -
The attribute
version
in theDocumentation
defines the version. -
The attribute
name
in theStereotype
defines the class name. -
The attribute
type
in theApply
defines the attribute name with prefixbase_
. -
The attribute
name
in theTag
defines the attribute name.
To load the extension, you can use the following code:
mymodule_xml = "mymodule.xml"
Xmi::EaRoot.load_extension(mymodule_xml)
After you load the extension, a class Klass
have been generated in the module
Mymodule
.
module Xmi
class EaRoot
module Mymodule
class Klass < Shale::Mapper
attribute :base_apply_attribute, Shale::Type::String
attribute :tag, Shale::Type::String
xml do
root "import"
map_attribute "base_ApplyAttribute", to: :base_apply_attribute
map_attribute "tag", to: :tag
end
end
end
end
end
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to [rubygems.org](https://rubygems.org).
Everyone interacting in the Xmi project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](USERNAME/xmi/blob/master/CODE_OF_CONDUCT.md).