Class: Clamp::Attribute::Instance
- Inherits:
-
Object
- Object
- Clamp::Attribute::Instance
- Defined in:
- lib/clamp/attribute/instance.rb
Overview
Represents an attribute of a Clamp::Command instance.
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
Instance Method Summary collapse
-
#_append(value) ⇒ Object
default implementation of append_method.
-
#_read ⇒ Object
default implementation of read_method.
-
#_replace(values) ⇒ Object
default implementation of write_method for multi-valued attributes.
- #default ⇒ Object
- #default_from_environment ⇒ Object
- #defined? ⇒ Boolean
-
#get ⇒ Object
get value directly.
-
#initialize(attribute, command) ⇒ Instance
constructor
A new instance of Instance.
- #missing? ⇒ Boolean
- #read ⇒ Object
-
#set(value) ⇒ Object
set value directly.
- #signal_usage_error(*args) ⇒ Object
- #take(value) ⇒ Object
- #unset? ⇒ Boolean
- #verify_not_missing ⇒ Object
Constructor Details
#initialize(attribute, command) ⇒ Instance
Returns a new instance of Instance.
10 11 12 13 |
# File 'lib/clamp/attribute/instance.rb', line 10 def initialize(attribute, command) @attribute = attribute @command = command end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
15 16 17 |
# File 'lib/clamp/attribute/instance.rb', line 15 def attribute @attribute end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
15 16 17 |
# File 'lib/clamp/attribute/instance.rb', line 15 def command @command end |
Instance Method Details
#_append(value) ⇒ Object
default implementation of append_method
42 43 44 45 |
# File 'lib/clamp/attribute/instance.rb', line 42 def _append(value) current_values = get || [] set(current_values + [value]) end |
#_read ⇒ Object
default implementation of read_method
36 37 38 39 |
# File 'lib/clamp/attribute/instance.rb', line 36 def _read set(default) unless self.defined? get end |
#_replace(values) ⇒ Object
default implementation of write_method for multi-valued attributes
48 49 50 51 |
# File 'lib/clamp/attribute/instance.rb', line 48 def _replace(values) set([]) Array(values).each { |value| take(value) } end |
#default ⇒ Object
31 32 33 |
# File 'lib/clamp/attribute/instance.rb', line 31 def default command.send(attribute.default_method) if command.respond_to?(attribute.default_method, true) end |
#default_from_environment ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/clamp/attribute/instance.rb', line 69 def default_from_environment return if self.defined? return if attribute.environment_variable.nil? return unless ENV.key?(attribute.environment_variable) # Set the parameter value if it's environment variable is present value = ENV[attribute.environment_variable] begin take(value) rescue ArgumentError => e signal_usage_error Clamp.(:env_argument_error, env: attribute.environment_variable, message: e.) end end |
#defined? ⇒ Boolean
17 18 19 |
# File 'lib/clamp/attribute/instance.rb', line 17 def defined? command.instance_variable_defined?(attribute.ivar_name) end |
#get ⇒ Object
get value directly
22 23 24 |
# File 'lib/clamp/attribute/instance.rb', line 22 def get command.instance_variable_get(attribute.ivar_name) end |
#missing? ⇒ Boolean
90 91 92 |
# File 'lib/clamp/attribute/instance.rb', line 90 def missing? attribute.required? && unset? end |
#read ⇒ Object
53 54 55 |
# File 'lib/clamp/attribute/instance.rb', line 53 def read command.send(attribute.read_method) end |
#set(value) ⇒ Object
set value directly
27 28 29 |
# File 'lib/clamp/attribute/instance.rb', line 27 def set(value) command.instance_variable_set(attribute.ivar_name, value) end |
#signal_usage_error(*args) ⇒ Object
65 66 67 |
# File 'lib/clamp/attribute/instance.rb', line 65 def signal_usage_error(*args) command.send(:signal_usage_error, *args) end |
#take(value) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/clamp/attribute/instance.rb', line 57 def take(value) if attribute.multivalued? command.send(attribute.append_method, value) else command.send(attribute.write_method, value) end end |
#unset? ⇒ Boolean
82 83 84 85 86 87 88 |
# File 'lib/clamp/attribute/instance.rb', line 82 def unset? if attribute.multivalued? read.empty? else read.nil? end end |
#verify_not_missing ⇒ Object
94 95 96 |
# File 'lib/clamp/attribute/instance.rb', line 94 def verify_not_missing signal_usage_error attribute. if missing? end |