Class: Clamp::Attribute::Instance

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

Overview

Represents an option/parameter of a Clamp::Command instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attributeObject (readonly)

Returns the value of attribute attribute.



13
14
15
# File 'lib/clamp/attribute/instance.rb', line 13

def attribute
  @attribute
end

#commandObject (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

#_append(value) ⇒ Object

default implementation of append_method



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

def _append(value)
  current_values = get || []
  set(current_values + [value])
end

#_readObject

default implementation of read_method



34
35
36
37
# File 'lib/clamp/attribute/instance.rb', line 34

def _read
  set(default) unless self.defined?
  get
end

#_replace(values) ⇒ Object

default implementation of write_method for multi-valued attributes



46
47
48
49
# File 'lib/clamp/attribute/instance.rb', line 46

def _replace(values)
  set([])
  Array(values).each { |value| take(value) }
end

#defaultObject



29
30
31
# File 'lib/clamp/attribute/instance.rb', line 29

def default
  command.send(attribute.default_method)
end

#default_from_environmentObject



63
64
65
66
67
68
69
70
71
72
73
74
# 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, "$#{attribute.environment_variable}: #{e.message}")
  end
end

#defined?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/clamp/attribute/instance.rb', line 15

def defined?
  command.instance_variable_defined?(attribute.ivar_name)
end

#getObject

get value directly



20
21
22
# File 'lib/clamp/attribute/instance.rb', line 20

def get
  command.instance_variable_get(attribute.ivar_name)
end

#readObject



51
52
53
# File 'lib/clamp/attribute/instance.rb', line 51

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

#take(value) ⇒ Object



55
56
57
58
59
60
61
# 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