Module: Ergane::DSL::Macros

Included in:
CommandDSL, Tool
Defined in:
lib/ergane/dsl/macros.rb

Overview

Helpers for defining DSL methods on a host class/module.

Instance Method Summary collapse

Instance Method Details

#dsl_value(name, default: nil) ⇒ Object

Defines a class-level “value” accessor with combined getter/setter semantics: called with a truthy argument it stores and returns it; called with none (or a falsy value) it returns the stored value, or default if unset.

dsl_value :description, default: ""
description "Deploy"   # => "Deploy" (and stored)
description            # => "Deploy"


15
16
17
18
19
20
# File 'lib/ergane/dsl/macros.rb', line 15

def dsl_value(name, default: nil)
  ivar = "@#{name}"
  define_method(name) do |value = nil|
    value ? instance_variable_set(ivar, value) : (instance_variable_get(ivar) || default)
  end
end