Skip to content

Latest commit

 

History

History
56 lines (41 loc) · 2.65 KB

README.md

File metadata and controls

56 lines (41 loc) · 2.65 KB

Command Designer

Gem Version Build Status Dependency Status Code Climate Coverage Status Inline docs Yard Docs Github Code

Build command text based on multiple filters

About

This is framework to build command strings based on current context.

DSL Methods

  • initialize(priorities_array) - sets up initial context, execute all methods on this instance, try priorities: [:first, nil, :last]
  • filter(priority, options) { code } - create priority based filter for given options with the code bloc to execute
  • context(options) { code } - build new context, options are for matching filters, all code will be executes in context of given options
  • local_filter(filter_block) { code } - define local filter_block to take effect for the given code block, it's tricky as it takes two lambdas, try: local_filter(Proc.new{|cmd| "cd path && #{cmd}"}) { code }
  • command(name, *args) - build command by evaluate global and local filters in the order of given priority, local filters are called after the nil priority or on the end

Example

subject = CommandDesigner::Dsl.new([:first, nil, :last])

subject.filter(:last,  {:server => "::2" }) {|cmd| "command #{cmd}" }
subject.filter(:first, {:target => "true"}) {|cmd| "env #{cmd}" }

subject.local_filter(Proc.new{|cmd| "cd /path && #{cmd}" }) do

  subject.command("true")  # => "cd /path && env true"
  subject.command("false") # => "cd /path && false"

end

subject.context(:server => "::2") do |server2|

  # the :last filter with "command" was applied on the end
  server2.command("true") # => "command env false"
  # you do not have to use the block variable, subject works fine too
  subject.command("false") # => "command false"

end