Class: Configurable::Config
Overview
Configs are used by ConfigHash to determine how to delegate read/write operations to a receiver. Configs also track metadata related to their presentation in various contexts.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
An hash of metadata for self, often used to indicate how a config is presented in different contexts (ex on the command line, in a web form, or a desktop app).
-
#reader ⇒ Object
readonly
The reader method called on a receiver during get.
-
#writer ⇒ Object
readonly
The writer method called on a receiver during set.
Class Method Summary collapse
-
.duplicable_value?(value) ⇒ Boolean
Determines if the value is duplicable.
Instance Method Summary collapse
-
#[](key, default = nil) ⇒ Object
Returns the value for the specified attribute, or default if the attribute is unspecified.
-
#default(duplicate = true) ⇒ Object
Returns the default value.
-
#get(receiver) ⇒ Object
Calls reader on the receiver and returns the result.
-
#init(receiver) ⇒ Object
Sets the default value on the receiver.
-
#init? ⇒ Boolean
Returns true or false as specified in new.
-
#initialize(reader, writer = "#{reader}=", default = nil, attributes = {}, init = true, dup = nil) ⇒ Config
constructor
Initializes a new Config.
-
#inspect ⇒ Object
Returns an inspection string.
-
#set(receiver, value) ⇒ Object
Calls writer on the receiver with the value.
Constructor Details
#initialize(reader, writer = "#{reader}=", default = nil, attributes = {}, init = true, dup = nil) ⇒ Config
Initializes a new Config.
32 33 34 35 36 37 38 39 |
# File 'lib/configurable/config.rb', line 32 def initialize(reader, writer="#{reader}=", default=nil, attributes={}, init=true, dup=nil) self.reader = reader self.writer = writer @default = default @attributes = attributes @init = init @dup = dup.nil? ? Config.duplicable_value?(default) : dup end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
An hash of metadata for self, often used to indicate how a config is presented in different contexts (ex on the command line, in a web form, or a desktop app).
29 30 31 |
# File 'lib/configurable/config.rb', line 29 def attributes @attributes end |
#reader ⇒ Object
The reader method called on a receiver during get
21 22 23 |
# File 'lib/configurable/config.rb', line 21 def reader @reader end |
#writer ⇒ Object
The writer method called on a receiver during set
24 25 26 |
# File 'lib/configurable/config.rb', line 24 def writer @writer end |
Class Method Details
.duplicable_value?(value) ⇒ Boolean
Determines if the value is duplicable. Non-duplicable values include nil, true, false, Symbol, Numeric, Method, Module, and any object that does not respond to dup.
12 13 14 15 16 17 |
# File 'lib/configurable/config.rb', line 12 def duplicable_value?(value) case value when nil, true, false, Symbol, Numeric, Method, Module then false else value.respond_to?(:dup) end end |
Instance Method Details
#[](key, default = nil) ⇒ Object
Returns the value for the specified attribute, or default if the attribute is unspecified.
50 51 52 |
# File 'lib/configurable/config.rb', line 50 def [](key, default=nil) attributes.has_key?(key) ? attributes[key] : default end |
#default(duplicate = true) ⇒ Object
Returns the default value. If duplicate is specified and the default may be duplicated (see Config.duplicable_value?) then a duplicate of the default is returned.
44 45 46 |
# File 'lib/configurable/config.rb', line 44 def default(duplicate=true) duplicate && @dup ? @default.dup : @default end |
#get(receiver) ⇒ Object
Calls reader on the receiver and returns the result.
55 56 57 |
# File 'lib/configurable/config.rb', line 55 def get(receiver) receiver.send(reader) end |
#init(receiver) ⇒ Object
Sets the default value on the receiver. Normally this method is only called during Configurable#initialize_config, and only then when init? returns true.
67 68 69 |
# File 'lib/configurable/config.rb', line 67 def init(receiver) receiver.send(writer, default) end |
#init? ⇒ Boolean
Returns true or false as specified in new. True indicates that this delegate is allowed to initialize values on the receiver during Configurable#initialize_config.
74 75 76 |
# File 'lib/configurable/config.rb', line 74 def init? @init end |
#inspect ⇒ Object
Returns an inspection string.
79 80 81 |
# File 'lib/configurable/config.rb', line 79 def inspect "#<#{self.class}:#{object_id} reader=#{reader} writer=#{writer} default=#{default.inspect} >" end |
#set(receiver, value) ⇒ Object
Calls writer on the receiver with the value.
60 61 62 |
# File 'lib/configurable/config.rb', line 60 def set(receiver, value) receiver.send(writer, value) end |