class Clamp::Attribute::Definition

Represents an attribute of a Clamp::Command class.

Attributes

description[R]
environment_variable[R]

Public Class Methods

new(options) click to toggle source
# File lib/clamp/attribute/definition.rb, line 12
def initialize(options)
  @attribute_name = options[:attribute_name].to_s if options.key?(:attribute_name)
  @default_value = options[:default] if options.key?(:default)
  @environment_variable = options[:environment_variable] if options.key?(:environment_variable)
  @hidden = options[:hidden] if options.key?(:hidden)
end

Public Instance Methods

append_method() click to toggle source
# File lib/clamp/attribute/definition.rb, line 48
def append_method
  "append_to_#{attribute_name}" if multivalued?
end
attribute_name() click to toggle source
# File lib/clamp/attribute/definition.rb, line 64
def attribute_name
  @attribute_name ||= infer_attribute_name
end
default_method() click to toggle source
# File lib/clamp/attribute/definition.rb, line 40
def default_method
  "default_#{read_method}"
end
default_value() click to toggle source
# File lib/clamp/attribute/definition.rb, line 68
def default_value
  if defined?(@default_value)
    @default_value
  elsif multivalued?
    []
  end
end
help() click to toggle source
# File lib/clamp/attribute/definition.rb, line 28
def help
  [help_lhs, help_rhs]
end
help_rhs() click to toggle source
# File lib/clamp/attribute/definition.rb, line 21
def help_rhs
  rhs = description
  comments = required_indicator || default_description
  rhs += " (#{comments})" if comments
  rhs
end
hidden?() click to toggle source
# File lib/clamp/attribute/definition.rb, line 60
def hidden?
  @hidden
end
ivar_name() click to toggle source
# File lib/clamp/attribute/definition.rb, line 32
def ivar_name
  "@#{attribute_name}"
end
multivalued?() click to toggle source
# File lib/clamp/attribute/definition.rb, line 52
def multivalued?
  @multivalued
end
of(command) click to toggle source
# File lib/clamp/attribute/definition.rb, line 76
def of(command)
  Attribute::Instance.new(self, command)
end
option_missing_message() click to toggle source
# File lib/clamp/attribute/definition.rb, line 80
def option_missing_message
  if environment_variable
    Clamp.message(:option_or_env_required,
                  option: switches.first,
                  env: environment_variable)
  else
    Clamp.message(:option_required,
                  option: switches.first)
  end
end
read_method() click to toggle source
# File lib/clamp/attribute/definition.rb, line 36
def read_method
  attribute_name
end
required?() click to toggle source
# File lib/clamp/attribute/definition.rb, line 56
def required?
  @required
end
write_method() click to toggle source
# File lib/clamp/attribute/definition.rb, line 44
def write_method
  "#{attribute_name}="
end

Private Instance Methods

default_description() click to toggle source
# File lib/clamp/attribute/definition.rb, line 93
def default_description
  default_sources = [
    ("$#{@environment_variable}" if defined?(@environment_variable)),
    (@default_value.inspect if defined?(@default_value))
  ].compact
  return nil if default_sources.empty?
  "#{Clamp.message(:default)}: " + default_sources.join(", #{Clamp.message(:or)} ")
end