Module: Restful::Configuration::Configurable
- Included in:
- Configurator
- Defined in:
- lib/restful/configuration.rb
Overview
Provides a register constructor which takes a block, exposing the created instance for fine grained configuration.
A class including configurable should declare one or more options using the option
class macro.
It expects a single option Hash for initialization.
If you override initialize, you must call super(option_hash).
A Configurable should allow an initialize with no arguments if it is going to be an element of an another Configurable’s Hash or Array option. See (Option#generate_from)
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
A configurable equals another configurable if they are of the same class and their configurations are equal.
- #deep_clone ⇒ Object
-
#deep_merge!(other) ⇒ Object
Produces a new Configurable deeply merged with the passed Configurable or Hash According to the semantics of the
deep_merge
gem’s deep_merge! call. -
#explicitly_set?(option_name) ⇒ Boolean
True if the option was explicitly set by passing in a value (as opposed to passively set by default).
-
#find_option(name) ⇒ Object
Lookup option by name.
-
#hash ⇒ Object
Equal Configurables should have equal hashes.
- #initialize(options = {}) ⇒ Object
- #option_names ⇒ Object
-
#reset ⇒ Object
Clears the current configuration.
-
#set(options) ⇒ Object
Clears the current configuration and resets with the passed options.
-
#to_hash(to_hash_options = {}) ⇒ Object
Returns a plain deeply duplicated Hash with all options as key/values.
Class Method Details
.included(base) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/restful/configuration.rb', line 21 def self.included(base) base.class_eval do extend ClassMethods class_inheritable_array :options self. = [] end end |
Instance Method Details
#==(other) ⇒ Object
A configurable equals another configurable if they are of the same class and their configurations are equal.
114 115 116 117 |
# File 'lib/restful/configuration.rb', line 114 def ==(other) return false unless other.class <= self.class return self.config == other.config end |
#deep_clone ⇒ Object
124 125 126 |
# File 'lib/restful/configuration.rb', line 124 def deep_clone Marshal.load(Marshal.dump(self)) end |
#deep_merge!(other) ⇒ Object
Produces a new Configurable deeply merged with the passed Configurable or Hash According to the semantics of the deep_merge
gem’s deep_merge! call. If the Configurable or Hash has options unknown to this Configurable, ArgumentErrors will be raised.
Empty container options and defaulted options are skipped.
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/restful/configuration.rb', line 134 def deep_merge!(other) other_hash = case other when Configurable then other.to_hash(:ignore_empty => true, :skip_defaults => true) else other end hash = to_hash(:ignore_empty => true, :skip_defaults => true) new_configurable = self.class.new hash._rs_deep_merge!(other_hash) return new_configurable.set(hash) end |
#explicitly_set?(option_name) ⇒ Boolean
True if the option was explicitly set by passing in a value (as opposed to passively set by default).
157 158 159 |
# File 'lib/restful/configuration.rb', line 157 def explicitly_set?(option_name) explicitly_set.include?(option_name.to_sym) end |
#find_option(name) ⇒ Object
Lookup option by name.
151 152 153 |
# File 'lib/restful/configuration.rb', line 151 def find_option(name) return .find { |o| o.name == name } end |
#hash ⇒ Object
Equal Configurables should have equal hashes.
120 121 122 |
# File 'lib/restful/configuration.rb', line 120 def hash return config.hash end |
#initialize(options = {}) ⇒ Object
54 55 56 57 |
# File 'lib/restful/configuration.rb', line 54 def initialize( = {}) = ( || {}).symbolize_keys set() end |
#option_names ⇒ Object
146 147 148 |
# File 'lib/restful/configuration.rb', line 146 def option_names .map { |o| o.name } end |
#reset ⇒ Object
Clears the current configuration.
60 61 62 63 64 65 66 |
# File 'lib/restful/configuration.rb', line 60 def reset @config = nil @explicitly_set = nil .each do |opt| self.config[opt.name] = opt.initialized end end |
#set(options) ⇒ Object
Clears the current configuration and resets with the passed options.
69 70 71 72 |
# File 'lib/restful/configuration.rb', line 69 def set() reset _set() end |
#to_hash(to_hash_options = {}) ⇒ Object
Returns a plain deeply duplicated Hash with all options as key/values. Deeply converts all Configurable nodes to hash as well.
-
to_hash_options
-
:ignore_empty => if
ignore_empty
is set to true, will not include
options that point to empty containers. (Default false)
-
:skip_defaults => if
skip_defaults
is set to true, will not include
options that are passively set to their default value. Options actively set to their default value during initialization should remain. (Default false)
-
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/restful/configuration.rb', line 83 def to_hash( = {}) = ( || {}).symbolize_keys ignore_empty = [:ignore_empty] || false skip_defaults = [:skip_defaults] || false deeply_dup = lambda do |element| return case element when Configurable then element.to_hash() when Array then element.map { |e| deeply_dup.call(e) } when Hash then duped = element.dup duped.each { |k,v| duped[k] = deeply_dup.call(v) } duped else element end end config_hash = {} config.each do |key,value| next if skip_defaults && !value.nil? && !explicitly_set?(key) && find_option(key).default == value duplicated = deeply_dup.call(value) unless ignore_empty && duplicated.respond_to?(:empty?) && duplicated.empty? config_hash[key] = duplicated end end return config_hash end |