Class: Configure::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/configure/value.rb

Overview

Handler for a single configuration value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, configuration, key) ⇒ Value

Returns a new instance of Value.



9
10
11
# File 'lib/configure/value.rb', line 9

def initialize(schema, configuration, key)
  @schema, @configuration, @key = schema, configuration, key
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



6
7
8
# File 'lib/configure/value.rb', line 6

def configuration
  @configuration
end

#keyObject

Returns the value of attribute key.



7
8
9
# File 'lib/configure/value.rb', line 7

def key
  @key
end

#schemaObject

Returns the value of attribute schema.



5
6
7
# File 'lib/configure/value.rb', line 5

def schema
  @schema
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/configure/value.rb', line 50

def exists?
  @configuration.respond_to?(:has_key?) ? @configuration.has_key?(@key) : !!self.get
end

#getObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/configure/value.rb', line 38

def get
  check_only_list!
  method_name = :"#{@key}"
  if @configuration.respond_to?(method_name)
    @configuration.send method_name
  elsif @configuration.respond_to?(:[])
    @configuration[@key]
  else
    raise Configure::InvalidKeyError, "couldn't get configuration value for key #{@key}!"
  end
end

#put(value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/configure/value.rb', line 26

def put(value)
  check_only_list!
  method_name = :"#{@key}="
  if @configuration.respond_to?(method_name)
    @configuration.send method_name, value
  elsif @configuration.respond_to?(:[]=)
    @configuration[@key] = value
  else
    raise Configure::InvalidKeyError, "couldn't set configuration value for key #{@key}!"
  end
end

#put_or_combine(value) ⇒ Object



17
18
19
20
# File 'lib/configure/value.rb', line 17

def put_or_combine(value)
  existing_value = get
  self.put existing_value.nil? ? value : [ existing_value, value ].flatten
end

#put_single_or_multiple(values) ⇒ Object



13
14
15
# File 'lib/configure/value.rb', line 13

def put_single_or_multiple(values)
  self.put_or_combine values.size == 1 ? values.first : values
end

#put_unless_existing(value) ⇒ Object



22
23
24
# File 'lib/configure/value.rb', line 22

def put_unless_existing(value)
  self.put value unless exists?
end