Class: Configurable::ConfigClasses::ScalarConfig
- Inherits:
-
Object
- Object
- Configurable::ConfigClasses::ScalarConfig
- Includes:
- Configurable::ConfigTypes
- Defined in:
- lib/configurable/config_classes/scalar_config.rb
Overview
ConfigClasses are used by ConfigHash to delegate get/set configs on a receiver and to map configs between user interfaces.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
The default config value.
-
#key ⇒ Object
readonly
The config key, used as a hash key for access.
-
#metadata ⇒ Object
readonly
A hash of information used to render self in various contexts.
-
#name ⇒ Object
readonly
The config name used in interfaces where only word-based names are appropriate.
-
#reader ⇒ Object
readonly
The reader method called on a receiver during get.
-
#type ⇒ Object
readonly
The config type for self (defaults to an ObjectType).
-
#writer ⇒ Object
readonly
The writer method called on a receiver during set.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #cast(input) ⇒ Object
-
#get(receiver) ⇒ Object
Calls reader on the receiver and returns the result.
-
#initialize(key, attrs = {}) ⇒ ScalarConfig
constructor
Initializes a new Config.
-
#inspect ⇒ Object
Returns an inspect string.
-
#set(receiver, value) ⇒ Object
Calls writer on the receiver with the value.
- #uncast(value) ⇒ Object
Constructor Details
#initialize(key, attrs = {}) ⇒ ScalarConfig
Initializes a new Config. Specify attributes like default, reader, writer, type, etc. within attrs.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 34 def initialize(key, attrs={}) @key = key @name = attrs[:name] || @key.to_s check_name(@name) @default = attrs[:default] @type = attrs[:type] || ObjectType.new check_default(@default) @reader = (attrs[:reader] || name).to_sym @writer = (attrs[:writer] || "#{name}=").to_sym @metadata = attrs[:metadata] || {} end |
Instance Attribute Details
#default ⇒ Object (readonly)
The default config value.
24 25 26 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 24 def default @default end |
#key ⇒ Object (readonly)
The config key, used as a hash key for access.
11 12 13 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 11 def key @key end |
#metadata ⇒ Object (readonly)
A hash of information used to render self in various contexts.
30 31 32 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 30 def @metadata end |
#name ⇒ Object (readonly)
The config name used in interfaces where only word-based names are appropriate. Names are strings consisting of only word characters.
15 16 17 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 15 def name @name end |
#reader ⇒ Object (readonly)
The reader method called on a receiver during get.
18 19 20 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 18 def reader @reader end |
#type ⇒ Object (readonly)
The config type for self (defaults to an ObjectType)
27 28 29 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 27 def type @type end |
#writer ⇒ Object (readonly)
The writer method called on a receiver during set.
21 22 23 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 21 def writer @writer end |
Instance Method Details
#[](key) ⇒ Object
48 49 50 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 48 def [](key) [key] end |
#cast(input) ⇒ Object
62 63 64 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 62 def cast(input) type.cast(input) end |
#get(receiver) ⇒ Object
Calls reader on the receiver and returns the result.
53 54 55 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 53 def get(receiver) receiver.send(reader) end |
#inspect ⇒ Object
Returns an inspect string.
71 72 73 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 71 def inspect "#<#{self.class}:#{object_id} key=#{key} name=#{name} default=#{default.inspect} reader=#{reader} writer=#{writer} >" end |
#set(receiver, value) ⇒ Object
Calls writer on the receiver with the value.
58 59 60 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 58 def set(receiver, value) receiver.send(writer, value) end |
#uncast(value) ⇒ Object
66 67 68 |
# File 'lib/configurable/config_classes/scalar_config.rb', line 66 def uncast(value) type.uncast(value) end |