1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
require "clamp/attribute/instance"
module Clamp
module Attribute
class Definition
def initialize(options)
if options.key?(:attribute_name)
@attribute_name = options[:attribute_name].to_s
end
@default_value = options[:default] if options.key?(:default)
if options.key?(:environment_variable)
@environment_variable = options[:environment_variable]
end
@hidden = options[:hidden] if options.key?(:hidden)
end
attr_reader :description, :environment_variable
def help_rhs
description + default_description
end
def help
[help_lhs, help_rhs]
end
def ivar_name
"@#{attribute_name}"
end
def read_method
attribute_name
end
def default_method
"default_#{read_method}"
end
def write_method
"#{attribute_name}="
end
def append_method
"append_to_#{attribute_name}" if multivalued?
end
def multivalued?
@multivalued
end
def required?
@required
end
def hidden?
@hidden
end
def attribute_name
@attribute_name ||= infer_attribute_name
end
def default_value
if defined?(@default_value)
@default_value
elsif multivalued?
[]
end
end
def of(command)
Attribute::Instance.new(self, command)
end
private
def default_description
default_sources = [
("$#{@environment_variable}" if defined?(@environment_variable)),
(@default_value.inspect if defined?(@default_value))
].compact
return "" if default_sources.empty?
" (default: " + default_sources.join(", or ") + ")"
end
end
end
end
|