Skip to content

mtrense/mahuta

Repository files navigation

Mahuta

Mahuta let's you describe abstract trees as pseudo-schema based internal DSL. Use the resulting tree to generate source code, documentation or anything structured.

Named after the Tāne Mahuta in Waipoua Forest, New Zealand.

Installation

Add this line to your application's Gemfile:

gem 'mahuta'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mahuta

Usage

First require the library in your code:

require 'mahuta'

Mahuta uses modules to define the schema for specific types of nodes. A Mahuta::Schema defines the mapping between node types (such as :root and :namespace from the example below) to modules that are extend()ed onto each node object.

Each module can define arbitrary methods that should be available for each node of types that extend the specific module. One major use case for such methods would be to create new child nodes of a specific type. As a convention (and to avoid name clashes with other helper methods on node objects) you could use method names ending in an exclamation mark for those methods.

The following example uses explicit modules to define a simple schema:

module Namespace
  def namespace!(*names, &block)
    add_child :namespace, name: names, &block
  end
  def type!(name, &block)
    add_child :type, name: name, &block
  end
end
schema = Mahuta::Schema.new(
  root: Namespace, 
  namespace: Namespace
)

You can now create trees from the schema object like this:

schema.new do
  namespace! :com, :example do
    type! :Person
  end
end

For each newly created tree, Mahuta executed the block given to Schema#define() in the context of a special node of type :root so don't forget to add a definition for that type to your schema.

For a complete example have a look at example/model-definition.rb.

Development

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mtrense/mahuta. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Code of Conduct

Everyone interacting in the Mahuta project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

No description or website provided.

Topics

Resources

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published