Class: Configurable::ConfigClasses::NestConfig

Inherits:
ScalarConfig
  • Object
show all
Defined in:
lib/configurable/config_classes/nest_config.rb

Overview

Represents a config where the input is expected to be Configurable.

Instance Attribute Summary

Attributes inherited from ScalarConfig

#default, #key, #metadata, #name, #reader, #type, #writer

Instance Method Summary collapse

Methods inherited from ScalarConfig

#[], #cast, #inspect, #uncast

Constructor Details

#initialize(key, attrs = {}) ⇒ NestConfig

Returns a new instance of NestConfig.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/configurable/config_classes/nest_config.rb', line 7

def initialize(key, attrs={})
  unless attrs.has_key?(:default)
    attrs[:default] = {}
  end
  
  unless attrs.has_key?(:type)
    attrs[:type] = NestType.new(attrs)
  end
  
  super
  
  unless type.respond_to?(:init)
    raise "invalid type for #{self}: #{type.inspect}"
  end
end

Instance Method Details

#get(receiver) ⇒ Object

Calls the reader on the reciever to retreive an instance of the configurable_class and returns it’s config. Returns nil if the reader returns nil.



26
27
28
29
30
31
32
# File 'lib/configurable/config_classes/nest_config.rb', line 26

def get(receiver)
  if configurable = receiver.send(reader)
    configurable.config
  else
    nil
  end
end

#set(receiver, value) ⇒ Object

Calls the reader on the reciever to retrieve an instance of the configurable_class, and reconfigures it with value. The instance will be initialized by init if necessary.

If value is an instance of the configurable_class, then it will be set by calling writer.



40
41
42
43
44
45
46
47
# File 'lib/configurable/config_classes/nest_config.rb', line 40

def set(receiver, value)
  unless value.respond_to?(:config)
    value = default.merge(value)
    value = type.init(value)
  end
  
  receiver.send(writer, value)
end