Are you tired of writing patterns for Machete? You can use Machete::DSL::Builder
To start working with Machete::DSL you need to execute method #build on Machete::DSL::Builder
Machete::DSL::Builder.build {} # => {}When we want to create simple assignment
Machete::DSL::Builder.build do
key "value"
end # => {:key=>"value"}When we want to use Rubinius::AST object for example FixnumLiteral we need to execute method fixnum_literal.
Machete::DSL::Builder.build do
fixnum_literal do
value "2"
end
end # => {:FixnumLiteral=>{:value=>"2"}}SendWithArguments send_with_arguments
ActualArguments actual_arguments
etc.
When value is a array you need pass :array symbol to key method
Machete::DSL::Builder.build do
key(:array) do
value "2"
value "3"
end
end # => {:key=>[{:value=>"2"}, {:value=>"3"}]}Ruby have special words.
for, if, alias, next, not, super, when, case,
while, yield, class, module, and, break, sendAnd we can't use this methods directly in DSL. You need execute method with underscore on front.
Machete::DSL::Builder.build do
_class "2"
end # => {:Class=>"2"}After require machete-dsl you can use to_m method on Hash.
Machete::DSL::Builder.build do
send_with_arguments do
name ":redirect_to"
arguments do
actual_arguments do
array(:array) do
symbol_literal do
value ":to"
end
end
name ":[]"
receiver do
_send do
name ":params"
end
end
end
end
end
endThis code will produce
SendWithArguments<
name = :redirect_to,
arguments = ActualArguments<
array = [
SymbolLiteral<value = :to>
],
name = :[],
receiver = Send<
name = :params
>
>
>Add this line to your application's Gemfile:
gem 'machete-dsl'
And then execute:
$ bundle
Or install it yourself as:
$ gem install machete-dsl
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request