class Clamp::Attribute::Instance
Represents an option/parameter of a Clamp::Command instance.
Attributes
attribute[R]
command[R]
Public Class Methods
new(attribute, command)
click to toggle source
# File lib/clamp/attribute/instance.rb, line 8 def initialize(attribute, command) @attribute = attribute @command = command end
Public Instance Methods
_append(value)
click to toggle source
default implementation of append_method
# File lib/clamp/attribute/instance.rb, line 40 def _append(value) current_values = get || [] set(current_values + [value]) end
_read()
click to toggle source
default implementation of read_method
# File lib/clamp/attribute/instance.rb, line 34 def _read set(default) unless self.defined? get end
_replace(values)
click to toggle source
default implementation of write_method for multi-valued attributes
# File lib/clamp/attribute/instance.rb, line 46 def _replace(values) set([]) Array(values).each { |value| take(value) } end
default()
click to toggle source
# File lib/clamp/attribute/instance.rb, line 29 def default command.send(attribute.default_method) end
default_from_environment()
click to toggle source
# File lib/clamp/attribute/instance.rb, line 63 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 take(value) rescue ArgumentError => e command.send(:signal_usage_error, Clamp.message(:env_argument_error, :env => attribute.environment_variable, :message => e.message)) end end
defined?()
click to toggle source
# File lib/clamp/attribute/instance.rb, line 15 def defined? command.instance_variable_defined?(attribute.ivar_name) end
get()
click to toggle source
get value directly
# File lib/clamp/attribute/instance.rb, line 20 def get command.instance_variable_get(attribute.ivar_name) end
read()
click to toggle source
# File lib/clamp/attribute/instance.rb, line 51 def read command.send(attribute.read_method) end
set(value)
click to toggle source
set value directly
# File lib/clamp/attribute/instance.rb, line 25 def set(value) command.instance_variable_set(attribute.ivar_name, value) end
take(value)
click to toggle source
# File lib/clamp/attribute/instance.rb, line 55 def take(value) if attribute.multivalued? command.send(attribute.append_method, value) else command.send(attribute.write_method, value) end end