Class: Configurations::ArbitraryConfiguration
- Inherits:
-
Configuration
- Object
- BlankObject
- Configuration
- Configurations::ArbitraryConfiguration
- Defined in:
- lib/configurations/arbitrary.rb
Overview
Configuration is a blank object in order to allow configuration of various properties including keywords
Constant Summary
Constants inherited from BlankObject
BlankObject::ALIAS_KERNEL_METHODS, BlankObject::KEEP_KERNEL_METHODS, BlankObject::KEEP_METHODS
Instance Method Summary collapse
-
#__configurable?(_property) ⇒ Boolean
Whether the given property is configurable.
-
#__writeable__=(data) ⇒ Object
Set the configuration to writeable or read only.
-
#from_h(h) ⇒ Configuration
A convenience accessor to instantiate a configuration from a hash.
-
#initialize(options = {}, &block) {|HostModule::Configuration| ... } ⇒ HostModule::Configuration
constructor
Initialize a new configuration.
-
#method_missing(method, *args, &block) ⇒ Object
Method missing gives access for reading and writing to the underlying configuration hash via dot notation.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Respond to missing according to the method_missing implementation.
Methods inherited from Configuration
Methods inherited from BlankObject
Constructor Details
#initialize(options = {}, &block) {|HostModule::Configuration| ... } ⇒ HostModule::Configuration
An arbitrary configuration has to control its writeable state, therefore configuration is only possible in the initialization block
Initialize a new configuration
18 19 20 21 22 |
# File 'lib/configurations/arbitrary.rb', line 18 def initialize( = {}, &block) self.__writeable__ = true super self.__writeable__ = false if block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Method missing gives access for reading and writing to the underlying configuration hash via dot notation
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/configurations/arbitrary.rb', line 27 def method_missing(method, *args, &block) if __respond_to_writer?(method) __assign!(method.to_s[0..-2].to_sym, args.first) elsif __respond_to_method_for_write?(method) @data[method] elsif __respond_to_method_for_read?(method, *args, &block) @data.fetch(method, &__not_configured_callback_for__(method)) else super end end |
Instance Method Details
#__configurable?(_property) ⇒ Boolean
Returns whether the given property is configurable.
66 67 68 |
# File 'lib/configurations/arbitrary.rb', line 66 def __configurable?(_property) true end |
#__writeable__=(data) ⇒ Object
Set the configuration to writeable or read only. Access to writer methods is only allowed within the configure block, this method is used to invoke writeability for subconfigurations.
76 77 78 79 80 81 82 83 |
# File 'lib/configurations/arbitrary.rb', line 76 def __writeable__=(data) @__writeable__ = data return if @data.nil? @data.each do |_k, v| v.__writeable__ = data if v.is_a?(__class__) end end |
#from_h(h) ⇒ Configuration
can only be accessed during writeable state (in configure block). Unassignable values are ignored
A convenience accessor to instantiate a configuration from a hash
55 56 57 58 59 60 61 |
# File 'lib/configurations/arbitrary.rb', line 55 def from_h(h) unless @__writeable__ fail ::ArgumentError, 'can not dynamically assign values from a hash' end super end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Respond to missing according to the method_missing implementation
41 42 43 44 45 46 |
# File 'lib/configurations/arbitrary.rb', line 41 def respond_to_missing?(method, include_private = false) __respond_to_writer?(method) || __respond_to_method_for_read?(method, *args, &block) || __respond_to_method_for_write?(method) || super end |