Class: Clamp::Attribute::Instance
- Inherits:
-
Object
- Object
- Clamp::Attribute::Instance
- Defined in:
- lib/clamp/attribute/instance.rb
Overview
Represents an option/parameter 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
-
#_read ⇒ Object
default implementation of read_method.
-
#_write(value) ⇒ Object
default implementation of write_method.
- #default ⇒ Object
- #default_from_environment ⇒ Object
- #defined? ⇒ Boolean
-
#get ⇒ Object
get value directly.
-
#initialize(attribute, command) ⇒ Instance
constructor
A new instance of Instance.
- #read ⇒ Object
-
#set(value) ⇒ Object
set value directly.
- #write(value) ⇒ Object
Constructor Details
#initialize(attribute, command) ⇒ Instance
Returns a new instance of Instance.
8 9 10 11 |
# File 'lib/clamp/attribute/instance.rb', line 8 def initialize(attribute, command) @attribute = attribute @command = command end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
13 14 15 |
# File 'lib/clamp/attribute/instance.rb', line 13 def attribute @attribute end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
13 14 15 |
# File 'lib/clamp/attribute/instance.rb', line 13 def command @command end |
Instance Method Details
#_read ⇒ Object
default implementation of read_method
34 35 36 37 38 39 40 |
# File 'lib/clamp/attribute/instance.rb', line 34 def _read if self.defined? get else default end end |
#_write(value) ⇒ Object
default implementation of write_method
43 44 45 46 47 48 49 50 |
# File 'lib/clamp/attribute/instance.rb', line 43 def _write(value) if attribute.multivalued? current_values = get || [] set(current_values + [value]) else set(value) end end |
#default ⇒ Object
29 30 31 |
# File 'lib/clamp/attribute/instance.rb', line 29 def default command.send(attribute.default_method) end |
#default_from_environment ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/clamp/attribute/instance.rb', line 60 def default_from_environment return if self.defined? return if attribute.environment_variable.nil? return unless ENV.has_key?(attribute.environment_variable) # Set the parameter value if it's environment variable is present value = ENV[attribute.environment_variable] begin write(value) rescue ArgumentError => e command.send(:signal_usage_error, "$#{attribute.environment_variable}: #{e.message}") end end |
#defined? ⇒ Boolean
15 16 17 |
# File 'lib/clamp/attribute/instance.rb', line 15 def defined? command.instance_variable_defined?(attribute.ivar_name) end |
#get ⇒ Object
get value directly
20 21 22 |
# File 'lib/clamp/attribute/instance.rb', line 20 def get command.instance_variable_get(attribute.ivar_name) end |
#read ⇒ Object
52 53 54 |
# File 'lib/clamp/attribute/instance.rb', line 52 def read command.send(attribute.read_method) end |
#set(value) ⇒ Object
set value directly
25 26 27 |
# File 'lib/clamp/attribute/instance.rb', line 25 def set(value) command.instance_variable_set(attribute.ivar_name, value) end |
#write(value) ⇒ Object
56 57 58 |
# File 'lib/clamp/attribute/instance.rb', line 56 def write(value) command.send(attribute.write_method, value) end |