Class: Configurations::Configuration
- Inherits:
-
BlankObject
- Object
- BlankObject
- Configurations::Configuration
- Defined in:
- lib/configurations/configuration.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.
-
#__configured?(property) ⇒ Boolean
Whether the given property has been configured.
-
#__empty? ⇒ Boolean
Whether this configuration is empty.
-
#from_h(h) ⇒ Configuration
A convenience accessor to instantiate a configuration from a hash.
-
#initialize(options = {}, &block) ⇒ Configuration
constructor
Initialize a new configuration.
-
#inspect(debug = false) ⇒ String
Inspect a configuration.
-
#method_missing(method, *args, &block) ⇒ Object
Method missing gives access to Kernel methods.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Respond to missing according to the method_missing implementation.
-
#to_h ⇒ Hash
A convenience accessor to get a hash representation of the current state of the configuration.
Methods inherited from BlankObject
Constructor Details
#initialize(options = {}, &block) ⇒ Configuration
Initialize a new configuration
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/configurations/configuration.rb', line 21 def initialize( = {}, &block) @data = Data.new(__configuration_hash__) @path = .fetch(:path) { Path.new } @data_map = .fetch(:data) { Maps::Data.new } @methods = .fetch(:methods) { ::Hash.new } @method_blocks = .fetch(:method_blocks) { Maps::Blocks.new } @not_configured_blocks = .fetch(:not_configured_blocks) { Maps::Blocks.new } @reserved_method_validator = Validators::ReservedMethods.new @key_ambiguity_validator = Validators::Ambiguity.new __instance_eval__(&[:defaults]) if [:defaults] __instance_eval__(&block) if block __install_configuration_methods__ end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Method missing gives access to Kernel methods
41 42 43 44 45 46 47 |
# File 'lib/configurations/configuration.rb', line 41 def method_missing(method, *args, &block) if __can_delegate_to_kernel?(method) ::Kernel.__send__(method, *args, &block) else super end end |
Instance Method Details
#__configurable?(property) ⇒ Boolean
Returns whether the given property is configurable.
106 107 108 109 110 111 112 |
# File 'lib/configurations/configuration.rb', line 106 def __configurable?(property) if defined?(@configurable_properties) && @configurable_properties @configurable_properties.configurable?(@path.add(property)) else true end end |
#__configured?(property) ⇒ Boolean
Returns whether the given property has been configured.
117 118 119 |
# File 'lib/configurations/configuration.rb', line 117 def __configured?(property) @data.key?(property) end |
#__empty? ⇒ Boolean
Returns whether this configuration is empty.
122 123 124 |
# File 'lib/configurations/configuration.rb', line 122 def __empty? @data.empty? end |
#from_h(h) ⇒ Configuration
A convenience accessor to instantiate a configuration from a hash
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/configurations/configuration.rb', line 74 def from_h(h) @key_ambiguity_validator.validate!(h) h.each do |property, value| p = property.to_sym if value.is_a?(::Hash) && __nested?(p) @data[p].from_h(value) elsif __configurable?(p) __assign!(p, value) end end self end |
#inspect(debug = false) ⇒ String
Inspect a configuration. Implements inspect without exposing internally used instance variables.
95 96 97 98 99 100 101 |
# File 'lib/configurations/configuration.rb', line 95 def inspect(debug = false) unless debug '#<%s:0x00%x @data=%s>' % [__class__, object_id << 1, @data.inspect] else super() end end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Respond to missing according to the method_missing implementation
51 52 53 |
# File 'lib/configurations/configuration.rb', line 51 def respond_to_missing?(method, include_private = false) __can_delegate_to_kernel?(method) || super end |
#to_h ⇒ Hash
A convenience accessor to get a hash representation of the current state of the configuration
59 60 61 62 63 64 65 |
# File 'lib/configurations/configuration.rb', line 59 def to_h @data.reduce({}) do |h, (k, v)| h[k] = v.is_a?(__class__) ? v.to_h : v h end end |