Class: Clamp::Attribute::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/clamp/attribute/definition.rb

Overview

Represents an attribute of a Clamp::Command class.

Direct Known Subclasses

Option::Definition, Parameter::Definition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Definition

Returns a new instance of Definition.



12
13
14
15
16
17
# 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

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



19
20
21
# File 'lib/clamp/attribute/definition.rb', line 19

def description
  @description
end

#environment_variableObject (readonly)

Returns the value of attribute environment_variable.



19
20
21
# File 'lib/clamp/attribute/definition.rb', line 19

def environment_variable
  @environment_variable
end

Instance Method Details

#append_methodObject



48
49
50
# File 'lib/clamp/attribute/definition.rb', line 48

def append_method
  "append_to_#{attribute_name}" if multivalued?
end

#attribute_nameObject



64
65
66
# File 'lib/clamp/attribute/definition.rb', line 64

def attribute_name
  @attribute_name ||= infer_attribute_name
end

#default_methodObject



40
41
42
# File 'lib/clamp/attribute/definition.rb', line 40

def default_method
  "default_#{read_method}"
end

#default_valueObject



68
69
70
71
72
73
74
# File 'lib/clamp/attribute/definition.rb', line 68

def default_value
  if defined?(@default_value)
    @default_value
  elsif multivalued?
    []
  end
end

#helpObject



28
29
30
# File 'lib/clamp/attribute/definition.rb', line 28

def help
  [help_lhs, help_rhs]
end

#help_rhsObject



21
22
23
24
25
26
# 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?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/clamp/attribute/definition.rb', line 60

def hidden?
  @hidden
end

#ivar_nameObject



32
33
34
# File 'lib/clamp/attribute/definition.rb', line 32

def ivar_name
  "@#{attribute_name}"
end

#multivalued?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/clamp/attribute/definition.rb', line 52

def multivalued?
  @multivalued
end

#of(command) ⇒ Object



76
77
78
# File 'lib/clamp/attribute/definition.rb', line 76

def of(command)
  Attribute::Instance.new(self, command)
end

#option_missing_messageObject



80
81
82
83
84
85
86
87
88
89
# 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_methodObject



36
37
38
# File 'lib/clamp/attribute/definition.rb', line 36

def read_method
  attribute_name
end

#required?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/clamp/attribute/definition.rb', line 56

def required?
  @required
end

#write_methodObject



44
45
46
# File 'lib/clamp/attribute/definition.rb', line 44

def write_method
  "#{attribute_name}="
end