Class: Ergane::CommandDefinition::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/ergane/command_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSL

Returns a new instance of DSL.



139
140
141
142
143
144
145
# File 'lib/ergane/command_definition.rb', line 139

def initialize
  @config = {
    switch_definitions: {},
    run_block: nil,
    requirements_block: nil
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(k, v) ⇒ Object



147
148
149
# File 'lib/ergane/command_definition.rb', line 147

def method_missing(k, v)
  @config[k] = v
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



137
138
139
# File 'lib/ergane/command_definition.rb', line 137

def config
  @config
end

Instance Method Details

#option(label, short: nil, kind: nil, description: nil, default: nil, &block) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/ergane/command_definition.rb', line 152

def option(label, short: nil, kind: nil, description: nil, default: nil, &block)
  label, argument = case label
    when Hash
      [label.keys.first, "=#{label.values.first}"]
    else
      [label, nil]
    end
  warn "Warning! #{label.inspect} option is being redefined.".red if @config[:switch_definitions][label]
  @config[:switch_definitions][label] = SwitchDefinition.new(label, argument: argument, short: short, kind: kind, description: description, default: default, &block)
end

#requirements(options, &block) ⇒ Object



166
167
168
# File 'lib/ergane/command_definition.rb', line 166

def requirements(options, &block)
  @config[:requirements_block] = block
end

#run(&block) ⇒ Object



170
171
172
173
# File 'lib/ergane/command_definition.rb', line 170

def run(&block)
  # puts "setting run_block"
  @config[:run_block] = block
end

#switches(inherit: true, drop: [], &block) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ergane/command_definition.rb', line 151

def switches(inherit: true, drop: [], &block)
  def option(label, short: nil, kind: nil, description: nil, default: nil, &block)
    label, argument = case label
      when Hash
        [label.keys.first, "=#{label.values.first}"]
      else
        [label, nil]
      end
    warn "Warning! #{label.inspect} option is being redefined.".red if @config[:switch_definitions][label]
    @config[:switch_definitions][label] = SwitchDefinition.new(label, argument: argument, short: short, kind: kind, description: description, default: default, &block)
  end
  block.call if block_given?
  @config[:switch_definitions]
end