Class: FunWith::Configurations::Config
- Includes:
- ConfigOverriddenMethods
- Defined in:
- lib/fun_with/configurations/config.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](sym) ⇒ Object
- #[]=(sym, val) ⇒ Object
- #each(*args, &block) ⇒ Object
-
#fwc {|_self| ... } ⇒ Object
Useful when assigning selective settings from a config tree.
-
#fwc_assign!(obj = self.fwc_configured_object) ⇒ Object
Assigns the topmost set of configuration symbols as instance variables.
-
#fwc_configured_object ⇒ Object
Every subtree / subconfig in the config hierarchy will share the same configured object.
-
#fwc_configured_object=(obj) ⇒ Object
used for assigning the configured.
- #fwc_overridden_methods ⇒ Object
- #fwc_parent ⇒ Object
-
#fwc_root ⇒ Object
returns the topmost configuration, obviously.
- #fwc_root? ⇒ Boolean
- #fwc_save(file = nil) ⇒ Object
-
#initialize(key_to_self = nil, parent = nil, &block) ⇒ Config
constructor
A new instance of Config.
- #method_missing(method, *args, &block) ⇒ Object
-
#promote_configuration(*keys) ⇒ Object
Say you had a configuration that had multiple entries, and you wanted to select from among them at runtime.
- #to_hash ⇒ Object
- #to_ruby_code(indent = 0) ⇒ Object
- #to_s(style = :hash) ⇒ Object
- #try(*keys) ⇒ Object
Methods included from ConfigOverriddenMethods
Constructor Details
#initialize(key_to_self = nil, parent = nil, &block) ⇒ Config
Returns a new instance of Config.
6 7 8 9 10 11 |
# File 'lib/fun_with/configurations/config.rb', line 6 def initialize( key_to_self = nil, parent = nil, &block ) @key_to_self = key_to_self @parent = parent @config_vars = {} self.instance_exec( &block ) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/fun_with/configurations/config.rb', line 13 def method_missing( method, *args, &block ) method = method.to_s.gsub( /=$/, '' ).to_sym if block_given? self[method] = Config.new(method, self) unless self[method].is_a?(Config) self[method].instance_exec( &block ) elsif args.length == 1 self[method] = args.first elsif args.length > 1 self[method] = args else self[method] end end |
Class Method Details
.from_hash(hash) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/fun_with/configurations/config.rb', line 123 def self.from_hash( hash ) (config = self.new).tap do for k, v in hash config.send( k, v.is_a?( Hash ) ? self.from_hash( v ) : v ) end end config end |
.fwc_overridden_methods ⇒ Object
132 133 134 |
# File 'lib/fun_with/configurations/config.rb', line 132 def self.fwc_overridden_methods ConfigOverriddenMethods.instance_methods.grep( /[^=]$/ ) end |
.key_check(sym) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/fun_with/configurations/config.rb', line 63 def self.key_check( sym ) @reserved_symbols ||= Config.instance_methods - self.fwc_overridden_methods raise KeyError.new("#{sym} is not a symbol") unless sym.is_a?(Symbol) raise KeyError.new("#{sym} is reserved for use by Hash") if @reserved_symbols.include?( sym ) end |
Instance Method Details
#[](sym) ⇒ Object
28 29 30 31 32 |
# File 'lib/fun_with/configurations/config.rb', line 28 def []( sym ) sym = sym.to_sym if sym.is_a?(String) self.class.key_check( sym ) @config_vars[ sym ] end |
#[]=(sym, val) ⇒ Object
34 35 36 37 38 |
# File 'lib/fun_with/configurations/config.rb', line 34 def []=( sym, val ) sym = sym.to_sym if sym.is_a?(String) self.class.key_check( sym ) @config_vars[ sym ] = val end |
#each(*args, &block) ⇒ Object
98 99 100 |
# File 'lib/fun_with/configurations/config.rb', line 98 def each( *args, &block ) @config_vars.each( *args, &block ) end |
#fwc {|_self| ... } ⇒ Object
Useful when assigning selective settings from a config tree. Example:
RailsSite.config.constant_contact.fwc do |config|
@key = config.oauth.key
@secret = config.oauth.secret
@redir = config.oauth.redirect_url
@user = config.user
@password = config.password
end
208 209 210 211 |
# File 'lib/fun_with/configurations/config.rb', line 208 def fwc( &block ) yield self if block_given? self end |
#fwc_assign!(obj = self.fwc_configured_object) ⇒ Object
Assigns the topmost set of configuration symbols as instance variables. Translates any subconfigurations into a hash.
Example:
config.set.subset.subsubset.fwc_assign!( obj )
Say that the referenced config had keys :name, :height, :gravatar, :public_key, then the object would be assigned the corresponding instance_vars, @name, @height, etc.
149 150 151 152 153 154 155 |
# File 'lib/fun_with/configurations/config.rb', line 149 def fwc_assign!( obj = self.fwc_configured_object ) for k, v in self obj.instance_variable_set( "@#{k}", v.is_a?( Config ) ? v.to_hash : v ) end obj end |
#fwc_configured_object ⇒ Object
Every subtree / subconfig in the config hierarchy will share the same configured object.
179 180 181 182 183 184 185 |
# File 'lib/fun_with/configurations/config.rb', line 179 def fwc_configured_object if self.fwc_root? @configured_object else self.fwc_root.fwc_configured_object end end |
#fwc_configured_object=(obj) ⇒ Object
used for assigning the configured
188 189 190 191 192 193 194 |
# File 'lib/fun_with/configurations/config.rb', line 188 def fwc_configured_object=( obj ) if self.fwc_root? @configured_object = obj else self.fwc_root.fwc_configured_object = obj end end |
#fwc_overridden_methods ⇒ Object
136 137 138 |
# File 'lib/fun_with/configurations/config.rb', line 136 def fwc_overridden_methods self.class.fwc_overridden_methods end |
#fwc_parent ⇒ Object
174 175 176 |
# File 'lib/fun_with/configurations/config.rb', line 174 def fwc_parent @parent end |
#fwc_root ⇒ Object
returns the topmost configuration, obviously
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/fun_with/configurations/config.rb', line 158 def fwc_root root = self while true if root.fwc_parent.is_a?(Config) root = root.fwc_parent else return root end end end |
#fwc_root? ⇒ Boolean
170 171 172 |
# File 'lib/fun_with/configurations/config.rb', line 170 def fwc_root? self == self.fwc_root end |
#fwc_save(file = nil) ⇒ Object
214 215 216 217 218 219 220 221 |
# File 'lib/fun_with/configurations/config.rb', line 214 def fwc_save( file = nil ) raise "NOT TESTED!" root = self.fwc_root file = (file || root.fwc_configuration_file).fwf_filepath file.write( root.to_s ) end |
#promote_configuration(*keys) ⇒ Object
Say you had a configuration that had multiple entries, and you wanted to select from among them at runtime. Example: config:
important_folder:
development: "/this/directory",
test: "/that/directory",
production: "~/another/directory"
You could do config.important_folder every time you want to access that setting. Or you can do config.important_folder.promote_configuration(:development) and have the development subconfiguration replace the important_folder: configuration
You can promote a sub-sub-sub-config by sending an array of symbols. But I hope it never comes to that.
54 55 56 57 58 59 60 61 |
# File 'lib/fun_with/configurations/config.rb', line 54 def promote_configuration( *keys ) replace_with = self.try.config_method_chain_result( keys ) if replace_with.success? @parent[@key_to_self] = replace_with.config else raise ChainError.new( "config failed to promote_configuration #{keys.inspect}" ) end end |
#to_hash ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/fun_with/configurations/config.rb', line 115 def to_hash (hash = {}).tap do for k, v in @config_vars hash[k] = v.is_a?(Config) ? v.to_hash : v end end end |
#to_ruby_code(indent = 0) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/fun_with/configurations/config.rb', line 78 def to_ruby_code( indent = 0 ) (code = "").tap do if indent == 0 code << "FunWith::Configurations::Config.new do\n" code << self.to_ruby_code( 2 ) code << "end\n" else for k, v in @config_vars if v.is_a?( Config ) code << (" " * indent) + "#{k} do\n" code << v.to_ruby_code( indent + 2 ) code << (" " * indent) + "end\n" else code << (" " * indent) + "#{k} #{v.inspect}\n" end end end end end |
#to_s(style = :hash) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/fun_with/configurations/config.rb', line 102 def to_s( style = :hash ) case style when :hash self.to_hash.inspect when :ruby self.to_ruby_code when :yaml Psych.dump( self.to_hash ) else super end end |
#try(*keys) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/fun_with/configurations/config.rb', line 70 def try( *keys ) (t = TryObject.new( self )).tap do for key in keys t[key] end end end |