This Ruby DSL enables querying, traversing and manipulating Ruby code using a nice Rubish DSL.
- First parsing the ruby code into an object graph using
ripper2ruby
- Query the graph to find the code constructs of interest using the Query API
- Manipulate the code constructs using the Manipulation API
This project is a DSL layer on top of ripper2ruby
which again is on top of ripper
which comes with ruby 1.9.
Ruby 1.9 is therefore a requirement!
See the unit tests in the test directory for examples of use.
See the Wiki for more details.
- Ruby 1.9
- ripper2ruby >= 0.0.2
See Query API
- find(type, name, options, &block)
- inside (alias for find which requires a block argument)
The following symbols are supported [:module, :class, :variable, :assignment, :call, :block, :def]
See Manipulation API
The insert and update API support blocks and always returns the updated or inserted node
- insert(position, code, &block)
- update(:select, :with, &block)
- update(:select, :with_code, &block)
- update(:value, &block)
Supports updating
- arguments (for method calls, block and method definitions)
- assignment values (for assignments)
Argument find for update
- by position (fx :arg => '#1')
- by key name (fx :arg => :src)
- by value (fx :arg => 'blip')
Note: Update needs some polishing and is still a little buggy!
- delete
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a future version unintentionally.
- Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
- Send me a pull request. Bonus points for topic branches.
Since this API was designed I have completely redesigned a new API and ideas for the code required for it to work. The new API uses actions:
- SearchAction - returns SearchResult
- FilterAction
- UpdateAction (composite of Delete and Insert actions)
- DeleteAction
- InsertAction
- CreateAction
Actions are executed on either
- Node
- NodeSet (composite of Node - Enumerates notes)
node.find(select).where(filter) node.find(select).where(filter).limit(number) # limit: first, all
node.update_with(node to update with) node.append(node to append) node.prepend(node to prepend) node.delete
nodeset.find(select).where(filter) nodeset.find(select).where(filter).limit(number) # limit: first, all
nodeset.update_with(node to update with) nodeset.append(node to append) nodeset.prepend(node to prepend) nodeset.delete
Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.