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 collapse
- RESERVED_METHODS =
Reserved methods are not assignable. They define behaviour needed for the configuration object to work properly.
[ :initialize, :inspect, :method_missing, :object_id, :singleton_class, # needed by rbx :to_h, :to_s # needed by rbx / 1.9.3 for inspect ]
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
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/configurations/configuration.rb', line 33 def initialize( = {}, &block) @__methods__ = .fetch(:methods) { ::Hash.new } @__not_configured__ = .fetch(:not_configured) { ::Hash.new } @data = __configuration_hash__ __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
47 48 49 50 51 52 53 |
# File 'lib/configurations/configuration.rb', line 47 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.
111 112 113 |
# File 'lib/configurations/configuration.rb', line 111 def __configurable?(_property) fail NotImplementedError, 'must be implemented in subclass' end |
#__configured?(property) ⇒ Boolean
Returns whether the given property has been configured.
118 119 120 |
# File 'lib/configurations/configuration.rb', line 118 def __configured?(property) @data.key?(property) end |
#__empty? ⇒ Boolean
Returns whether this configuration is empty.
123 124 125 |
# File 'lib/configurations/configuration.rb', line 123 def __empty? @data.empty? end |
#from_h(h) ⇒ Configuration
A convenience accessor to instantiate a configuration from a hash
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/configurations/configuration.rb', line 80 def from_h(h) __test_ambiguity!(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.
100 101 102 103 104 105 106 |
# File 'lib/configurations/configuration.rb', line 100 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
57 58 59 |
# File 'lib/configurations/configuration.rb', line 57 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
65 66 67 68 69 70 71 |
# File 'lib/configurations/configuration.rb', line 65 def to_h @data.reduce({}) do |h, (k, v)| h[k] = v.is_a?(__class__) ? v.to_h : v h end end |