Class: Levels::Input::Ruby::DSL
- Inherits:
-
Object
- Object
- Levels::Input::Ruby::DSL
- Includes:
- Runtime
- Defined in:
- lib/levels/input/ruby.rb
Overview
The Ruby syntax DSL. This syntax aims to be very readable and writable. A stateful syntax and no trailing commas allows values to be easily moved around without excess text editing.
Examples
group :webserver
set hostname: "localhost"
group :task_queue
set workers: 1
set queues: -> { ["high", "low", webserver.hostname] }
Constant Summary
Constants included from Runtime
Instance Method Summary collapse
- #close_current_group ⇒ Object
-
#group(name) ⇒ Object
Public: Start a group.
-
#initialize(level) ⇒ DSL
constructor
A new instance of DSL.
- #inspect ⇒ Object
-
#set(*args) ⇒ Object
Public: Set a key/value pair in the current group.
- #to_s ⇒ Object
Methods included from Runtime
Constructor Details
#initialize(level) ⇒ DSL
Returns a new instance of DSL.
33 34 35 |
# File 'lib/levels/input/ruby.rb', line 33 def initialize(level) @level = level end |
Instance Method Details
#close_current_group ⇒ Object
103 104 105 106 107 |
# File 'lib/levels/input/ruby.rb', line 103 def close_current_group @level.set_group(@group, @hash) if @group @group = nil @hash = nil end |
#group(name) ⇒ Object
Public: Start a group.
name - Symbol or String name of the group.
Returns nothing. Raises a RuntimeError if the group has already been defined.
43 44 45 46 47 48 49 50 51 |
# File 'lib/levels/input/ruby.rb', line 43 def group(name) close_current_group if @level.defined?(name) raise RuntimeError, "Group has already been created: #{name.inspect}" end @group = name @hash = {} nil end |
#inspect ⇒ Object
113 114 115 |
# File 'lib/levels/input/ruby.rb', line 113 def inspect "<Levels>" end |
#set(*args) ⇒ Object
Public: Set a key/value pair in the current group.
Arguments
hash - Hash of key/value pairs. The keys may be a Symbol or String.
key - Symbol or String key. value - Any value.
Examples
# Ruby 1.9 Hash.
set key: "value"
# Ruby 1.9 Hash.
set :key => "value"
# Key can be a String.
set "key" => "value"
# Multiple key/values.
set :key => "value", :more => "values"
# Key, value.
set :key, "value"
Returns nothing. Raises SyntaxError if no current group is defined. Raises ArgumentError if other forms of arguments are given Raises RuntimeError if a key has already been set.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/levels/input/ruby.rb', line 83 def set(*args) raise SyntaxError, "No group is defined" if @hash.nil? case when args.size == 1 && Hash === args.first hash = args.first when args.size == 2 && !(args.any? { |a| Hash === a }) hash = { args.first => args.last } else raise ArgumentError, "Set must be given a Hash or two arguments. Got #{args.inspect}" end key_values = Levels::KeyValues.new(@hash) hash.keys.each do |key| if key_values.key?(key) raise RuntimeError, "Key has already been set: #{key.inspect}" end end @hash.update(hash) nil end |
#to_s ⇒ Object
109 110 111 |
# File 'lib/levels/input/ruby.rb', line 109 def to_s "<Levels>" end |